问题描述
{ ["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
我尝试将 PHP foreach 用于上述 json 解码输出。它总是给我 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;
}
}