ShutyA1488
@ShutyA1488

Отсортировать многомерный массив PHP?

Из Базы Данных приходит массив в формате:
array(2) {
  ["BMW"]=>
  array(8) {
    [0]=>
    array(1) {
      ["data"]=>
      string(303) "{"cells":[{"data":"7 Series","hidden":false,"type":"text","meta":[]},{"data":"2010","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D 750i","hidden":false,"type":"text","meta":[]},{"data":"12 447","hidden":false,"type":"text","meta":[]},{"data":"20 282","hidden":false,"type":"text","meta":[]}]}"
    }
    [1]=>
    array(1) {
      ["data"]=>
      string(309) "{"cells":[{"data":"7 Series","hidden":false,"type":"text","meta":[]},{"data":"2010","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D 750Lxi AWD","hidden":false,"type":"text","meta":[]},{"data":"14 163","hidden":false,"type":"text","meta":[]},{"data":"22 787","hidden":false,"type":"text","meta":[]}]}"
    }
    [2]=>
    array(1) {
      ["data"]=>
      string(303) "{"cells":[{"data":"7 Series","hidden":false,"type":"text","meta":[]},{"data":"2018","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D 750i","hidden":false,"type":"text","meta":[]},{"data":"46 993","hidden":false,"type":"text","meta":[]},{"data":"52 361","hidden":false,"type":"text","meta":[]}]}"
    }
    [3]=>
    array(1) {
      ["data"]=>
      string(315) "{"cells":[{"data":"7 Series","hidden":false,"type":"text","meta":[]},{"data":"2018","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D 740xe AWD hybrid","hidden":false,"type":"text","meta":[]},{"data":"48 611","hidden":false,"type":"text","meta":[]},{"data":"59 078","hidden":false,"type":"text","meta":[]}]}"
    }
    [4]=>
    array(1) {
      ["data"]=>
      string(297) "{"cells":[{"data":"M5","hidden":false,"type":"text","meta":[]},{"data":"2012","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D 740i","hidden":false,"type":"text","meta":[]},{"data":"11 922","hidden":false,"type":"text","meta":[]},{"data":"22 787","hidden":false,"type":"text","meta":[]}]}"
    }
    [5]=>
    array(1) {
      ["data"]=>
      string(298) "{"cells":[{"data":"M5","hidden":false,"type":"text","meta":[]},{"data":"2012","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D 760Li","hidden":false,"type":"text","meta":[]},{"data":"23 844","hidden":false,"type":"text","meta":[]},{"data":"51 012","hidden":false,"type":"text","meta":[]}]}"
    }
    [6]=>
    array(1) {
      ["data"]=>
      string(304) "{"cells":[{"data":"M5","hidden":false,"type":"text","meta":[]},{"data":"2018","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D M760xi AWD","hidden":false,"type":"text","meta":[]},{"data":"94 018","hidden":false,"type":"text","meta":[]},{"data":"139 273","hidden":false,"type":"text","meta":[]}]}"
    }
    [7]=>
    array(1) {
      ["data"]=>
      string(266) "{"cells":[{"data":"","hidden":false,"type":"text","meta":[]},{"data":"","hidden":false,"type":"text","meta":[]},{"data":"","hidden":false,"type":"text","meta":[]},{"data":"","hidden":false,"type":"text","meta":[]},{"data":"","hidden":false,"type":"text","meta":[]}]}"
    }
  }
  ["Ford"]=>
  array(12) {
    [0]=>
    array(1) {
      ["data"]=>
      string(306) "{"cells":[{"data":"Crown Victoria","hidden":false,"type":"text","meta":[]},{"data":"2009","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D S","hidden":false,"type":"text","meta":[]},{"data":"11 065","hidden":false,"type":"text","meta":[]},{"data":"11 065","hidden":false,"type":"text","meta":[]}]}"
    }
    [1]=>
    array(1) {
      ["data"]=>
      string(315) "{"cells":[{"data":"Crown Victoria","hidden":false,"type":"text","meta":[]},{"data":"2009","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D S Extended","hidden":false,"type":"text","meta":[]},{"data":"11 556","hidden":false,"type":"text","meta":[]},{"data":"11 556","hidden":false,"type":"text","meta":[]}]}"
    }
    [2]=>
    array(1) {
      ["data"]=>
      string(307) "{"cells":[{"data":"Crown Victoria","hidden":false,"type":"text","meta":[]},{"data":"2009","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D LX","hidden":false,"type":"text","meta":[]},{"data":"12 071","hidden":false,"type":"text","meta":[]},{"data":"14 409","hidden":false,"type":"text","meta":[]}]}"
    }
    [3]=>
    array(1) {
      ["data"]=>
      string(306) "{"cells":[{"data":"Crown Victoria","hidden":false,"type":"text","meta":[]},{"data":"2010","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D S","hidden":false,"type":"text","meta":[]},{"data":"10 329","hidden":false,"type":"text","meta":[]},{"data":"12 074","hidden":false,"type":"text","meta":[]}]}"
    }
    [4]=>
    array(1) {
      ["data"]=>
      string(315) "{"cells":[{"data":"Crown Victoria","hidden":false,"type":"text","meta":[]},{"data":"2010","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D S Extended","hidden":false,"type":"text","meta":[]},{"data":"10 844","hidden":false,"type":"text","meta":[]},{"data":"13 598","hidden":false,"type":"text","meta":[]}]}"
    }
    [5]=>
    array(1) {
      ["data"]=>
      string(307) "{"cells":[{"data":"Crown Victoria","hidden":false,"type":"text","meta":[]},{"data":"2010","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D LX","hidden":false,"type":"text","meta":[]},{"data":"11 335","hidden":false,"type":"text","meta":[]},{"data":"14 397","hidden":false,"type":"text","meta":[]}]}"
    }
    [6]=>
    array(1) {
      ["data"]=>
      string(305) "{"cells":[{"data":"Crown Victoria","hidden":false,"type":"text","meta":[]},{"data":"2011","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D S","hidden":false,"type":"text","meta":[]},{"data":"9 812","hidden":false,"type":"text","meta":[]},{"data":"12 860","hidden":false,"type":"text","meta":[]}]}"
    }
    [7]=>
    array(1) {
      ["data"]=>
      string(315) "{"cells":[{"data":"Crown Victoria","hidden":false,"type":"text","meta":[]},{"data":"2011","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D S Extended","hidden":false,"type":"text","meta":[]},{"data":"10 302","hidden":false,"type":"text","meta":[]},{"data":"13 985","hidden":false,"type":"text","meta":[]}]}"
    }
    [8]=>
    array(1) {
      ["data"]=>
      string(307) "{"cells":[{"data":"Crown Victoria","hidden":false,"type":"text","meta":[]},{"data":"2011","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D LX","hidden":false,"type":"text","meta":[]},{"data":"10 817","hidden":false,"type":"text","meta":[]},{"data":"14 621","hidden":false,"type":"text","meta":[]}]}"
    }
    [9]=>
    array(1) {
      ["data"]=>
      string(299) "{"cells":[{"data":"Focus","hidden":false,"type":"text","meta":[]},{"data":"2017","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D S I4","hidden":false,"type":"text","meta":[]},{"data":"7 677","hidden":false,"type":"text","meta":[]},{"data":"12 171","hidden":false,"type":"text","meta":[]}]}"
    }
    [10]=>
    array(1) {
      ["data"]=>
      string(299) "{"cells":[{"data":"Focus","hidden":false,"type":"text","meta":[]},{"data":"2017","hidden":false,"type":"text","meta":[]},{"data":"Sedan 4D SEL","hidden":false,"type":"text","meta":[]},{"data":"11 974","hidden":false,"type":"text","meta":[]},{"data":"16 225","hidden":false,"type":"text","meta":[]}]}"
    }
    [11]=>
    array(1) {
      ["data"]=>
      string(315) "{"cells":[{"data":"Focus","hidden":false,"type":"text","meta":[]},{"data":"2017","hidden":false,"type":"text","meta":[]},{"data":"Hatchback 5D RS AWD I4 Turbo","hidden":false,"type":"text","meta":[]},{"data":"25 198","hidden":false,"type":"text","meta":[]},{"data":"43 850","hidden":false,"type":"text","meta":[]}]}"
    }
  }
}


Нужно отсортировать его в таком формате:
$example = [
    "BMW" => [
        "M5" => [
            0 => [
                "Year" => "2018",
                "Marka" => "Sedan 4D M760xi AWD",
                "Bottom" => "94 018",
                "Top" => "139 273"
            ],
            1 => [
                "Year" => "2019",
                "Marka" => "Sedan 4D M760xi",
                "Bottom" => "10 018",
                "Top" => "20 273"
            ]
        ],
        "7 Series" => [
            0 => [
                "Year" => "2010",
                "Marka" => "Sedan 4D 750i",
                "Bottom" => "12 447",
                "Top" => "20 282"
            ],
            1 => [
                "Year" => "2018",
                "Marka" => "Sedan 4D 750i",
                "Bottom" => "46 993",
                "Top" => "52 361"
            ]
        ],

    ]
];
  • Вопрос задан
  • 104 просмотра
Пригласить эксперта
Ответы на вопрос 2
0xD34F
@0xD34F
$grouped = [];

foreach ($arr as $brand => $items) {
  foreach ($items as $item) {
    $data = array_column(json_decode($item['data'], true)['cells'], 'data');
    $model = array_shift($data);

    if ($model) {
      $grouped[$brand][$model][] = array_combine(
        [ 'Year', 'Model', 'Bottom', 'Top' ],
        $data
      );
    }
  }
}
Ответ написан
@the5x
Я думаю вам надо сортировать, как-то в эту сторону

usort($array, function($a, $b) {
    return $a['order'] <=> $b['order'];
});
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы