[{id: 1, group: core},{id: 2, group: elite},{id: 3, group: elite},{id: 4, group: elite}]
How to check if an id already exits in the array?
I tried
in_array(array('id' => 2), $myarray) but it's not working.
you must pass all array:
in_array(array('id' => 2, 'group' => 'elite'), $myarray)
You can use array_column function to extract one column from array and then search for value, like this:
in_array(2, array_column($myarray, 'id'));
This assume that you convert that JSON to associative array and not array of objects.
WARNING: This function is available only on PHP 5.5 and newer, so you can use this workaround for older versions, where function is not exists (on 5.5+ it will use native fast implementation instead):
if (!function_exists('array_column')) {
function array_column($array, $column_key) {
$ret = array();
foreach ($array as $item)
$ret[] = $item[$column_key];
return $ret;
}
}