php merge arrays

前端 未结 6 1667
北海茫月
北海茫月 2021-01-21 08:33

I have been trying (unsuccessfully) to merge the output of multiple arrays into a single array. An example of what I tried was:

$data1 = array(\"cat\", \"goat\")         


        
6条回答
  •  花落未央
    2021-01-21 09:11

    Your example that works is completely different from your non working code. You are not even using array_merge in it.

    If you are only accessing scalar elements, the following will work, but does not use array_merge either:

    $items = array();
    foreach ($lines as $inner) {
        $items[] = $inner[1];
    }    
    $items = array_unique($items);
    
    echo "
    ";
    print_r($items);
    echo "
    ";

    If you are interested in all of $inner, than you would use array_merge:

    $items = array();
    foreach ($lines as $inner) {
        $items = array_merge($items, $inner);
    }
    

提交回复
热议问题