我正在尝试返回每个$ 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"
答案 0 :(得分:1)
编辑:此解决方案与您的解决方案略有不同但是有效,我已对其进行了测试。
请注意“算法”保持为O(N)。即使你必须做第二次foreach来遍历数组,第二个包含少于N个元素,因此它保持N型。
$locations = array();
foreach($subcategory['Product'] as $models) {
if(isset($locations[$models['location']])){
$locations[$models['location']] += 1;
} else {
$locations[$models['location']] = 1;
}
}
你将获得一个包含所有计数的数组。您所要做的就是打印它。
它的功能可能看起来很奇怪但很简单。您查看数组并检查条目是否已存在。如果是,则增加该位置的值,否则将值初始化为1。
数组将位置字符串作为键。
您也可以将其作为短片:
$locations = array();
foreach($subcategory['Product'] as $models) {
isset($locations[$models['location']]) ? $locations[$models['location']] += 1 : $locations[$models['location']] = 1;
}
回声示例:
foreach($locations as $key => $count) {
echo "$count in $key <br />";
}
答案 1 :(得分:0)
可能会发生这种情况,因为在您开始迭代时i
未定义,请在$i = 0;
开始后尝试foreach
。