1

I am stuck in a simple problem. I have two arrays:

$array1 = array ( productcode => 218133, categoryid => 315, color => red )
$array2 = (10,220)

How to merge this two arrays to have the below result?

$array1 = array ( productcode => 218133, categoryid => array (315, 10, 220),         color => red )

2 Answers 2

1
$array1 = array ( 'productcode' => 218133, 'categoryid' => 315, 'color' => 'red' );
$array2 = array (10,220);

$array1['categoryid'] = array_merge((array)$array1['categoryid'], $array2);
print_r($array1);

Demo

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use array_merge() function here.

Please note that both the parameters must be of type array.

So, first, we can declare an array variable $category_id and store the current categoryid within the array.

$array2 is the array which needs to be appended to the categoryid field.

So, we can apply merge function on these two.

$category_id[] = $array1['categoryid'];
$array1['categoryid'] = array_merge($category_id, $array2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.