Page 1 of 1
A question about multidimensional array?
Posted: Wed Jul 09, 2003 6:11 pm
by mudkicker
I've an array like this =>
$i is the number of users and $be is what he is. (It is either "member" or "guest")
$arr[$i][$be]
(for example : $arr[3]["member"] gives mi the info that 3rd person is a member.)
What i want to ask is...
How can i find the number of "member"s in this multidimensional array?

Posted: Wed Jul 09, 2003 6:26 pm
by McGruff
count($array) - the fn doesn't care whether the array elements are values or arrays ie returns same count regardless if you have a multi-dimensional array containing 6 sub-arrays, or a one dimensional array containing 6 values..
Actually, you could use a one dimensional array here if you can set the numerical keys explicitly to the user numbers:
array(1=>'member', 32=>'guest', 554=>'member', etc)
set the array values like this:
array[$i]
..where $i is the user number.
Posted: Thu Jul 10, 2003 2:14 am
by mudkicker
ok that's known, but...
For example there are 25 elements in this array and 12 are "member". How can i find that they are 12 elements?
not only this count() fn does work!
Posted: Thu Jul 10, 2003 3:29 am
by qartis
Code: Select all
$i=0;
foreach ($array as $user_type){
if ($user_type == "member"){
$i++;
}
}
echo "There are $i members.";
Posted: Thu Jul 10, 2003 9:52 am
by McGruff
Code: Select all
<?php
$i = 0;
foreach($arr as $subarray) {
foreach($subarray as $value) {
IF ($value == 'member') {
$i++;
}
}
}
echo $i;
?>
Posted: Thu Jul 10, 2003 10:53 am
by ik
But you don't need multidimentional array for this task
Code: Select all
$people=array(
"John"=>"member",
"Steve"=>"guest"
);
$count=0;
foreach($people as $key=>$val) {
if ($val=="member") count++;
}
Posted: Thu Jul 10, 2003 11:05 am
by McGruff
Yes.
Posted: Thu Jul 10, 2003 2:43 pm
by mudkicker
Thank you people!
