Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
spasticdonkey
RavenNuke(tm) Development Team



Joined: Dec 02, 2006
Posts: 1693
Location: Texas, USA

PostPosted: Sat Jul 27, 2013 11:03 pm Reply with quote

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)
 
View user's profile Send private message Visit poster's website
neralex
Site Admin



Joined: Aug 22, 2007
Posts: 1774

PostPosted: Sun Jul 28, 2013 12:58 am Reply with quote

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 
View user's profile Send private message
Palbin
Site Admin



Joined: Mar 30, 2006
Posts: 2583
Location: Pittsburgh, Pennsylvania

PostPosted: Sun Jul 28, 2013 8:25 am Reply with quote

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. 
View user's profile Send private message
spasticdonkey







PostPosted: Sun Jul 28, 2013 11:22 am Reply with quote

thanks guys. was hoping there was something easy in all those array functions Rolling Eyes

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

PostPosted: Mon Jul 29, 2013 12:29 pm Reply with quote

What exactly you try to achieve?
 
View user's profile Send private message Send e-mail
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Fri Aug 09, 2013 9:35 pm Reply with quote

wHiTeHaT, please see his original post... at the bottom... Wink

_________________
Where Do YOU Stand?
HTML Newsletter::ShortLinks::Mailer::Downloads and more... 
View user's profile Send private message Visit poster's website
montego







PostPosted: Fri Aug 09, 2013 10:03 pm Reply with quote

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







PostPosted: Sat Aug 10, 2013 4:24 am Reply with quote

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.
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©