Author |
Message |
spasticdonkey
RavenNuke(tm) Development Team
Joined: Dec 02, 2006
Posts: 1693
Location: Texas, USA
|
Posted:
Sat Jul 27, 2013 11:03 pm |
|
Curious if someone would know offhand how to count a multi-dimensional array based on 2nd level value. For example, this array below:
$samplearray = array();
$samplearray["number1"] = array (
"value" => "some value",
"type" => "monkey"
);
$samplearray["number2"] = array (
"value" => "some value",
"type" => "monkey"
);
$samplearray["number3"] = array (
"value" => "some value",
"type" => "gorilla"
);
$samplearray["number4"] = array (
"value" => "some value",
"type" => "gorilla"
);
I can do:
$numMonkeys = count($samplearray);
(which equals 4)
But what I really want to know is the count that are of the "type" => "monkey" on the second level (2) |
|
|
|
|
neralex
Site Admin
Joined: Aug 22, 2007
Posts: 1774
|
Posted:
Sun Jul 28, 2013 12:58 am |
|
I have found here some examples:
[ Only registered users can see links on this board! Get registered or login! ]
[ Only registered users can see links on this board! Get registered or login! ]
maybe:
Code:$totalcount = 0;
foreach ($samplearray as $key=>$value) {
if ($samplearray[$key]['type'] == 'monkey') {
$totalcount++;
}
}
echo $totalcount;
|
|
_________________ Github: RavenNuke |
|
|
|
Palbin
Site Admin
Joined: Mar 30, 2006
Posts: 2583
Location: Pittsburgh, Pennsylvania
|
Posted:
Sun Jul 28, 2013 8:25 am |
|
As neralex has pointed out you have to iterate through the array. There are no base PHP functions that will help when dealing with multi-dimensional arrays. |
_________________ "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan. |
|
|
|
spasticdonkey
|
Posted:
Sun Jul 28, 2013 11:22 am |
|
thanks guys. was hoping there was something easy in all those array functions
I wanted the data just prior to iterating thru the array, so I might look for other options.... |
|
|
|
|
wHiTeHaT
Life Cycles Becoming CPU Cycles
Joined: Jul 18, 2004
Posts: 579
|
Posted:
Mon Jul 29, 2013 12:29 pm |
|
What exactly you try to achieve? |
|
|
|
|
montego
Site Admin
Joined: Aug 29, 2004
Posts: 9457
Location: Arizona
|
Posted:
Fri Aug 09, 2013 9:35 pm |
|
|
|
|
montego
|
Posted:
Fri Aug 09, 2013 10:03 pm |
|
spasticdonkey, I haven't really thought this through all the way, but you may be able to strip out the second column into its own array and then do a count() on it...
[ Only registered users can see links on this board! Get registered or login! ]
Like I said... haven't really though it through, but was looking for a way to utilize some additional array functions, strung together, to get what you want. These may be faster than brute force, but that might not be a need or even true... |
|
|
|
|
spasticdonkey
|
Posted:
Sat Aug 10, 2013 4:24 am |
|
Thanks for the input Montego and good idea. At this point I'm just iterating the entire array and skipping the values that are not of a certain type.
foreach ($myarray as $myname => $details) {
$mytype = $details['type'];
if ($mytype === $type) {
// do stuff
}
}
The array isn't particularly large (about 100 keys), so not sure if it's worth the extra code or not, as I was just looking for a count to exit the loop when the relevant data had all been retrieved. Although in normal use only a small portion of the array is needed. |
|
|
|
|
|