Skip to content

Instantly share code, notes, and snippets.

@Digi92
Last active March 8, 2017 14:57
Show Gist options
  • Select an option

  • Save Digi92/4a4d2376e127ab52551debadc83e52c6 to your computer and use it in GitHub Desktop.

Select an option

Save Digi92/4a4d2376e127ab52551debadc83e52c6 to your computer and use it in GitHub Desktop.
Find entry by object property from a array of objects
<?php
// Note: This will return all results with if true
$neededObject = array_filter(
$arrayOfObjects,
function ($element) use (&$searchedValue) {
/**@var element $element*/
return $element->id == $searchedValue;
}
);
<?php
// Note: This will return only the first hit
$item = null;
foreach ($array as $struct) {
/**@var element $element*/
if ($element == $struct->ID) {
$item = $struct;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment