Karaethon Posted March 25, 2019 Share Posted March 25, 2019 (edited) $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; } Edited March 25, 2019 by Karaethon Quote Link to comment https://forums.phpfreaks.com/topic/308512-is-this-valid-syntax/ 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? Quote Link to comment https://forums.phpfreaks.com/topic/308512-is-this-valid-syntax/#findComment-1565602 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. Quote Link to comment https://forums.phpfreaks.com/topic/308512-is-this-valid-syntax/#findComment-1565603 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. Quote Link to comment https://forums.phpfreaks.com/topic/308512-is-this-valid-syntax/#findComment-1565604 Share on other sites More sharing options...
Karaethon Posted March 25, 2019 Author Share Posted March 25, 2019 (edited) 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 Edited March 25, 2019 by Karaethon Quote Link to comment https://forums.phpfreaks.com/topic/308512-is-this-valid-syntax/#findComment-1565605 Share on other sites More sharing options...
Karaethon Posted March 25, 2019 Author Share Posted March 25, 2019 (edited) 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; } Edited March 25, 2019 by Karaethon Quote Link to comment https://forums.phpfreaks.com/topic/308512-is-this-valid-syntax/#findComment-1565606 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 Quote Link to comment https://forums.phpfreaks.com/topic/308512-is-this-valid-syntax/#findComment-1565607 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... Quote Link to comment https://forums.phpfreaks.com/topic/308512-is-this-valid-syntax/#findComment-1565608 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; } Quote Link to comment https://forums.phpfreaks.com/topic/308512-is-this-valid-syntax/#findComment-1565609 Share on other sites More sharing options...
Karaethon Posted March 25, 2019 Author Share Posted March 25, 2019 (edited) 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!!!! Edited March 25, 2019 by Karaethon Quote Link to comment https://forums.phpfreaks.com/topic/308512-is-this-valid-syntax/#findComment-1565612 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)); } Quote Link to comment https://forums.phpfreaks.com/topic/308512-is-this-valid-syntax/#findComment-1565613 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.