php检查foreach if match中的值在匹配行后输出值

php检查foreach if match中的值在匹配行后输出值,php,foreach,Php,Foreach,一些简单的代码,如果我有一个json数据。我想做一些事情,首先检查json数据中的匹配字符串,如果有,在匹配行后输出值,否则输出所有json数据 示例1,匹配字符串为9,在json数据中匹配,在匹配行7、3后输出值 $txt = '[{"a":"5"},{"a":"9"},{"a":"7"},{"a":"3"}]'; $array = json_decode($txt); $match_string = '9'; foreach ($array as $data){ echo

一些简单的代码,如果我有一个json数据。我想做一些事情,首先检查json数据中的
匹配字符串
,如果有,在匹配行后输出值,否则输出所有json数据

示例1,匹配字符串为9,在json数据中匹配,在匹配行7、3后输出值

$txt = '[{"a":"5"},{"a":"9"},{"a":"7"},{"a":"3"}]';
$array = json_decode($txt);
$match_string = '9';
foreach ($array as $data){      
    echo $data->a;//7, 3
}
Exapmle2,匹配字符串为2,在json数据中不匹配,输出所有值,5、9、7、3

$txt = '[{"a":"5"},{"a":"9"},{"a":"7"},{"a":"3"}]';
$array = json_decode($txt);
$match_string = '2';
foreach ($array as $data){      
    echo $data->a;//5, 9, 7, 3
}
如何做这个判断?我在foreach中执行类似操作,只需忽略匹配字符串:

if($match_string == $data->a){
  continue;//fut this in the foreach ,get 5, 7, 3, but I need 7, 3, next value from 9.
}
谢谢

$matched = false;
foreach($array as $data){
    if($matched)
        echo $data->a;
    $matched = ($data->a==$matchString) || $matched;
}
if(!$matched)
    foreach($array as $data)
        echo $data->a;
这是你的基本情况


这是您的基本情况。

您需要设置一个标志,告诉您是否找到匹配项:

$txt = '[{"a":"5"},{"a":"9"},{"a":"7"},{"a":"3"}]';
$array = json_decode($txt);
$match_string = "2";
$found = false;
foreach ($array as $data) {
    if ($found) {
        echo $data->a;
    } else if ($data->a === $match_string) {
        // If we set $found *after* we have the opportunity to display it,
        // we'll have to wait until the next pass.
        $found = true;
    }
}
if (!$found) {
    // Display everything
    foreach ($array as $data) {
        echo $data->a;
    }
}

您需要设置一个标志,告诉您是否找到匹配项:

$txt = '[{"a":"5"},{"a":"9"},{"a":"7"},{"a":"3"}]';
$array = json_decode($txt);
$match_string = "2";
$found = false;
foreach ($array as $data) {
    if ($found) {
        echo $data->a;
    } else if ($data->a === $match_string) {
        // If we set $found *after* we have the opportunity to display it,
        // we'll have to wait until the next pass.
        $found = true;
    }
}
if (!$found) {
    // Display everything
    foreach ($array as $data) {
        echo $data->a;
    }
}

下面的代码应该可以工作,只要$txt是一个有序列表而不是数组字典(对不起,我显然是幻觉)



但是,当所需的匹配是数组中的最后一个条目时,应该发生什么还不清楚。如果$txt是一个有序列表而不是数组字典,则不会有任何输出,因为该行已找到,但没有后续行。

如果$txt是一个有序列表而不是数组字典,则下面的代码应该可以工作(对不起,我显然是在产生幻觉)


但是,当所需的匹配是数组中的最后一个条目时,应该发生什么还不清楚。会发生的情况是没有任何输出,因为该行已找到,但没有后续行。

将其缩短

$txt = '[{"a":"5"},{"a":"9"},{"a":"7"},{"a":"3"}]';
$array = json_decode($txt);
$toFind = "9";
$mapped = array_map("current",$array);

if (!in_array($toFind,$mapped))
    echo implode(", ",$mapped);
else
    echo implode(", ",array_slice($mapped,array_search($toFind,$mapped)+1));
请注意,您不会使用该功能保留密钥
为性能而编辑,使其更短

$txt = '[{"a":"5"},{"a":"9"},{"a":"7"},{"a":"3"}]';
$array = json_decode($txt);
$toFind = "9";
$mapped = array_map("current",$array);

if (!in_array($toFind,$mapped))
    echo implode(", ",$mapped);
else
    echo implode(", ",array_slice($mapped,array_search($toFind,$mapped)+1));
请注意,您不会使用该功能保留密钥

为性能而编辑

我不得不说,这不是非常合理的代码
$data->a==$matchString
是一回事,但是
$data->a==$matchString | |$matched
。。。?我甚至不记得这里的优先顺序。相等比布尔连接具有更高的优先级。永远,用任何语言,永远。不过,如果你觉得不舒服的话。。。($data->a==$matchString)| |$matched。。。修复就像两个括号一样简单。@arxanas实际上,在大多数语言中,都有括号、一元运算符、乘法/除法、加法/减法、比较和/或。我不得不说,这不是很合理的代码
$data->a==$matchString
是一回事,但是
$data->a==$matchString | |$matched
。。。?我甚至不记得这里的优先顺序。相等比布尔连接具有更高的优先级。永远,用任何语言,永远。不过,如果你觉得不舒服的话。。。($data->a==$matchString)| |$matched。。。修复就像两个括号一样简单。@arxanas实际上,在大多数语言中,都有括号、一元运算符、乘法/除法、加法/减法、比较和/或。if子句缺少右大括号。此外,在我看来,他实际上并不想打印匹配的值…更喜欢这个答案,因为@FrankieTheKneeMan暗示了许多关于优先顺序的知识:-)@arxanas,请查看您的编辑代码,没错,只需执行
$array=json\u decode($txt)//move true
,谢谢。您缺少if子句的右括号。此外,在我看来,他实际上并不想打印匹配的值…更喜欢这个答案,因为@FrankieTheKneeMan暗示了许多关于优先顺序的知识:-)@arxanas,请查看您的编辑代码,没错,只需执行
$array=json\u decode($txt)//move true
,谢谢。+1因为这是一个很酷的解决方案,通过编写自己的映射方法,您可以很容易地将其概括。+1因为这是一个很酷的解决方案,通过编写自己的映射方法,您可以很容易地将其概括。