0

I Have a Json output like

{ ["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) 

I want to get the value with id as 19 and currency as 2. The output I require is 443.44

I was trying using PHP foreach for the above json decoded output. It always gives me the first value of 5.99

I am using the below code.

     $pid = 19;
     $cur = 2;

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

     return $getproductrrice[$pid];

I need to add one more condition so that I can pass currency and get the output as 443.44.

1
  • And what's the problem? Why not add that condition? Commented Jul 14, 2021 at 15:12

1 Answer 1

1

AS you are looping, test each product to see if it is one you want

$pid = 19;
$cur = 2;

foreach($products as $product){
    if ( $product->id == $pid && $product->currency == $cur ) {
        $getproductrrice[$product->id] = $product->monthly;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.