saeed_violinist Posted April 8, 2007 Share Posted April 8, 2007 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. Link to comment https://forums.phpfreaks.com/topic/46187-counting-number-of-a-specified-value-in-an-multi-array/ Share on other sites More sharing options...
Demonic Posted April 8, 2007 Share Posted April 8, 2007 Try something like this: $blah = array();// your array here . $total = count($blah); $counter = 0; for($x=0;$x<$total;$x++){ if($blah[$x][show] == "y"){ $counter++; } } Link to comment https://forums.phpfreaks.com/topic/46187-counting-number-of-a-specified-value-in-an-multi-array/#findComment-224537 Share on other sites More sharing options...
saeed_violinist Posted April 8, 2007 Author Share Posted April 8, 2007 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++; } } Link to comment https://forums.phpfreaks.com/topic/46187-counting-number-of-a-specified-value-in-an-multi-array/#findComment-224555 Share on other sites More sharing options...
saeed_violinist Posted April 8, 2007 Author Share Posted April 8, 2007 hehey I found the solution, thanks to your idea $counter = 0; foreach($hidden as $key => $val) { if ($val["show"] == "N") { $counter++; } } echo $counter; Link to comment https://forums.phpfreaks.com/topic/46187-counting-number-of-a-specified-value-in-an-multi-array/#findComment-224558 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.