I have a multidimentional array like this $membergroups
:
Array ( [0] =>
Array ( [id] => 1645819602
[name] => Oryza NurFa
[first_name] => Oryza
[last_name] => NurFa
[work] => MAN 2 Yogyakarta )
[1] =>
Array ( [id] => 100000251643877
[name] => Lathif Pambudi
[first_name] => Muhammad Lathif
[last_name] => Pambudi
[work] => Omah TI )
[2] =>
Array ( [id] => 1152078197
[name] => Novantio Bangun
[first_name] => Novantio
[last_name] => Bangun
[work] => Pertamina))
How to delete one of child array with specific value. For the example, I want to delete an array with [id] => 100000251643877
inside? So the output will be :
Array ( [0] =>
Array ( [id] => 1645819602
[name] => Oryza NurFa
[first_name] => Oryza
[last_name] => NurFa
[work] => MAN 2 Yogyakarta )
[1] =>
Array ( [id] => 1152078197
[name] => Novantio Bangun
[first_name] => Novantio
[last_name] => Bangun
[work] => Pertamina))
Here is my php code, but it doesn't work :
if (($key = array_search($user_fbid, $membergroups)) !== false) {
unset($membergroups[$key]);
}
Any help would be greatly appreciated. Thank you
Using a foreach you can do the job like this
$id = 100000251643877;//Example
foreach($membergroups as $key => $value){
if($value['id'] == $id){
unset($membergroups[$key]);
}
}
You can make use of array_column
but only for php >= 5.5
if (($key = array_search($user_fbid, array_column( $membergroups, 'id') ) !== false) {
unset($membergroups[$key]);
}
array_column( $membergroups, 'id')
search in membergroup multidimensional array for id column, and return tou you an array containing all rows values entries with id key.
array_column -> MANUAL
Loop through the whole array:
foreach ($membergroups as $idx => $group) {
if ($group['id'] === $user_fbid) {
unset($membergrouops[$idx]);
break;
}
}
foreach($membergroups as $key => $value){
if($value['id'] == $user_fbid){
unset($membergroups[$key]);
}
}
Don't forget to merge the array after removing key to keep the index sequantially
$membergroups = array_merge($membergroups);