PHP array_column - how to keep the keys?
Looking for the same solution and combine some tricks, I created this:
$userdb=Array
(
"test1" => array
(
'uid' => '100',
'name' => 'Sandra Shush',
'url' => 'urlof100'
),
"test2" => array
(
'uid' => '5465',
'name' => 'Stefanie Mcmohn',
'pic_square' => 'urlof100'
),
"test3" => array
(
'uid' => '40489',
'name' => 'Michael',
'pic_square' => 'urlof40489'
)
);
echo $key = array_search(
40489,
array_filter(
array_combine(
array_keys($userdb),
array_column(
$userdb, 'uid'
)
)
)
);
Result is 'test3'.
Work in array numbers or named arrays.
I think this is the fastest way to keep keys without loops and iterations
array_diff(array_combine(array_keys($items), array_column($items, 'id')), [null])
The simplest and the most performant way is probably using
array_combine(array_keys($data), array_column($data, 0));
See if this could help
array_filter(array_combine(array_keys($items), array_column($items, 'id')));