dtip91401 2015-10-27 07:11
浏览 44
已采纳

将Eloquent(Laravel 4.2)结果组合成基于A值的多维数组

I have this array that is returned from Laravel 4.2 Query Builder I am trying to rearrange a bit. I can't seem to figure out how to get the result I am looking for.

    array (size=8)
  0 => 
    object(stdClass)[565]
      public 'id' => int 53
      public 'vehicle_id' => int 11
      public 'date' => string '2015-10-27' (length=10)
  1 => 
    object(stdClass)[563]
      public 'id' => int 55
      public 'vehicle_id' => int 11
      public 'date' => string '2015-10-29' (length=10)
  2 => 
    object(stdClass)[567]
      public 'id' => int 61
      public 'vehicle_id' => int 7
      public 'date' => string '2015-10-30' (length=10)
  3 => 
    object(stdClass)[561]
      public 'id' => int 63
      public 'vehicle_id' => int 6
      public 'date' => string '2015-10-30' (length=10)
  4 => 
    object(stdClass)[572]
      public 'id' => int 64
      public 'vehicle_id' => int 8
      public 'date' => string '2015-10-30' (length=10)
  5 => 
    object(stdClass)[580]
      public 'id' => int 65
      public 'vehicle_id' => int 9
      public 'date' => string '2015-10-30' (length=10)
  6 => 
    object(stdClass)[579]
      public 'id' => int 66
      public 'vehicle_id' => int 10
      public 'date' => string '2015-10-30' (length=10)
  7 => 
    object(stdClass)[578]
      public 'id' => int 57
      public 'vehicle_id' => int 11
      public 'date' => string '2015-10-31' (length=10)

Example of desired result:

array(
  '2015-10-27' => array(11),
  '2015-10-30' => array( 6, 7, 8, 9, 10 ), 
  '2015-10-31' => array(11)
)

I have tried foreach loops nested in for loops etc.. been driving me nuts for a while now. Am I missing something simple here?

  • 写回答

1条回答 默认 最新

  • ds19891231 2015-10-27 07:22
    关注
    $dates = [];
    
    foreach($yourArray as $obj) {   
        $dates[$obj->date][] = $obj->vehicle_id;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)