Reputation: 67
I'm trying to return a count of how many values are in each $models['location']. I've come up with the following code to do this and it sort of works, the problem is the first count returns a 0.
Here's an example of the output: 0in7B 27in7C 40in8A 36in8B 40in8C
. Now this is almost correct but there are really 27in7B 40in7C 36in8A
and so on. Does anyone know what I'm doing wrong here or a better way to do this?
<?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']) returns:
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"
Upvotes: 1
Views: 186
Reputation: 8420
EDIT: This solution is a bit different than yours but it works, I've tested it.
Please note also that the "algorithm" stays of O(N). Even though you have to do a second foreach to go over the array the second one contains a lot less than N elements and thus it stays of N type.
$locations = array();
foreach($subcategory['Product'] as $models) {
if(isset($locations[$models['location']])){
$locations[$models['location']] += 1;
} else {
$locations[$models['location']] = 1;
}
}
You will get an array with all the count. All you have to do is print it.
What it does may look weird but is really simple. You go over the array and check whether an entry already exists. If it does you increment the value at the position, else you initialize the value at 1.
The array will have the location string as key.
You can also do it as a short if :
$locations = array();
foreach($subcategory['Product'] as $models) {
isset($locations[$models['location']]) ? $locations[$models['location']] += 1 : $locations[$models['location']] = 1;
}
Example of echo :
foreach($locations as $key => $count) {
echo "$count in $key <br />";
}
Upvotes: 1
Reputation: 536
It probably happens because i
is undefined at the moment you start iterating, try putting $i = 0;
after the foreach
start.
Upvotes: 0