我试图返回每个 $models['location'] 中有多少值的计数。我想出了下面的代码来做到这一点,它有点工作,问题是第一个计数返回 0。
下面是一个输出示例: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($models['location']) 返回:
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"