PHP remove sub-array

Viewed 39

I have a PHP array that is returning dynamic values as follows:

Array
(
[0] => Array
    (
        [user_id] => 1234
    )

[1] => Array
    (
        [user_id] => 9876
    )

)

Problem is that I want to find out what array key user_id 9876 has, but because user_id is a sub array I can't simply do an array flip and then look it up like that. I tried doing an array flip method like below but the array flip doesnt work

$fliparray = array_flip($original);
echo $fliparray['7876'];
1 Answers

With

$arrId = array_column($arr,null,'user_id');

you get an array of the form

array (
  1234 => 
  array (
    'user_id' => 1234,
  ),
  5678 => 
  array (
    'user_id' => 5678,
  ),
)

You can delete entries directly by use id.

unset($arrId[1234]);

If necessary, you can with

$numArr = array_values($arrId);

return an array how

array (
  0 => 
  array (
    'user_id' => 5678,
  ),
)

Demo: https://3v4l.org/lBcqK

Related