Karaethon Posted March 25, 2019 Share Posted March 25, 2019 $ARR[$i] == $NEEDLE ? $Count++ : $Count I am using it in: private function _countIf($ARR, $NEEDLE){ $Count = 0; for($i = 0; $i < count($ARR); i++){ $ARR[$i] == $NEEDLE ? $Count++ : $Count } return $Count; } Link to comment Share on other sites More sharing options...
NotionCommotion Posted March 25, 2019 Share Posted March 25, 2019 I do not believe it is. What results are you getting? Link to comment Share on other sites More sharing options...
taquitosensei Posted March 25, 2019 Share Posted March 25, 2019 I just tried the syntax and it works. Have you tried it? Without knowing where $ARR and $NEEDLE come from and what they look like. It's hard to know what the problem is. Link to comment Share on other sites More sharing options...
Karaethon Posted March 25, 2019 Author Share Posted March 25, 2019 Havent yet tried it, its one method in a class im writing and it's callers are still under comstruction. Link to comment Share on other sites More sharing options...
Karaethon Posted March 25, 2019 Author Share Posted March 25, 2019 1 minute ago, taquitosensei said: I just tried the syntax and it works. Have you tried it? Without knowing where $ARR and $NEEDLE come from and what they look like. It's hard to know what the problem is. $ARR is any array and $NEEDLE is a value you are trying to count occurances of. Tho overall code works, the original version was if($ARR[$i] == $NEEDLE){ $Count++; } but I thought the newer version cleaner Link to comment Share on other sites More sharing options...
Karaethon Posted March 25, 2019 Author Share Posted March 25, 2019 oops, not that it makes much of a difference but i mis-typed when i wrote it here it's function countIf( $ARR, $NEEDLE){ $Count = 0; foreach( $ARR as $Entry){ $Entry === $NEEDLE ? $Count++ : $Count; } return $Count; } Link to comment Share on other sites More sharing options...
NotionCommotion Posted March 25, 2019 Share Posted March 25, 2019 Actually, I first thought it was valid but when first tried, it didn't work because of a typo. http://sandbox.onlinephpfunctions.com/code/71d97ebba750645b61dbfe821a9cf76e836ffec2 Link to comment Share on other sites More sharing options...
Karaethon Posted March 25, 2019 Author Share Posted March 25, 2019 2 minutes ago, NotionCommotion said: Actually, I first thought it was valid but when first tried, it didn't work because of a typo. http://sandbox.onlinephpfunctions.com/code/71d97ebba750645b61dbfe821a9cf76e836ffec2 I followed your link (neat site, gonna bookmark it) and it did work when i tried it... Link to comment Share on other sites More sharing options...
Barand Posted March 25, 2019 Share Posted March 25, 2019 try function countIf($arr, $needle) { $counts = array_count_values($arr); return $counts[$needle] ?? 0; } Link to comment Share on other sites More sharing options...
Karaethon Posted March 25, 2019 Author Share Posted March 25, 2019 12 minutes ago, Barand said: try function countIf($arr, $needle) { $counts = array_count_values($arr); return $counts[$needle] ?? 0; } CARP! I wrote countIf because all my searching of php documentation I couldnt find anything that let me search specific value.... I must have missed array_count_values, because it doesnt look for specific I moved on. I remember seeing it. arghhhh! all i needed was array_count_values(ARRAY)[VALUE IM LOKING FOR] ONE FREAKIN LINE!!!! Link to comment Share on other sites More sharing options...
Barand Posted March 25, 2019 Share Posted March 25, 2019 Alternatively function countIf($arr, $needle) { return count(array_keys($arr, $needle)); } Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.