在每个城市名称的多个数组中,我有不同的“点”。就像休斯顿有两个阵列,每个阵列都有一个不同的“点”值。我想要做的是把这两个“点”值与休斯顿或任何城市的名字相加,也许是一个新的数组。所以当我想进入“景点”时,我会得到一个城市的“景点”总数。
这是控制器内部的代码:
foreach ($request->city as $city) {
$citySpots[$city] = Controller::select('spots')
->where('city', $city)
->get()
->toArray();
}
dd($citySpots);dd值:
array:2 [▼
"Houston" => array:2 [▼
0 => array:1 [▼
"spots" => "20"
]
1 => array:1 [▼
"spots" => "10"
]
]
"New York" => array:1 [▼
0 => array:1 [▼
"spots" => "500"
]
]
]发布于 2021-05-23 00:38:13
每个城市都有参观过的景点,我建议你数一下景点,然后按城市的名称分组:
,像这样的东西
foreach ($request->city as $city) {
$citySpots[$city] = DB::table('table_name')
->select(DB::raw('sum(spots) as spots'))
->where('city', $city)
->groupBy('city')
->get()
->toArray();
}
dd($citySpots);dd应该是
array:2 [▼
"Houston" => array:1 [▼
0 => array:1 [▼
"spots" => "30"
]
]
"New York" => array:1 [▼
0 => array:1 [▼
"spots" => "500"
]
]
]https://stackoverflow.com/questions/67654958
复制相似问题