首先按根类别对类别排序

时间:2016-07-03 12:19:01

标签: php

我想对一系列给定的类别进行排序。根类别必须首先出现在新数组中的子类别之前。

示例数据:

$data = [
  ['id' => '1', 'name' => 'abc', 'parent_id' => '0' ],
  ['id' => '4', 'name' => 'def', 'parent_id' => '5' ],
  ['id' => '3', 'name' => 'ghj', 'parent_id' => '2' ],
  ['id' => '5', 'name' => 'zst', 'parent_id' => '1' ],
  ['id' => '2', 'name' => 'klm', 'parent_id' => '1' ],
];

输出我想实现:

$output = [
  '1' => ['id' => '1', 'name' => 'abc', 'parent_id' => '0' ],
  '5' => ['id' => '5', 'name' => 'zst', 'parent_id' => '1' ],
  '4' => ['id' => '4', 'name' => 'def', 'parent_id' => '5' ],
  '2' => ['id' => '2', 'name' => 'klm', 'parent_id' => '1' ],
  '3' => ['id' => '3', 'name' => 'ghj', 'parent_id' => '2' ],
];

我的代码到现在为止:

function sortCategories($data) {
  $output = [];
  foreach ($data as $category) {
    if ($output[$category['id']] || $output[$category['parent_id']])
      continue;

    if (!$output[$category['parent_id']])
      $output = getParentCategories($category, $ouput, $data);

    $output[$category['id']] = [
      'id' => $category['id'],
      'name' => $category['name'],
      'parent_id' => $category['parent_id']
    ];
  }
  return $output;
}

function getParentCategories($category, $output, $data) {
  if ($output[$category['parent_id']]) {
    return $output;
  } else {
  }
}

我遇到的问题是我不知道如何实现getParentCategories(),以便此函数以正确的方式处理根类别。

有任何建议如何解决问题?

2 个答案:

答案 0 :(得分:1)

从php 7开始,我们可以像这样使用太空船运算符

usort($output, function ($a,$b) {
   return $a['parent_id'].$a['id'] <=> $b['parent_id'].$b['id'];
});

这两个ID都会连接在一起,以便对idparent_id进行排序,parent_id的优先级高于id

在实际案例中,id的范围可能大于&gt; 0..9,你想要5&lt;如上所述,在比较字符串时不是这种情况。也许你可以发布一个考虑到这一点的代码并让我们审查它。也许首先要了解'1' => ['id' => '1']1 => ['id' => 1]之间的区别。

答案 1 :(得分:1)

使用foreach()的解决方案适合您: -

<?php 
$data = [
  ['id' => '1', 'name' => 'abc', 'parent_id' => '0' ],
  ['id' => '4', 'name' => 'def', 'parent_id' => '5' ],
  ['id' => '3', 'name' => 'ghj', 'parent_id' => '2' ],
  ['id' => '5', 'name' => 'zst', 'parent_id' => '1' ],
  ['id' => '2', 'name' => 'klm', 'parent_id' => '1' ],
]; // original array

$new_array = array(); // new empty array

foreach($data as $key=>$value){ // iterate through original array
    if($value['parent_id'] == 0){ // check root value
        $new_array[$value['id']] = $value; // assign full sub-array to newly created array
    }else{ // if not root value
        $get_key = searchForId($value['parent_id'],$data); // based on parent-id get the parent array key from original array
            $new_array[$data[$get_key]['id']] = $data[$get_key]; // based on key assign parent array to the newly created array
            $new_array[$value['id']] = $value; // assign the child array to newly created array
    }

}
function searchForId($id, $array) { // send parent_id and original array
   foreach ($array as $key => $val) {
       if ($val['id'] === $id) {
           return $key; // return key of parent id
       }
   }
   return null;
}
echo "<pre/>";print_r($new_array); // print new array
?>

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