drkrsx3135168 2016-01-06 09:02
浏览 35
已采纳

阵列排列

Currently my array looks like:

array (size=3)
  0 => 
    object(stdClass)[32]
      public 'id' => string '11' (length=2)
      public 'housetype_id' => string '2' (length=1)
      public 'name' => string 'Test' (length=6)
      public 'excerpt' => string '' (length=0)
      public 'info_block_list_id' => string '1' (length=1)
  1 => 
    object(stdClass)[34]
      public 'id' => string '11' (length=2)
      public 'housetype_id' => string '2' (length=1)
      public 'name' => string 'Test' (length=6)
      public 'excerpt' => string '' (length=0)
      public 'info_block_list_id' => string '2' (length=1)
  2 => 
    object(stdClass)[35]
      public 'id' => string '11' (length=2)
      public 'housetype_id' => string '2' (length=1)
      public 'name' => string 'Test' (length=6)
      public 'excerpt' => string '' (length=0)
      public 'info_block_list_id' => string '3' (length=1)

Since only info_block_list_id changes I want to rearrange my array to look like so:

object(stdClass)[35]
  public 'id' => string '11' (length=2)
  public 'housetype_id' => string '2' (length=1)
  public 'name' => string 'Test' (length=6)
  public 'excerpt' => string '' (length=0)
  public 'info_block_list_id' => 
    array (size=3)
       0 => string '1' (length=1)
       1 => string '2' (length=1)
       2 => string '3' (length=1)
  • 写回答

2条回答 默认 最新

  • douxu4610 2016-01-06 09:12
    关注

    You want to create a variable that holds a hash/key of the unique id and changes to the info_block_list_id property to an array:

    $items =  [];
    foreach ($data as $item) {
        if (!isset($items[ $data->id ])) {
            $items[ $data->id ] = $item;
            $items[ $data->id ]->info_block_list_id = [];
        }
        $items[ $data->id ]->info_block_list_id[] = $item->info_block_list_id;
    }
    
    var_dump($items);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)