【问题标题】:Multi-dimensional array search for part of word or phrase and remove key多维数组搜索部分单词或短语并删除关键
【发布时间】:2018-10-30 03:25:13
【问题描述】:

在开始之前,我正在学习,我不会声称自己是 PHP 专家。我尝试了几种不同的方法,但这种方法让我最接近我正在寻找的东西。

我有一个要搜索的 JSON 数组,如果部分文本与数组中一行(警报)的任何部分匹配,则从数组中删除整个键。 (如果可能的话,我只希望它匹配最新的键,而不是删除所有匹配的键)

下面的代码正在处理数组中的最新项目,但无法搜索较旧的记录。

例如,

[8] => Array
       (
           [Code] => 9
           [Alerts] => bob went away
       )

   [9] => Array
       (
           [Code] => 9
           [Alerts] => randy jumped in the air
       )

)

如果我调用脚本,使用“bob”这个词,它什么也找不到。如果我使用术语“randy”调用脚本,它将完美地删除键 9。然后我可以搜索术语“bob”,它将删除键 8。

这是我目前所拥有的。 (同样可能有更好的方法)

<?php   
$jsondata = file_get_contents('myfile.json');
$json = json_decode($jsondata, true);
$done = 'term';
$pattern = preg_quote($done, '/');
$pattern = "/^.*$pattern.*\$/m";
$arr_index = array();
foreach ($json as $key => $value)
    $contents = $value['Alerts'];
{
    if(preg_match($pattern, $contents, $matches))
    {
        $trial = implode($matches);
    }
    if ($contents == $trial)
    {
        $arr_index[] = $key;
    }
}
foreach ($arr_index as $i)
{
    unset($json[$i]);
}
$json = array_values($json);
file_put_contents('myfile-test.json', json_encode($json));
echo $trial; //What did our search come up with?
die;
}

再次感谢!

【问题讨论】:

    标签: php arrays json search


    【解决方案1】:

    问题是使用$contents 的代码不在foreach 循环内。循环体中只有一条语句:

    $contents = $value['Alerts'];
    

    当循环结束时,$contents 包含最后一个警报值,然后在它之后的代码块中使用。

    您需要将该语句放在大括号内。

    <?php   
    $jsondata = file_get_contents('myfile.json');
    $json = json_decode($jsondata, true);
    $done = 'term';
    $pattern = preg_quote($done, '/');
    $pattern = "/^.*$pattern.*\$/m";
    $arr_index = array();
    foreach ($json as $key => $value)
    {
        $contents = $value['Alerts'];
        if(preg_match($pattern, $contents, $matches))
        {
            $trial = implode($matches);
        }
        if ($contents == $trial)
        {
            $arr_index[] = $key;
        }
    }
    foreach ($arr_index as $i)
    {
        unset($json[$i]);
    }
    $json = array_values($json);
    file_put_contents('myfile-test.json', json_encode($json));
    echo $trial; //What did our search come up with?
    die;
    }
    

    你应该使用你的编辑器的功能来自动缩进代码,它会让这样的问题更加明显。

    【讨论】:

    • 感谢 Barmar 的输入。我实际上最初是这样设置的,但问题是它会将数组更改为仅出于某种原因找到的数组。我实际上简化了一点,它按预期工作。非常感谢您的帮助!
    • $trial = implode($matches); 有什么意义? $pattern 中没有捕获组,因此 $matches 只有一个元素。那应该只是$trial = $matches[0];
    • 您每次循环都会覆盖$trial,因此当您最后echo $trial 时,它将只是找到的最后一个。也许你应该把它们做成一个数组。
    • 是的 - 你是对的。这只是为了让我快速进行故障排除,看看它是否有任何问题。最终,在新代码中根本不需要它。再次感谢!我真的很感激!
    【解决方案2】:

    如果有人需要,我实际上可以通过以下方式让它工作。这比我最初的想法要简单一些。这将在找到的文本的第一个键处停止。要删除所有记录,只需删除“break”,它将删除包含所述文本或短语的所有键。

    $pattern = preg_quote($done, '/');
    $pattern = "/^.*$pattern.*\$/m";
    $arr_index = array();
    foreach ($json as $key => $contents) 
    {
        if(preg_match($pattern, $contents['Alerts'], $matches)) {
          unset($json[$key]);
          break; 
       }
    }
    $json = array_values($json);
    
    file_put_contents('myfile-test.json', json_encode($json));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多