Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多维数组中键的返回值_Php_Arrays - Fatal编程技术网

Php 多维数组中键的返回值

Php 多维数组中键的返回值,php,arrays,Php,Arrays,假设有这样一个二维数组: $rates = [ ['id' => 'd', 'title' => 'dollar', 'sign' => '$'], ['id' => 'r', 'title' => 'rial', 'sign' => 'ریال'], ['id' => 't', 'title' => 'toman', 'sign' => 'T'] ] 我有一个名为$rate的变量,如下所示:

假设有这样一个二维数组:

$rates = [
        ['id' => 'd', 'title' => 'dollar', 'sign' => '$'],
        ['id' => 'r', 'title' => 'rial', 'sign' => 'ریال'],
        ['id' => 't', 'title' => 'toman', 'sign' => 'T']
]
我有一个名为$rate的变量,如下所示:

$rate = 'd';
现在我只想得到数组的
title
的值,它的
id
是相同的
$rate
值。在这种情况下,
美元

我知道我们可以在主数组中搜索数组,但我会寻找一种更简单的方法,比如
$rates[????][title]

最好的方法是什么

如果要避免循环,使用and是最好的方法

您还可以变换数组或创建搜索函数,该函数允许您根据传递的内容选择所需内容

例如:在
$rates
get
title
中,其中
id
等于
d

<?php
$rates = [
        ['id' => 'd', 'title' => 'dollar', 'sign' => '$'],
        ['id' => 'r', 'title' => 'rial', 'sign' => 'ریال'],
        ['id' => 't', 'title' => 'toman', 'sign' => 'T']
];

$rates = function ($key, $where, $equals) use ($rates) {
    return $rates[array_search($equals, array_column($rates, $where))][$key];
};

echo $rates('title', 'id', 'd');        // dollar
echo $rates('sign', 'id', 'd');         // $
echo $rates('sign', 'title', 'dollar'); // $
如果要避免循环,使用and是最好的方法

您还可以变换数组或创建搜索函数,该函数允许您根据传递的内容选择所需内容

例如:在
$rates
get
title
中,其中
id
等于
d

<?php
$rates = [
        ['id' => 'd', 'title' => 'dollar', 'sign' => '$'],
        ['id' => 'r', 'title' => 'rial', 'sign' => 'ریال'],
        ['id' => 't', 'title' => 'toman', 'sign' => 'T']
];

$rates = function ($key, $where, $equals) use ($rates) {
    return $rates[array_search($equals, array_column($rates, $where))][$key];
};

echo $rates('title', 'id', 'd');        // dollar
echo $rates('sign', 'id', 'd');         // $
echo $rates('sign', 'title', 'dollar'); // $

如果您将其转换为使用
id
作为使用
array\u列的键,则您可以在以后的过程中访问它

$rates = [
        ['id' => 'd', 'title' => 'dollar', 'sign' => '$'],
        ['id' => 'r', 'title' => 'rial', 'sign' => 'ریال'],
        ['id' => 't', 'title' => 'toman', 'sign' => 'T']
]
$rates = array_column($rates,null,'id');

echo $rates['d']['title'];

dollar

如果您将其转换为使用
id
作为使用
array\u column
的键,则您可以在以后的过程中访问它

$rates = [
        ['id' => 'd', 'title' => 'dollar', 'sign' => '$'],
        ['id' => 'r', 'title' => 'rial', 'sign' => 'ریال'],
        ['id' => 't', 'title' => 'toman', 'sign' => 'T']
]
$rates = array_column($rates,null,'id');

echo $rates['d']['title'];

dollar