简体   繁体   English

json 数组和动态多级数组检查的正则表达式模式

[英]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:我对此的疑问:

  1. How could I make the isset query dynamic in php if I don't know beforehand how many levels the keyword has?如果我事先不知道关键字有多少级别,我怎么能在 php 中使 isset 查询动态化? This is important for when a keyword is set, to check in the backend whether the keyword is valid at all.这对于设置关键字很重要,可以在后端检查关键字是否有效。 I could loop through each level though;不过,我可以遍历每个级别; is there an alternative for 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);
  1. I am familiar with regex, but I am having trouble finding a pattern that solves this problem.我熟悉正则表达式,但找不到解决此问题的模式。 What does the pattern have to look like if I want to know if "MainCategory1 > Category2 > Value5" matches the JSON string in this particular example?如果我想知道“MainCategory1 > Category2 > Value5”是否与这个特定示例中的 JSON 字符串匹配,模式必须是什么样子? (This convention is only a paraphrase for the different levels. The string can also be parsed differently) (这个约定只是针对不同级别的意译。字符串也可以进行不同的解析)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM