php - PHP循环遍历数组,查找匹配并附加属性
问题描述
如果找到匹配项,我需要检测当前 URL 并为其设置一个附加属性,所以目前我正在循环遍历该对象,如果未找到匹配项并且当前属性是一个数组,则将该数组传递给它自己。
所以我无法知道我在对象中的哪个位置来设置属性,我需要以某种方式保留键的引用吗?所以我可以设置 objects 属性。
示例对象:
$example = [
'home' => [
'link' => 'index.php',
'code' => 'home',
'label' => 'Home'
],
'cars' => [
'link' => 'javascript:void(0)',
'code' => 'used-cars',
'label' => 'Used Cars',
'void' => true,
'subpages' => [
[
'link' => 'used-cars',
'code' => 'used-cars',
'label' => 'View all cars'
]
],
'brands' => [
'used' => [
[
'link' => "/used/cars/audi",
'code' => "used-audi",
'label' => "Audi"
],
[
'link' => "/used/cars/bmw",
'code' => "used-bmw",
'label' => "BMW"
],
[
'link' => "/used/cars/chevrolet",
'code' => "used-chevrolet",
'label' => "Chevrolet"
]
]
]
]
];
因此,例如,假设我目前正在使用“二手车/汽车/奥迪”。上面包含它的数组link
应该如下所示:
[
'link' => "/used/cars/audi",
'code' => "used-audi",
'label' => "Audi"
'current' = true
]
这是我目前拥有的代码,如何在循环期间将属性添加到对象内的正确位置?
private function findCurrent($links) {
$request_uri = explode("?", $_SERVER['REQUEST_URI'])[0];
if ($request_uri === '') {
$request_uri = '/index.php';
}
foreach ($links as $key => $value) {
if (isset($value) && $value == $request_uri) {
// Set as current
break;
}
// No match, but does it have children!?
if (is_array($value)) {
$this->findCurrent($value);
}
}
}
解决方案
一个问题是该行:$value == $request_uri
-> 因为'void' => true
将完成该惰性比较。将其更改为
$value == $request_uri
第二:如果要从函数内部和 foreach 循环内部更改数组,则需要通过引用传递它
这是您的功能的一个版本,它可以按照我的预期工作:
// this '&' is important to pass the array as reference
function findCurrent(&$links) {
// dummy-data here for testing
$request_uri = explode("?", "/used/cars/audi?test")[0];
// original: $request_uri = explode("?", $_SERVER['REQUEST_URI'])[0];
if ($request_uri === '') {
$request_uri = '/index.php';
}
// again, use $value as reference
foreach ($links as $key => &$value) {
#echo "now in $key<br>";
if (isset($value) && $value === $request_uri) {
// we found the correct entry, so return true
return true;
}
// No match, but does it have children!?
if (is_array($value)) {
// if we found the link in this array,...
if(findCurrent($value)) {
// add the flag 'current'
$value['current'] = true;
// and get out of the function
return true;
}
}
}
}
$success = findCurrent($links);
// testing if that array has changed as intended:
var_dump($links['cars']['brands']['used'][0]);
// array(4) {
// ["link"]=> string(15) "/used/cars/audi"
// ["code"]=> string(9) "used-audi"
// ["label"]=> string(4) "Audi"
// ["current"]=> bool(true)
// }
推荐阅读
- api - 如何通过 api 将电子表格(ODS 或 XLSX)插入文本文档(ODT 或 DOCX)
- ruby-on-rails - 使用 S3 中的现有文件创建活动存储附件
- c# - JsonConvert.DeserializeObject 数组问题
- python - Python-Selenium - if else 语句检查元素可见性并单击(如果可用)
- reactjs - 如何使用酶在具有特定类的元素中找到 React 组件?
- regex - 如何为此创建 REGex ---> 7 到 12 个字符长,第 4、第 6 和(如果存在)第 10 是以下之一:1、5、9、A、S、K
- azure - 通过 TLS 从 Azure Function 使用 MQTT 触发器时出现问题
- python - 如何在 Python 3 中测试 NoneType 上的两个变量?
- python - Cython 中有哪些可用的类型?
- python - 如何改进我的 Python 代码以应用于有向图的 n 个节点?