PHP:使用键数组获取数组值或使用多个变量键引用数组值
PHP: Grab array value using an array of keys OR Reference array value using multiple variable keys
如果我有一个无限深的数组,如果我有一个键数组,我该如何引用特定的值。
示例数组:
Array
(
[a] => Array
(
[id] => something
...
)
[b] => Array
(
[id] => something
...
)
)
在上面的示例数组中,如果我改为使用这样的函数,我会怎么做:
function example($keys)
{
$keys = array("a", "id"); // example keys
$this->data[...$keys] = "something";
// $this->data["a"]["id] = "something";
}
请注意,我不能只 运行 一个循环来获取值,因为我需要引用原始数组,而不是创建一个新数组。所以这样的东西不是我需要的
$data = $this->data;
foreach($keys as $v){
$data = $data[$v];
}
我真正需要的只是一种使用键数组引用原始文件的简单方法。
所以您想在知道路径的数组中查找值?
有同样的事情,给我建个东西。
它并不完美。但它完成了工作。
注意:如果您更改它,那么如果找不到路径,请不要 return false。因为 false 也可能是找到的值。
方法:
/**
* Returns a value from given source by following the given keys.
*
* Example:
* <code>
* $array = [
* 'foo' => [
* 'bar' => [
* 'baz' => null,
* ],
* 'bar_2' => [
* 0 => [
* 'baz' => null,
* ],
* 1 => [
* 'baz' => 123,
* ],
* 2 => [
* 'baz' => 456,
* ],
* ]
* ]
* ];
* ::find($array, ['foo', 'bar_2', 1, 'baz']) // 123
* </code>
*
* @param array $source
* @param array $keys
*
* @return array|bool|mixed
*/
public function find(array $source, array $keys)
{
if (empty($keys)) {
// No keys - so actually expect the complete source to return.
return $source;
}
// Get the (next-) first key.
$keyCurrent = array_shift($keys);
if (!array_key_exists($keyCurrent, $source)) {
// Expected path to value does not exist.
throw new \InvalidArgumentException("Key '{$keyCurrent}' not found.");
}
$source = $source[$keyCurrent];
if (!empty($keys)) {
// Still got keys - need to keep looking for the value ...
if (!is_array($source)) {
// but no more array - cannot continue.
// Expected path to value does not exist. Return false.
throw new \InvalidArgumentException("No array on key '{$keyCurrent}'.");
}
// continue the search recursively.
$source = $this->find($source, $keys);
}
// No more keys to search for.
// We found the value on the given path.
return $source;
}
测试:
$array = [
'a' => ['id' => 'something at array[a][id]'],
'b' => ['id' => 'something at array[b][id]'],
'c' => [
'ca' => [
'cb' => [
'cc' => ['id' => 'something at array[c][ca][cb][cc][id]'],
],
],
],
];
$value = $this->find($array, ['c', 'ca', 'cb', 'cc', 'id']);
// something at array[c][ca][cb][cc][id]
$value = $this->find($array, ['c', 'ca', 'cb', 'cc']);
// array (
// 'id' => 'something at array[c][ca][cb][cc][id]',
// )
$value = $this->find($array, ['b', 'id']);
// something at array[b][id]
$value = $this->find($array, ['c', 'ca', 'cb', 'cc', 'does_not_exist']);
// Fatal error: Uncaught InvalidArgumentException: Key 'does_not_exist' not found. in
如果我有一个无限深的数组,如果我有一个键数组,我该如何引用特定的值。
示例数组:
Array
(
[a] => Array
(
[id] => something
...
)
[b] => Array
(
[id] => something
...
)
)
在上面的示例数组中,如果我改为使用这样的函数,我会怎么做:
function example($keys)
{
$keys = array("a", "id"); // example keys
$this->data[...$keys] = "something";
// $this->data["a"]["id] = "something";
}
请注意,我不能只 运行 一个循环来获取值,因为我需要引用原始数组,而不是创建一个新数组。所以这样的东西不是我需要的
$data = $this->data;
foreach($keys as $v){
$data = $data[$v];
}
我真正需要的只是一种使用键数组引用原始文件的简单方法。
所以您想在知道路径的数组中查找值?
有同样的事情,给我建个东西。
它并不完美。但它完成了工作。
注意:如果您更改它,那么如果找不到路径,请不要 return false。因为 false 也可能是找到的值。
方法:
/**
* Returns a value from given source by following the given keys.
*
* Example:
* <code>
* $array = [
* 'foo' => [
* 'bar' => [
* 'baz' => null,
* ],
* 'bar_2' => [
* 0 => [
* 'baz' => null,
* ],
* 1 => [
* 'baz' => 123,
* ],
* 2 => [
* 'baz' => 456,
* ],
* ]
* ]
* ];
* ::find($array, ['foo', 'bar_2', 1, 'baz']) // 123
* </code>
*
* @param array $source
* @param array $keys
*
* @return array|bool|mixed
*/
public function find(array $source, array $keys)
{
if (empty($keys)) {
// No keys - so actually expect the complete source to return.
return $source;
}
// Get the (next-) first key.
$keyCurrent = array_shift($keys);
if (!array_key_exists($keyCurrent, $source)) {
// Expected path to value does not exist.
throw new \InvalidArgumentException("Key '{$keyCurrent}' not found.");
}
$source = $source[$keyCurrent];
if (!empty($keys)) {
// Still got keys - need to keep looking for the value ...
if (!is_array($source)) {
// but no more array - cannot continue.
// Expected path to value does not exist. Return false.
throw new \InvalidArgumentException("No array on key '{$keyCurrent}'.");
}
// continue the search recursively.
$source = $this->find($source, $keys);
}
// No more keys to search for.
// We found the value on the given path.
return $source;
}
测试:
$array = [
'a' => ['id' => 'something at array[a][id]'],
'b' => ['id' => 'something at array[b][id]'],
'c' => [
'ca' => [
'cb' => [
'cc' => ['id' => 'something at array[c][ca][cb][cc][id]'],
],
],
],
];
$value = $this->find($array, ['c', 'ca', 'cb', 'cc', 'id']);
// something at array[c][ca][cb][cc][id]
$value = $this->find($array, ['c', 'ca', 'cb', 'cc']);
// array (
// 'id' => 'something at array[c][ca][cb][cc][id]',
// )
$value = $this->find($array, ['b', 'id']);
// something at array[b][id]
$value = $this->find($array, ['c', 'ca', 'cb', 'cc', 'does_not_exist']);
// Fatal error: Uncaught InvalidArgumentException: Key 'does_not_exist' not found. in