Php 如果存在其他参数,则在数组中查找参数
我的任务是在其他数组中存在其他参数时查找参数的值。我不知道如何用文字来正确描述它,所以这里是伪代码:Php 如果存在其他参数,则在数组中查找参数,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我的任务是在其他数组中存在其他参数时查找参数的值。我不知道如何用文字来正确描述它,所以这里是伪代码: if $_POST['from'] == X 在$array键'name'==X及其键'timezone'中查找,并将此值另存为参数: $array = [ [ 'name' => 'A', 'timezone' => 'AB', ], [ 'na
if $_POST['from'] == X
在$array
键'name'==X
及其键'timezone'
中查找,并将此值另存为参数:
$array = [
[
'name' => 'A',
'timezone' => 'AB',
],
[
'name' => 'B',
'timezone' => 'BC',
],
[
'name' => 'C',
'timezone' => 'AB',
],
如果$\u POST['name']==B
,'timezone'==BC
如果X,时区=B,我可以在10倍前到达,但我认为这不是最好的方法。我现在在这一步:
foreach ($array as $key => $value) {
foreach ($value as $key2 => $value2) {
if ($value2 == $_POST['from']) {
if ($key2 == 'timezone') {
echo $key;
}
}
}
}
只要
名称
值是唯一的:
echo array_column($array, 'timezone', 'name')[$_POST['from']];
- 提取一个数组,其中
作为键,name
作为值timezone
- 使用键
$\u POST['from']
isset($\u POST['from'])
):
这是array_column的一个有趣用法-以前从未见过这样的用法。非常感谢。我试过使用array_列,但不知道在这个函数中会出现这样的可能性
$tz = array_column($array, 'timezone', 'name');
if(isset($tz[$_POST['from']])) {
echo $tz[$_POST['from']];
}