Jump to content

Is this valid syntax?


Karaethon

Recommended Posts

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

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

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

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.