我有一个包含多个数组的数组,每个数组中的一个字段是另一个数组,请参见下文。我需要在“item_id”值为 177 的数组中找到“First name”的值。我该怎么做?
array(1) {
[0]=> array(4)
{
["product_id"]=> int(62)
["item_id"]=> int(177)
["quantity"]=> int(1)
["options"]=> array(5) {
[0]=> array(4) {
["field_id"]=> string(13) "655259ef26f01"
["label"]=> string(11) "First name:"
["value"]=> string(4) "test" ["type"]=> string(4) "text" }
[1]=> array(4) { ["field_id"]=> string(13) "65525a47553c3"
["label"]=> string(10) "Last name:"
["value"]=> string(4) "test" ["type"]=> string(4) "text" }
[2]=> array(4) {
["field_id"]=> string(13) "65525a658669b"
["label"]=> string(15) "E-mail address:"
["value"]=> string(17) "[email protected]"
["type"]=> string(5) "email" }
[3]=> array(4) { ["field_id"]=> string(13) "65525a964be34"
["label"]=> string(27) "Language for questionnaire:"
["value"]=> string(6) "German"
["type"]=> string(6) "select" }
} }
[1]=> array(4) {
["product_id"]=> int(62)
["item_id"]=> int(182)
["quantity"]=> int(1)
["options"]=> array(7) {
[0]=> array(4) {
["field_id"]=> string(13) "655259ef26f01"
["label"]=> string(11) "First name:"
["value"]=> string(4) "test"
["type"]=> string(4) "text"
}
[1]=> array(4) {
["field_id"]=> string(13) "65525a47553c3"
["label"]=> string(10) "Last name:"
["value"]=> string(4) "test"
["type"]=> string(4) "text"
}
[2]=> array(4) {
["field_id"]=> string(13) "65525a658669b"
["label"]=> string(15) "E-mail address:"
["value"]=> string(17) "[email protected]"
["type"]=> string(5) "email"
}
[3]=> array(4) {
["field_id"]=> string(13) "65525a7bb053f"
["label"]=> string(46) "Send copy of the report to this email address:"
["value"]=> string(17) "[email protected]"
["type"]=> string(5) "email"
}
[4]=> array(4) {
["field_id"]=> string(13) "65525a964be34"
["label"]=> string(27) "Language for questionnaire:"
["value"]=> string(7) "Chinese" ["type"]=> string(6) "select" }
} } }
我尝试了几件事,其中之一是:
$fname = $customs['item_id'][$itemID]['options']['field_id']['655259ef26f01']['value'];
您可以使用搜索
$custom
数组的函数,然后如果 item_id
与传递的内容匹配,则查找 options
子数组,然后在 value
匹配时取出 field_id
:
<?php
/*
Question Author: Pete
Question Answerer: Jacob Mulquin
Question: Find value in multidimensional array based on key
URL: https://stackoverflow.com/questions/77506541/find-value-in-multidimensional-array-based-on-key
Tags: , php, multidimensional-array, find
*/
$customs = [
[
'product_id' => 61,
'item_id' => 177,
'quantity' => 1,
'options' => [
[
'field_id' => '655259ef26f01',
'label' => 'First name:',
'value' => 'test firstname1',
'type' => 'text'
],
[
'field_id' => '65525a47553c3',
'label' => 'Last name:',
'value' => 'test lastname1',
'type' => 'text'
]
]
],
[
'product_id' => 60,
'item_id' => 177,
'quantity' => 1,
'options' => [
[
'field_id' => '655259ef26f01',
'label' => 'First name:',
'value' => 'test firstname1 again',
'type' => 'text'
],
[
'field_id' => '65525a47553c3',
'label' => 'Last name:',
'value' => 'test lastname1',
'type' => 'text'
]
]
],
[
'product_id' => 62,
'item_id' => 182,
'quantity' => 1,
'options' => [
[
'field_id' => '655259ef26f01',
'label' => 'First name:',
'value' => 'test firstname2',
'type' => 'text'
],
[
'field_id' => '65525a47553c3',
'label' => 'Last name:',
'value' => 'test lastname2',
'type' => 'text'
]
]
]
];
function search_item_array($search, $item_id, $field_id)
{
$found = [];
foreach ($search as $record) {
if ($record['item_id'] !== $item_id)
continue;
foreach ($record['options'] as $option) {
if ($option['field_id'] === $field_id)
$found[] = $option['value'];
}
}
return $found;
}
$output = search_item_array($customs, 177, '655259ef26f01');
var_dump($output);
产量:
array(2) {
[0]=>
string(15) "test firstname1"
[1]=>
string(21) "test firstname1 again"
}