I have multidimensional array and I need to search for top level key by the value in "add_fields" arrays by "value". I cannot figure out how I can achieve the result.
I tried this code:
array_search(
'001001',
array_column(
array_column(
$arr,
"usr_column_504"
),
0
)
),
but didn't get anything and I need to get top level key. In this example its **0**;
array (
0 =>
array (
'id' => 1,
'group_id' => 327,
'volume' => 0,
'vat' => 1,
'order_id' => 1,
'add_fields' =>
array (
1 =>
array (
'field' => 'usr_column_501',
'value' => '',
),
2 =>
array (
'field' => 'usr_column_504',
'value' => '001001',
),
),
),
1 =>
array (
'id' => 2,
'group_id' => 327,
'vat' => 1,
'order_id' => 2,
'add_fields' =>
array (
1 =>
array (
'field' => 'usr_column_501',
'value' => '',
),
2 =>
array (
'field' => 'usr_column_504',
'value' => '001002',
),
),
),
)
If you want the first key where the value of field
equals usr_column_504
you could use an outer and an inner foreach.
When the value of field is found, return the $key
from the outer foreach.
foreach ($arr as $key => $item) {
foreach ($item["add_fields"] as $addField) {
if ($addField["field"] === "usr_column_504") {
echo $key;
return;
}
}
}
Output
0