1

我试图返回每个 $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" 
4

2 回答 2

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 />";
 }
于 2013-03-08T19:13:25.673 回答
0

它可能会发生,因为i在您开始迭代时未定义,请尝试在开始$i = 0;之后放置foreach

于 2013-03-08T18:50:11.357 回答