php foreach 附加条件

php foreach with additional condition

我有一个 Json 输出像

{ ["id"]=> int(1) ["name"]=> string(7) "SH-Mini" ["currency"]=> int(1) ["monthly"]=> string(4) "4.99" } [79]=> object(stdClass)#301 (4) 
{ ["id"]=> int(1) ["name"]=> string(7) "SH-Mini" ["currency"]=> int(2) ["monthly"]=> string(4) "345.23" } [80]=> object(stdClass)#300 (4) 
{ ["id"]=> int(1) ["name"]=> string(7) "SH-Mini" ["currency"]=> int(6) ["monthly"]=> string(5) "4.01" } [81]=> object(stdClass)#299 (4) 
{ ["id"]=> int(19) ["name"]=> string(8) "SH-Basic" ["currency"]=> int(1) ["monthly"]=> string(4) "5.99" } [82]=> object(stdClass)#298 (4) 
{ ["id"]=> int(19) ["name"]=> string(8) "SH-Basic" ["currency"]=> int(2) ["monthly"]=> string(6) "443.44" } [83]=> object(stdClass)#297 (4) 
{ ["id"]=> int(19) ["name"]=> string(8) "SH-Basic" ["currency"]=> int(6) ["monthly"]=> string(4) "5.03" } [84]=> object(stdClass)#296 (4) 

我想获取id为19,货币为2的值,我需要的输出是443.44

我正在尝试对上述 json 解码输出使用 PHP foreach。它总是给我第一个值 5.99

我正在使用下面的代码。

     $pid = 19;
     $cur = 2;

     foreach($products as $product){
         $getproductrrice[$product->id] = $product->monthly;
     }

     return $getproductrrice[$pid];

我需要再添加一个条件,以便我可以传递货币并获得输出 443.44。

在循环的同时,测试每个产品,看看它是否是您想要的

$pid = 19;
$cur = 2;

foreach($products as $product){
    if ( $product->id == $pid && $product->currency == $cur ) {
        $getproductrrice[$product->id] = $product->monthly;
    }
}