[英]PHP Find value in a multi-dimensional array by key
我从API收到大型多维数组。 我将数组保存到类属性中,然后使用魔术吸气剂掩盖该数组,并让最终用户将键用作属性( $class->key ==> $class->data['key']
)。
现在,我要处理的数组是非常多维的,我需要位于不同“层”或层上的键。 想象一下,数组有两个带有多层的分支。
我正在寻找的钥匙通常是顶层。
我将尝试说明这一点:
Array
baseKey
key1
value1
key2
value2
key3
subKey1
value3
subKey2
value4
key4
value5
key5
subKey3
value6
key6
subKey4
subSubKey1
value7
我正在寻找一个可以给任何键提供功能的函数,包括baseKey
, key
, subKey
和subSubKey
,该函数将为匹配的第一个键返回各自的值。
因此,例如:
awesomeFindByKey('key2')
==> value2
awesomeFindByKey('subKey4')
==> array('subSubKey1' => 'value7')
awesomeFindByKey('key6')
==> array('subKey4' => array('subSubKey1' => 'value7'))
有这个功能吗?还是我必须自己写一个? 有人可能有执行此功能的功能吗?
我希望这是可以理解的。 谢谢。
这是非常简单的示例实现:
// sample class
class A {
// the data array
protected $data = array();
// init
public function __construct($data = null) {
$this->setData($data);
}
// recursive search of a key
private function searchLayer($key, $layer, $searchKey) {
// return the layer if we have a match
if ((string) $key === (string) $searchKey) return $layer;
// loop the array if we need to go deeper
if ((array) $layer === $layer) {
foreach ($layer as $k => $v) {
// apply recursition
$result = $this->searchLayer($k, $v, $searchKey);
// return on match
if (!empty($result)) return $result;
}
}
// nothing found - recursion ended here
return null;
}
// returns the value of the data array with the given key
public function getValueByKey($searchKey) {
echo '<br>search by key: ' . $searchKey . '<br>';
// check if we have a match
foreach ($this->data as $key => $layer) {
// resolve layer
$result = $this->searchLayer($key, $layer, $searchKey);
// return on match
if (!empty($result)) return $result;
}
// nothing found
return null;
}
// set the data
public function setData($data) {
if ((array) $data === $data) $this->data = $data;
}
// possible extension
public function __get($key) {
return $this->getValueByKey($key);
}
}
// create the object of the class 'a'
$a = new A();
// set the data
$a->setData(array(
'value1',
'key2' => 'value2',
'key3' => array(
'subkey31' => 'value31',
'subkey32' => 'value32'
)
));
var_dump($a->getValueByKey('subkey32'));
var_dump($a->subkey32);
输出: string(7) "value32" string(7) "value32"
在此处进行测试: http : //writecodeonline.com/php只需粘贴代码并运行即可。
编辑
当然,这是一个非常基本的示例-您还可以使用某些给定的php类,例如ArrayAccess
。
如果数组存储在类中,则可以实现PHP提供的ArrayAccess
接口。 这将使您能够在不同的语言构造中使用(内部)数组,以及能够将类本身用作数组。
例:
$value = $class['base_key']['key']['sub_key'];
这还有一个优点,就是您可以根据需要拥有任意多个级别。 我认为这比您提出的仅能处理3级深度数组的功能更方便。
UPDATE
拟议功能的可能实现如下所示:
public function resolve_key($key, $array) {
/*
* Check if the key exists in the first array level.
*/
if(array_key_exists($key, $array)) {
return $array[$key];
}
/*
* Check if the key exists as a sub key in the value of each array element.
*/
foreach($array as $value) {
if(is_array($value)) {
return resolve_key($key, $value);
}
}
/*
* Return NULL if no key was found or throw an exception.
*/
return null;
}
这使用了递归(如Karoly Hovarth在评论中所建议的)。 首先检查请求的键是否在提供的数组的第一级中。 如果否,则对作为数组的每个数组值再次执行该函数。
为了能够找到作为类属性的键,请使用以下命令:
public function __get($key) {
return $this->resolve_key($key, $this->internal_array);
}
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.