drbmhd9583 2016-09-21 15:30
浏览 49
已采纳

如何按特定键对PHP数组进行排序和合并? [重复]

This question already has an answer here:

I have the following PHP array

    array (size=14)
  0 => 
    object(stdClass)[39]
      public 'department' => string 'BOOKS' (length=32)
      public 'dep_url' => string 'cheap-books' (length=32)
      public 'category' => string 'Sci-fi' (length=23)
      public 'cat_url' => string 'sci-fi' (length=23)
  1 => 
    object(stdClass)[40]
      public 'department' => string 'JEWELRY' (length=32)
      public 'dep_url' => string 'cheap-jewels' (length=32)
      public 'category' => string 'Rings' (length=23)
      public 'cat_url' => string 'rings' (length=23)
  2 => 
    object(stdClass)[41]
      public 'department' => string 'JEWELRY' (length=32)
      public 'dep_url' => string 'cheap-jewels' (length=32)
      public 'category' => string 'Earings' (length=23)
      public 'cat_url' => string 'cheap-earings' (length=23)

As you can see its an array of departments with their categories, how can i merge the array to get something like the following:

  array (size=14)
  0 => 
    object(stdClass)[39]
      public 'department' => string 'BOOKS' (length=32)
      public 'dep_url' => string 'cheap-books' (length=32)
        innerarray[0] = 
            public 'category' => string 'Sci-fi' (length=23)
            public 'cat_url' => string 'sci-fi' (length=23)
  1 => 
    object(stdClass)[40]
      public 'department' => string 'JEWELRY' (length=32)
      public 'dep_url' => string 'cheap-jewels' (length=32)
        innerarray[0] = 
                   public 'category' => string 'Rings' (length=23)
                   public 'cat_url' => string 'rings' (length=23)
        innerarray[1] = 
                  public 'category' => string 'Earings' (length=23)
                  public 'cat_url' => string 'cheap-earings' (length=23)

I want to merge the array by department with the least amount of loops.

I hope i am clear with my question, thanks for any help you can give!

</div>

展开全部

  • 写回答

1条回答 默认 最新

  • dongpaipu8394 2016-09-21 15:38
    关注

    It would be best if you had a department ID (a primary key) to use to identify duplicates, but in lieu of that you should use the department name and URL together to match them.

    Something like this should work:

    $output = [];
    foreach ($array as $entry) {
        // no department ID, so create one for indexing the array instead...
        $key = md5($entry->department . $entry->dep_url);
    
        // create a new department entry
        if (!array_key_exists($key, $output)) {
            $class = new stdClass;
            $class->department = $entry->department;
            $class->dep_url = $entry->dep_url;
            $class->categories = [];
    
            $output[$key] = $class;
        }
    
        // add the current entry's category data to the indexed department
        $category = new stdClass;
        $category->category = $entry->category;
        $category->cat_url = $entry->cat_url;
    
        $output[$key]->categories[] = $category;
    }
    

    This will give you an array of department objects which contains, each of which contains an array of category objects. It'll be indexed by a hash which you create manually in lieu of a department ID/primary key to use instead.

    To remove those keys simply do:

    $output = array_values($output);
    

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部