[英]regex pattern for json array and dynamic multi-level array check
On the page there will be about several hundred elements to which individual keywords are assigned, as in a kind of index.在页面上将有大约数百个元素被分配了单独的关键字,就像在一种索引中一样。
These keywords are used to dynamically show or hide these elements with JavaScript, depending on whether the searched keywords are found.这些关键字用于动态显示或隐藏这些带有 JavaScript 的元素,具体取决于是否找到搜索到的关键字。 The keywords are inserted by PHP into the data-tags of the individual elements.
关键字由 PHP 插入到各个元素的数据标签中。
For example, the keyword directory could look like this:例如,关键字目录可能如下所示:
$keywords =
[
'MainCategory1' =>
[
'Category1' =>
[
'Value1' => 0,
'SubCategory1' => [
'Value2' => 0,
'Value3' => 0
]
],
'Category2' =>
[
'Value4' => 0,
'Value5' => 0
]
],
'MainCategory2' =>
[
'Category2' =>
[
'Value2' => 0,
'Value4' => 0
],
'Category3' =>
[
'Value5' => 0,
'Value6' => 0
]
]
];
Some categories and values may occur more than once, depending on which subtopic they are in.某些类别和值可能会出现不止一次,具体取决于它们所在的子主题。
For example, an element has been assigned these keywords:例如,已为元素分配了这些关键字:
MainCategory1 > Category1
MainCategory1 > Category2 > Value5
MainCategory2 > Category2 > Value2
The keywords are stored in the backend as a json string and written to the data attributes.关键字作为 json 字符串存储在后端并写入数据属性。 For this element:
对于这个元素:
{"MainCategory1":{"Category1":0,"Category2":{"Value5":0}},"MainCategory2":{"Category2":{"Value2":0}}}
My idea now is to loop through all elements as soon as a keyword is selected and see if the stored JSON string matches "MainCategory1 > Category2 > Value5".我现在的想法是一旦选择了关键字就循环遍历所有元素,并查看存储的 JSON 字符串是否匹配“MainCategory1 > Category2 > Value5”。
This would correspond in PHP to这将对应于 PHP 到
isset($keywords['MainCategory1']['Category2']['Value5'])
My questions about this:我对此的疑问:
What I'm doing so far:到目前为止我在做什么:
$arrayString = explode(' > ', $array);
$foundCount = 0;
$currentArray = $keywords;
for($i = 1; $i <= count($arrayString); $i++) {
$key = $arrayString[$i - 1];
$keyExists = array_key_exists($key, $currentArray);
if(!$keyExists)
break;
$currentArray = $currentArray[$key];
$foundCount++;
}
$isKeywordValid = $foundCount == count($arrayString);
Taken from How to access and manipulate multi-dimensional array by key names / path?摘自如何通过键名/路径访问和操作多维数组? , you can walk down the structure using a reference:
,您可以使用参考走下结构:
function array_isset($path, $array) {
$path = explode(' > ', $path);
$temp =& $array;
foreach($path as $key) {
if(!isset($temp[$key])) { return false; }
$temp =& $temp[$key];
}
return true;
}
Or turn MainCategory1 > Category2 > Value5
into ["MainCategory1"]["Category2"]["Value5"]
and evaluate it as an isset
:或者将
MainCategory1 > Category2 > Value5
转换为["MainCategory1"]["Category2"]["Value5"]
并将其评估为isset
:
function array_isset($path, $array) {
$path = '["' . str_replace(' > ', '"]["', $path) . '"]';
eval("return isset(\$array{$path});");
}
Then just do:然后就这样做:
if(array_isset('MainCategory1 > Category2 > Value5', $keywords)) {
// code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.