Jump to content

Counting number of a specified value in an multi array.


saeed_violinist

Recommended Posts

Hi,

 

I have a multi Array like thi :

 


Array
(
    [0] => Array
        (
            [fname] => Academic
            [ename] => Academic
            [id] => academic
            [image] => academic.gif
            [article] => 2
            [show] => Y
            [sort] => 1
            [url] => http://127.0.0.1/category/academic.html
        )

    [1] => Array
        (
            [fname] => CPU
            [ename] => CPU
            [id] => cpu-1
            [image] => cpu.gif
            [article] => 5
            [show] => Y
            [sort] => 2
            [url] => http://127.0.0.1/category/cpu-1.html
        )

    [2] => Array
        (
            [fname] => Network
            [ename] => Network
            [id] => network
            [image] => network.gif
            [article] => 0
            [show] => Y
            [sort] => 3
            [url] => http://127.0.0.1/category/network.html
        )

    [3] => Array
        (
            [fname] => Others
            [ename] => Others
            [id] => others-1
            [image] => others.gif
            [article] => 7
            [show] => N
            [sort] => 4
            [url] => http://127.0.0.1/category/others-1.html
        )

    [4] => Array
        (
            [fname] => laptop
            [ename] => Laptop
            [id] => laptop
            [image] => mobile.gif
            [article] => 0
            [show] => N
            [sort] => 5
            [url] => http://127.0.0.1/category/laptop.html
        )
)

 

Im trying to count number of keys that have Y sa value. As you can see above, in this array I have two [show] keys with values of 'N' , but since this is a dynamic array, I need to count number of keys with N as their value.

 

I tried a lot on this but no success. please help me friends.

Thanks for your reply,

 

But I cant figure out your answer! I tried your code and I recived 0 fo $count  :-[ , there is actually 2 N in the sub arrays, but your script echos $total 14, that is  number of keys.

  $blah = $hidden;// your array here .
  $total = count($blah);
  $counter = 0;
 for($x=0;$x<$total;$x++){
	if($blah[$x]["show"] == "N"){
		$counter++;
	}
}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.