重新排列多维数组中的项目

时间:2017-12-26 07:24:37

标签: php arrays multidimensional-array

我有一个多维数组,我想要获取关键数据'从每个数组项,并形成另一个数组,如第一个数组'数据'元素有3个项目,第二个有1个项目,第3个和第4个是空的,第5个有一个项目,我想创建一个单独的数组,如/account

输入数组是,

$temp_array['first_item_from_first_array,..., first_item_from_fifth_array, 'second_item_from_second_array,....,second_item_From_fifth_array]

输出就像这样

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
                            )

                    )

            )

    )

3 个答案:

答案 0 :(得分:1)

您需要执行以下操作: -

$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);

输出: - https://eval.in/925383

参考: - PHP: array_column - Manual

答案 1 :(得分:1)

您可以使用array_column()功能

示例: -

<?php

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

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

?>

答案 2 :(得分:0)

您可以使用foreach和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);

输出将是:

阵 (

[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
    )