我有一个数组循环:
数组函数:
$truckTypesArray = array(
"1" => "03 m3",
"2" => "05 m3",
"3" => "08 m3",
"4" => "10 m3",
"5" => "12 m3",
"6" => "14 m3",
"7" => "20 m3",
"8" => "20 m3 Hayon",
);
function getTruckTypes($truckTypesArray, $id)
{
return $truckTypesArray[$id];
}
循环代码:
while($fetchPresentTruckTypes = mysql_fetch_array($getTruckTypeQuery))
{
$createTruckTypeArray[] = getTruckTypes($truckTypesArray, $fetchPresentTruckTypes['truckTypeId']);
}
$allTructTypes = implode(",",$createTruckTypeArray);
现在它生成这个输出很好:
20 m3, 03 m3, 20 m3, 10 m3, 20 m3,
我希望它看起来像这样:
20 m3 = 3,
03 m3 = 1,
10 m3 = 1
答案 0 :(得分:1)
使用this。它为您计算数组并返回您需要的数据。
答案 1 :(得分:1)
function createTruck($createTruckTypeArray){
$result = array();
foreach($createTruckTypeArray as $val){
$result[$val] = array_count_values($val);
}
return $result;
}
此函数返回一个这样的数组
array(
'20 m3' => 3,
'03 m3' => 1,
'10 m3' => 1
);
这会破坏你的结果。不要这样,请检查
implode(', ', array_map(function ($v, $k) { return $k . '=' . $v; }, $result, array_keys($result)));