skip to Main Content

I have data stored in a PHP array like this:

(
    [category1] => Array
        (
            [0] => Array
                (
                    [name] => Test1
                )

            [1] => Array
                (
                    [name] => Test2
                )

            [2] => Array
                (
                    [name] => Test3
                )

        )

    [category2] => Array
        (
            [0] => Array
                (
                    [name] => Test4
                )

        )

    [category3] => Array
        (
            [0] => Array
                (
                    [name] => Test5
                )

        )

)

What functions can I use to modify this array to remove the first array keys of category1/2/3, and then reorder the numeric array keys, so the resulting array looks like this:

Array
(

            [0] => Array
                (
                    [name] => Test1
                )

            [1] => Array
                (
                    [name] => Test2
                )

            [2] => Array
                (
                    [name] => Test3
                )

            [3] => Array
                (
                    [name] => Test4
                )


            [4] => Array
                (
                    [name] => Test5
                )

)

I have tried array_shift, but this just returns 3 pieces of data, and doesn’t take into account the other categories:

        [0] => Array
            (
                [name] => Test1
            )

        [1] => Array
            (
                [name] => Test2
            )

        [2] => Array
            (
                [name] => Test3
            )

3

Answers


  1. Try array_merge() function and array_values() like this :

    $data = [
        "category1" => [
            [
                "name" => "Test1"
            ],
            [
                "name" => "Test2"
            ],
            [
                "name" => "Test3"
            ]
        ],
        "category2" => [
            [
                "name" => "Test4"
            ]
        ],
        "category3" => [
            [
                "name" => "Test5"
            ]
        ]
    ];
    
    $mergedArray = array_merge(...array_values($data));
    
    
    Login or Signup to reply.
  2. first loop through your array and create a new array like this

    $newArray = array();
    foreach ($yourArray as $item) {
        $newArray = array_merge($newArray, $item);
    }
    

    you can also use array_reduce instead of the above loop.

    $newArray = array_reduce($yourArray, function($carry, $item) {
        return array_merge($carry, $item);
    }, array());
    

    and then reorder your new array

    $newArray = array_values($newArray);
    
    Login or Signup to reply.
  3. The input array only contains leaf elements which are arrays with one key-value pair, and where the keys of each of these elements are identical (which means these keys are not needed in the result array).

    If that really is the input this approach would work too (otherwise David Teo’s answer is the way to go):

    $result = [];
    
    array_walk_recursive(
      $data,
      function ($item) use (&$result) { return $result[] = $item; }
    );
    
    print_r($result);
    

    Output:

    Array
    (
        [0] => Test1
        [1] => Test2
        [2] => Test3
        [3] => Test4
        [4] => Test5
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search