[英]find value in multidimensional array
I have to find value in a multidimensional array, size of array is not defined.我必须在多维数组中找到值,未定义数组的大小。 Let suppose user enters 1601 , result will be 011. and if 1605 , the result will be 015 according to array
假设用户输入 1601 ,结果将是 011. 如果 1605 ,结果将是 015 根据数组
array (size=6)
0 =>
array (size=2)
0 => string 'Zipcode' (length=7)
1 => string 'Territory Code' (length=14)
1 =>
array (size=2)
0 => string '1601' (length=4)
1 => string '011' (length=3)
2 =>
array (size=2)
0 => string '1602' (length=4)
1 => string '012' (length=3)
3 =>
array (size=2)
0 => string '1603' (length=4)
1 => string '013' (length=3)
4 =>
array (size=2)
0 => string '1604' (length=4)
1 => string '014' (length=3)
5 =>
array (size=2)
0 => string '1605' (length=4)
1 => string '015' (length=3)
If Zipcode
is unique then you can do:如果
Zipcode
是唯一的,那么您可以执行以下操作:
echo array_column($array, 1, 0)[1601];
Or if Territory Code
is unique:或者,如果
Territory Code
是唯一的:
echo array_search(1601, array_column($array, 0, 1), true);
array_column()
extracts a column from the multi-dimensional array to create a one dimensional array. array_column()
从多维数组中提取一列以创建一维数组。
array array_column ( array $input , mixed $column_key [, mixed $index_key = null ] )
数组 array_column ( 数组 $input , 混合 $column_key [, 混合 $index_key = null ] )
The second parameter $column_key
defining which column you want to get from the multidimensional array as values into the one dimensional array.第二个参数
$column_key
定义您希望从多维数组中获取哪一列作为一维数组中的值。 And the third parameter $index_key
defining which column you want to use as keys for the one dimensional array which you get back.第三个参数
$index_key
定义了您想将哪一列用作返回的一维数组的键。 If $index_key
is not defined, the array will be numerical enumerated.如果未定义
$index_key
,则数组将是数字枚举。
First code example第一个代码示例
So the first example extracts an array such as:因此,第一个示例提取了一个数组,例如:
array(1601 => '011', 1602 => '012')
Using the value 1601
as the key you get the desired output 011
.使用值
1601
作为键,您可以获得所需的输出011
。
Second code example第二个代码示例
The second example uses an array such as:第二个示例使用一个数组,例如:
array('011' => 1601, '012' => 1602)
And searches for 1601
with array_search()
to get the key 011
, which is the desired output.并使用
array_search()
搜索1601
以获取键011
,这是所需的输出。
See these two examples for what the second and third parameters do:有关第二个和第三个参数的作用,请参见这两个示例:
print_r(array_column($array, 1, 0));
print_r(array_column($array, 0, 1));
I wrote a nested function that does it:我写了一个嵌套函数来做到这一点:
$read=[
['Honar','607836545742426','IRO7P0001'],
['Danial','2304906587905','IRO7KHEP01'],
['Key'=>['sub_key'=>'sub_value']]
];
function is_exists_value($array,$needle){
if(!is_array($array) || is_null($needle))
exit("Error: check input");
if (array_key_exists($needle, $array)){
return true;
}elseif(in_array($needle,$array)){
return true;
}else{
foreach ($array as $value)
if(is_array($value))
if(is_exists_value($value,$needle))
return true;
}
return false;
}
var_dump(is_exists_value($read,"sub_value")); // Return True
var_dump(is_exists_value($read,"Key")); // Return True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.