简体   繁体   中英

Rearrange items in multidimensional array

I have a multidimensional array, I want to take the key 'data' from each array item and form another array, like first array 'data' element has 3 items, second has 1 item, 3rd and 4th are empty, and 5th has one item, I want to make a separate array like $temp_array['first_item_from_first_array,..., first_item_from_fifth_array, 'second_item_from_second_array,....,second_item_From_fifth_array]

input array is,

Array

    (
        [0] => Array
            (
                [meal_type] => bf
                [label] => Breakfast
                [calorie_limit] => 30
                [total_calorie] => 0
                [data] => Array
                    (
                        [0] => Array
                            (
                                [id] => 109
                                [label] => testfrom
                                [quantity] => 12
                                [unit] => g
                            )

                        [1] => Array
                            (
                                [id] => 118
                                [label] => test
                                [quantity] => 200
                                [unit] => oz
                            )

                        [2] => Array
                            (
                                [id] => 121
                                [label] => test
                                [quantity] => 10
                                [unit] => g
                            )

                    )

            )

        [1] => Array
            (
                [meal_type] => sn
                [label] => Snacks
                [calorie_limit] => 10
                [total_calorie] => 0
                [data] => Array
                    (
                        [0] => Array
                            (
                                [id] => 120
                                [label] => testfrom
                                [quantity] => 12
                                [unit] => g
                            )

                    )

            )

        [2] => Array
            (
                [meal_type] => lu
                [label] => Lunch
                [calorie_limit] => 20
                [total_calorie] => 0
                [data] => Array
                    (
                    )

            )

        [3] => Array
            (
                [meal_type] => su
                [label] => Supper
                [calorie_limit] => 30
                [total_calorie] => 0
                [data] => Array
                    (
                    )

            )

        [4] => Array
            (
                [meal_type] => dn
                [label] => Dinner
                [calorie_limit] => 20
                [total_calorie] => 0
                [data] => Array
                    (
                        [0] => Array
                            (
                                [id] => 119
                                [label] => test
                                [quantity] => 200
                                [unit] => oz
                            )

                    )

            )

    )

the output to be like this

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 109
                    [label] => testfrom
                    [quantity] => 12
                    [unit] => g
                )

            [1] => Array
                (
                    [id] => 120
                    [label] => testfrom
                    [quantity] => 12
                    [unit] => g
                )

            [2] => Array
                (
                )

            [3] => Array
                (
                )

            [4] => Array
                (
                    [id] => 119
                    [label] => test
                    [quantity] => 200
                    [unit] => oz
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 118
                    [label] => test
                    [quantity] => 200
                    [unit] => oz
                )

            [1] => Array
                (
                )

            [2] => Array
                (
                )

            [3] => Array
                (
                )

            [4] => Array
                (
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [id] => 121
                    [label] => test
                    [quantity] => 10
                    [unit] => g
                )

            [1] => Array
                (
                )

            [2] => Array
                (
                )

            [3] => Array
                (
                )

            [4] => Array
                (
                )

        )

)

You need to do it like below:-

$data_array= array_column($array,'data'); 

$count = 0;

foreach($data_array as $data){
  $real_count = count($data);
  if($real_count > $count){
      $count = $real_count;
  }
}

echo $count;
$final_array = [];

foreach($data_array as $data_arr){
  for($i=0;$i< $count; $i++){
     $final_array[$i][] = (count($data_arr[$i])>0)? $data_arr[$i]: array();
  }
}

echo "<pre/>";print_r($final_array);

Output:- https://eval.in/925383

Reference:- PHP: array_column - Manual

You can use array_column() function

Example :-

<?php

$array = [
    [
        "meal_type" => "bf",
        "data" => [
            "name" => "test",
            "email" => "test@ymail.com"
        ]
    ],
    [
        "meal_type" => "bf",
        "data" => []
    ]
];

echo "<pre>";
print_r(array_column($array, "data"));

?>

You can do this using foreach and array_push.

$data_item_array = [];

foreach ($array as $item) {
   $data = $item['data'];
   foreach ($data as $t) {
       array_push($data_item_array, $t);
   }
}

print_r($data_item_array);

Output will be :

Array (

[0] => Array
    (
        [id] => 109
        [label] => testfrom
        [quantity] => 12
        [unit] => g
    )

[1] => Array
    (
        [id] => 118
        [label] => test
        [quantity] => 200
        [unit] => oz
    )

[2] => Array
    (
        [id] => 121
        [label] => test
        [quantity] => 10
        [unit] => g
    )

[3] => Array
    (
        [id] => 120
        [label] => testfrom
        [quantity] => 12
        [unit] => g
    )

[4] => Array
    (
        [id] => 119
        [label] => test
        [quantity] => 200
        [unit] => oz
    )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM