【问题标题】:group update a array in laravel分组更新 laravel 中的数组
【发布时间】:2020-08-19 20:08:57
【问题描述】:

我有一个数组 $cart 结果像

      array:5 [▼
  188 => array:17 [▼
    "user_id" => ""
    "product_id" => "188"
    "qty" => "2"
    "date" => "03-05-2020"
    "product_type" => "rear type"
    "custom_color_title" => ""
    "custom_color_price" => ""
    "bolt_title" => ""
    "bolt_price" => ""
    "hub_center_rings_title" => ""
    "hub_center_rings_price" => ""
    "wheel_spacers_title" => ""
    "wheel_spacers_price" => ""
    "tire_pressure_title" => ""
    "tire_pressure_price" => ""
    "product_price" => 1890
    "product_size" => ""
  ]
  176 => array:17 [▼
    "user_id" => ""
    "product_id" => "176"
    "qty" => "2"
    "date" => "03-05-2020"
    "product_type" => "wheel type"
    "custom_color_title" => ""
    "custom_color_price" => ""
    "bolt_title" => ""
    "bolt_price" => ""
    "hub_center_rings_title" => ""
    "hub_center_rings_price" => ""
    "wheel_spacers_title" => ""
    "wheel_spacers_price" => ""
    "tire_pressure_title" => ""
    "tire_pressure_price" => ""
    "product_price" => 1680
    "product_size" => ""
  ]
  224 => array:17 [▶]
] 

如何从这个数组更新 user_id 值并插入数据库?,数组 id 产品值的索引值所以我很困惑如何更新这个数组。

【问题讨论】:

  • 您想用数组的索引更新user_id 的值吗?例如:如果索引是 176 那么 user_id:"176"?
  • @AlwaysSunny 没有从会话值获取的用户 ID
  • 所以假设会话中的用户 ID 为 100,那么数组中的所有 user_id 值将是 100 对吗?
  • @AlwaysSunny 是的
  • 您是否尝试过编写一些代码?您要问的似乎可以通过遍历数组的内容来完成。

标签: php arrays laravel


【解决方案1】:

您可以使用简单的foreach() 循环来设置会话中的user_id 值,

foreach($result as $key=>$value){
   $result[$key]['user_id'] = $_SESSION['user_id']; 
}

print_r($result);

【讨论】:

  • 数组 ( [188] => 数组 ( [user_id] => 272 ) [176] => 数组 ( [user_id] => 272 ) [224] => 数组 ( [user_id] => 272)),答案只显示这个不显示完整结果
  • @andrewghosh 再试一次
  • 旧答案有效,我错了!!! ,我打印错误的变量
猜你喜欢
  • 1970-01-01
  • 2019-02-23
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-16
  • 2021-11-04
相关资源
最近更新 更多