php - Quickest way to check an array of classed for an item? -
i have class values:
array ( [0] => stdclass object ( [id] => 1 [val] => 1 ) [1] => stdclass object ( [id] => 2 [val] => 2 ) [2] => stdclass object ( [id] => 3 [val] => 3 ) )
what quickest way search array see if there item id = 2? know work:
$hastwo = false; foreach ($arrayitems $arrayitem) { if ($arrayitem->id == 2) { $hastwo = true; break; } } if ($hastwo) { // wanted do... }
is there no easier way requires less code can same?
perhaps
$hastwo = array_filter( $arrayitems, function($value) { return $value->id == 2; } );
but you'll need test faster, , answer original code
Comments
Post a Comment