我試圖返回每個$ models ['location']中有多少個值的計數。我已經拿出下面的代碼來做到這一點,它的工作原理,問題是第一個計數返回0.如何在CakePHP中打印唯一值的計數
下面是一個輸出示例:0in7B 27in7C 40in8A 36in8B 40in8C
。現在這幾乎是正確的,但真的有27in7B 40in7C 36in8A
等等。有沒有人知道我在這裏做錯了什麼或更好的方式來做到這一點?
<?php
$locations = array();
foreach($subcategory['Product'] as $models) {
if (in_array($models['location'], $locations)) {
$i++;
continue;
}
$locations[] = $models['location'];
echo $i . "in" . $models['location'] . " ";
$i = 1;
}
?>
的var_dump($模式[ '位置'])返回:
string(2) "7B" 0in7B string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7B" string(2) "7C" 27in7C string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "7C" string(2) "8A" 40in8A string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8A" string(2) "8B" 36in8B string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8B" string(2) "8C" 40in8C string(2) "8C" string(2) "8C" string(2) "8C" string(2) "8C" string(2) "8C" string(2) "8C"
爲什麼要那樣做的'$車型的var_dump [ '位置']'而不是'$ locations'? – Dave 2013-03-08 19:28:38