2013-03-08 62 views
1

我試圖返回每個$ 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" 
+0

爲什麼要那樣做的'$車型的var_dump [ '位置']'而不是'$ locations'? – Dave 2013-03-08 19:28:38

回答

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 />"; 
} 
+0

我起初嘗試過,但後來它只是給第一個計數1 – rubyme8 2013-03-08 19:15:52

+0

@ ruby​​me8更新。這與你的解決方案有點不同,但即使數組一開始沒有排序也可以工作。 – 2013-03-08 19:35:43

+0

非常感謝!這正如我所需要的那樣工作!我將不得不研究你的答案一段時間,以更好地瞭解這是如何工作的。謝謝! – rubyme8 2013-03-08 19:52:22

0

這可能是因爲i在你開始迭代,嘗試把$i = 0;foreach開始後的那一刻不確定。

+0

剛剛嘗試過,然後我得到了0的所有計數... – rubyme8 2013-03-08 18:54:39

+0

當然,這是一個愚蠢的建議。 var_dump($ models ['location'])''在'foreach'開始看到其中的內容後,可能是'$ locations'中的第一個$ models ['location']'不在'$ locations'中,因此它的打印值爲0. – tonyjmnz 2013-03-08 18:58:36

+0

I只是用var_dump的輸出更新了我的問題,有什麼想法呢? – rubyme8 2013-03-08 19:04:38