I am using PHP & I have a multi dimensional array which I need to search to see if the value of a "key" exists and if it does then get the value of the "field". Here's my array:
我使用的是PHP &我有一个多维数组,我需要搜索一个“键”的值是否存在,然后它是否会得到“字段”的值。这是我的数组:
Array
(
[0] => Array
(
[key] => 31
[field] => CONSTRUCTN
[value] => LFD_CONSTRUCTION_2
)
[1] => Array
(
[key] => 32
[field] => COOLING
value] => LFD_COOLING_1
)
)
I want to be able to search the array for the "key" value of 31. If it exists, then I want to be able to extract the corresponding "field" value of "CONSTRUCTN".
我希望能够搜索数组中31的“key”值。如果它存在,那么我希望能够提取“CONSTRUCTN”对应的“字段”值。
I've tried using array_search(31, $myArray) but it does not work...
我尝试过使用array_search(31, $myArray),但是它不起作用……
4 个解决方案
#1
1
function searchMultiArray($val, $array) {
foreach ($array as $element) {
if ($element['key'] == $val) {
return $element['field'];
}
}
return null;
}
And then:
然后:
searchMultiArray(31, $myArray);
Should return "CONSTRUCTN".
应该返回“CONSTRUCTN”。
#2
3
One-line solution using array_column
and array_search
functions:
使用array_column和array_search函数的一行解决方案:
$result = array_search(31, array_column($arr, 'key', 'field'));
print_r($result); // CONSTRUCTN
Or with simple foreach
loop:
或者简单的foreach循环:
$search_key = 31;
$result = "";
foreach ($arr as $item) { // $arr is your initial array
if ($item['key'] == $search_key) {
$result = $item['field'];
break;
}
}
print_r($result); // CONSTRUCTN
#3
0
I haven't tested, but I think this should do it.
我还没有测试过,但是我认为这个应该可以。
function searchByKey($value, $Array){
foreach ($Array as $innerArray) {
if ($innerArray['key'] == $value) {
return $innerArray['field'];
}
}
}
Then calling searchByKey(31, $myArray);
should return 'CONSTRUCTN'.
然后调用searchByKey(31日myArray美元);应该返回“CONSTRUCTN”。
Hope that helps.
希望有帮助。
#4
0
You could use array_filter, something like this,
你可以使用array_filter,像这样,
function searchMultiDimensionalArray($array, $key, $value) {
$items = array_filter($array, function($item){
return $item[$key] == $value;
});
if (count($items) > 0) {
return $items[0];
}
return NULL;
}
And use it just like this for any multidimensional array,
对于任何多维数组,
$searchItem = searchMultiDimensionalArray($myArray, 'key', 31)
if ($searchItem != NULL) {
$field = $searchItem['field']
}
#1
1
function searchMultiArray($val, $array) {
foreach ($array as $element) {
if ($element['key'] == $val) {
return $element['field'];
}
}
return null;
}
And then:
然后:
searchMultiArray(31, $myArray);
Should return "CONSTRUCTN".
应该返回“CONSTRUCTN”。
#2
3
One-line solution using array_column
and array_search
functions:
使用array_column和array_search函数的一行解决方案:
$result = array_search(31, array_column($arr, 'key', 'field'));
print_r($result); // CONSTRUCTN
Or with simple foreach
loop:
或者简单的foreach循环:
$search_key = 31;
$result = "";
foreach ($arr as $item) { // $arr is your initial array
if ($item['key'] == $search_key) {
$result = $item['field'];
break;
}
}
print_r($result); // CONSTRUCTN
#3
0
I haven't tested, but I think this should do it.
我还没有测试过,但是我认为这个应该可以。
function searchByKey($value, $Array){
foreach ($Array as $innerArray) {
if ($innerArray['key'] == $value) {
return $innerArray['field'];
}
}
}
Then calling searchByKey(31, $myArray);
should return 'CONSTRUCTN'.
然后调用searchByKey(31日myArray美元);应该返回“CONSTRUCTN”。
Hope that helps.
希望有帮助。
#4
0
You could use array_filter, something like this,
你可以使用array_filter,像这样,
function searchMultiDimensionalArray($array, $key, $value) {
$items = array_filter($array, function($item){
return $item[$key] == $value;
});
if (count($items) > 0) {
return $items[0];
}
return NULL;
}
And use it just like this for any multidimensional array,
对于任何多维数组,
$searchItem = searchMultiDimensionalArray($myArray, 'key', 31)
if ($searchItem != NULL) {
$field = $searchItem['field']
}