dongshuo8756 2016-12-29 06:45
浏览 40
已采纳

将数组转换为另一种格式

The following code is a return of my function for display categories list.

My structure:

Parent_category -> Category -> Child_category

Now i combine child_category with category and add to category is_bold param.

Here's method:

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

Here's buildTree method:

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

Here's my return

"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": []
    }]
 }]

But now I want to return data like this:

{
  "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条回答 默认 最新

  • dqpwdai095465 2016-12-29 07:01
    关注

    You could make use of Laravel's Collection class.

    Simply replace

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

    with

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

    The keyBy() method replaces the keys of collection's elements with the value of selected attribute of those elements - name in your case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题