问题是使用
$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;
}
您应该使用编辑器的功能自动缩进代码,这会使类似的问题更加明显。