dshw124502
2017-10-13 06:56
浏览 12
已采纳

在php中添加数组的值

Hi i know its very simple but im stuck in this. Im fetching data by using join from database.. Now i got the values in array. i want to add these two value in a variable. Code is below..

$sql = "SELECT event_details.max_team_size FROM booking_details INNER JOIN event_details on booking_details.subcategory_id=event_details.id WHERE booking_details.`booking_id` = ".$booking_id." ";

        $command = Yii::$app->db->createCommand($sql);
        $array = $command->queryAll();

$array has got the values like this..

Array
(
[0] => Array
    (
        [max_team_size] => 6
    )

[1] => Array
    (
        [max_team_size] => 8
    )
 )

I want to add these two max_team_size into a single variable and use that later to compare.

图片转代码服务由CSDN问答提供 功能建议

我知道它非常简单但我坚持这个。 我通过使用数据库中的连接来获取数据..现在 我得到了数组中的值。 我想在变量中添加这两个值。 节点在下面..

  $ sql =“SELECT event_details.max_team_size FROM booking_details INNER JOIN event_details on booking_details.subcategory_id = event_details  .id WHERE booking_details .booking_id` =“。$ booking_id。”“; 
 
 $ command = Yii :: $ app-> db-> createCommand($ sql); 
 $ array = $ command-  > queryAll(); 
   
 
 

$ array 有这样的值..

  Array 
(
 [0] =>数组
(
 [
 [max_team_size] => 6 
)
 
 [1] =>数组
(
 [
 
 [max_team_size]  ] => 8 
)
)
   
 
 

我想将这两个 max_team_size 添加到单个变量中,稍后再使用 比较。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • donglv9116 2017-10-13 07:02
    已采纳
    $sum = 0;
    foreach($array as $data){
        $sum  += $data->max_team_size;
    }
    echo $sum;
    
    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题