Can't concatenate 2 arrays in PHP

后端 未结 10 1117
终归单人心
终归单人心 2020-11-28 12:49

I\'ve recently learned how to join 2 arrays using the + operator in PHP.

But consider this code...

$array = array(\'Item 1\');

$array += array(\'Ite         


        
10条回答
  •  执念已碎
    2020-11-28 13:04

    Try array_merge.

    $array1 = array('Item 1');
    
    $array2 = array('Item 2');
    
    $array3 = array_merge($array1, $array2);
    

    I think its because you are not assigning a key to either, so they both have key of 0, and the + does not re-index, so its trying to over write it.

提交回复
热议问题