在php中将数据插入到多维数组中

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 )
$array1 = array ( 'productcode' => 218133, 'categoryid' => 315, 'color' => 'red' );
$array2 = array (10,220);

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

Demo

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);