Search code examples
phparraysarray-unique

How to remove the parent numeric keys of php array


I've an array in php something like below

Array
(
    [0] => Array
        (
            [0] => 40173
            [1] => 514081
            [2] => 363885
            [3] => 891382
        ),
    [1] => Array
        (
            [0] => 40173
            [1] => 5181
            [2] => 385
            [3] => 891382
        )

)

Now I want to remove the parents indexes 0,1... and finally want to get all the values (only unique values).

Thanks.


Solution

  • One possible approach is using call_user_func_array('array_merge', $arr) idiom to flatten an array, then extracting unique values with array_unique():

    $new_arr = array_unique(
      call_user_func_array('array_merge', $old_arr));
    

    Demo. Obviously, it'll work with array of any length.