将数组转换为另一种格式

时间:2016-12-29 06:45:58

标签: php laravel loops laravel-5 nested

以下代码是我的显示类别列表功能的返回。

我的结构:

Parent_category - >类别 - > Child_category

现在我将child_category与category结合起来,并添加到category is_bold param。

这里的方法:

public function categories(){
  $categories = self::buildTree(Category::get()->toArray());
  $cats = $categories;
  foreach($cats as $key=>$category){
    if(count($category['childs'])>0){
      foreach($category['childs'] as $key1=>$c){
        if(count($c['childs'])>0){
          $categories[$key]['childs'] = array_merge($categories[$key]['childs'], $c['childs']);
          unset($categories[$key]['childs'][$key1]['childs']);
          $categories[$key]['childs'][$key1]['is_bold']=1;
        }
      }
      $categories[$key]['childs'] = self::array_sort($categories[$key]['childs'], 'id', SORT_ASC);
    }
  }
  return response()->json(array('data' => $categories), 200);
}

这是buildTree方法:

public static function buildTree(array $elements, $parentId = null){
  $result = array();
  foreach ($elements as $element) {
      if($element['parent_id'] == $parentId) {
          $children = self::buildTree($elements, $element['id']);
          $element['childs'] = [];
          if ($children) $element['childs'] = $children;
          $result[$element['id']] = $element;
      }
  }
  return array_values($result);
}

这是我的回归

"data": [
{
  "id": 1,
  "parent_id": null,
  "name": "tv",
  "icon": null,
  "slack": null,
  "childs": [
    {
      "id": 2,
      "parent_id": 1,
      "name": "tvs",
      "icon": null,
      "slack": null,
      "is_bold": 1
    },
    {
      "id": 3,
      "parent_id": 2,
      "name": "LED tvs",
      "icon": null,
      "slack": null,
      "childs": []
    },
    {
      "id": 4,
      "parent_id": 2,
      "name": "4K tvs",
      "icon": null,
      "slack": null,
      "childs": []
    },
    {
      "id": 5,
      "parent_id": 1,
      "name": "videos",
      "icon": null,
      "slack": null,
      "is_bold": 1
    },
    {
      "id": 6,
      "parent_id": 5,
      "name": "home cinema",
      "icon": null,
      "slack": null,
      "childs": []
    },
    {
      "id": 7,
      "parent_id": 5,
      "name": "videoplayers",
      "icon": null,
      "slack": null,
      "childs": []
    }]
 }]

但现在我想要返回这样的数据:

{
  "id": 1,
  "parent_id": null,
  "name": "Телевизоры, аудио, видео",
  "icon": null,
  "slack": null,
  "childs": [
   "televizory": [ {
      "id": 2,
      "parent_id": 1,
      "name": "Телевизоры",
      "icon": null,
      "slack": null,
      "is_bold": 1
    },
    {
      "id": 3,
      "parent_id": 2,
      "name": "LED телевизоры",
      "icon": null,
      "slack": null,
   },
    {
      "id": 4,
      "parent_id": 2,
      "name": "4K Телевизоры",
      "icon": null,
      "slack": null,
   }],
  video": [  {
      "id": 5,
      "parent_id": 1,
      "name": "Домашнее видео, аудио",
      "icon": null,
      "slack": null,
      "is_bold": 1
    },
    {
      "id": 6,
      "parent_id": 5,
      "name": "Домашние кинотеатры",
      "icon": null,
      "slack": null,
    },
    {
      "id": 7,
      "parent_id": 5,
      "name": "Видеоплееры",
      "icon": null,
      "slack": null,
   },
    {
      "id": 8,
      "parent_id": 5,
      "name": "Проекторы",
      "icon": null,
      "slack": null,
    },
    {
      "id": 9,
      "parent_id": 5,
      "name": "Акустика Hi-Fi",
      "icon": null,
      "slack": null,
    },
    {
      "id": 10,
      "parent_id": 5,
      "name": "Аксессуары",
      "icon": null,
      "slack": null,
    }]

1 个答案:

答案 0 :(得分:1)

您可以使用Laravel的收藏类。

简单地替换

if ($children) $element['childs'] = $children;

if ($children)  {
  $children = new \Illuminate\Support\Collection($children);
  $element['childs'] = $children->keyBy('name')->toArray();
}

keyBy()方法会使用这些元素的selected属性值替换集合元素的键 - name