0

How do I loop through the array to get the "converted_amount" values?

stdClass Object
(
[rows] => Array
    (
        [0] => stdClass Object
            (
                [components] => Array
                    (
                        [0] => stdClass Object
                           (
                                [amount] => 5033298.132349431
                                [count] => 1337
                                [rate] => 3.1398800
                                [converted_amount] => 1603021.9952863243
                            )

                        [1] => stdClass Object
                            (
                                [amount] => 458673.0026585825
                                [count] => 325
                                [rate] => 0.45260800
                                [converted_amount] => 1013400.4157520011
                            )

I have tried a foreach like this but it doesn't work. I think there should be something in-between components and converted_amount - maybe another foreach? I'm not sure.

foreach ($getexvolume as $vol) {
echo $vol['rows'][0]['components']['converted_amount'];}
1
  • Dunno if it will help, but your echo should be $vol->rows[0]->components[0]->converted_amount as far as I can tell. Commented Oct 23, 2018 at 13:31

4 Answers 4

1

You have an object instead if array. You must work with data as an object...

foreach ($getexvolume->rows as $row) {
    foreach ($row->components as $component) {
       echo $component->converted_amount;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you - this looks right but for some reason it's not echoing anything - it's blank
@Vikki, attach var_dump of $getexvolume variable, please
object(stdClass)#1137 (3) { ["result"]=> string(7) "success" ["count"]=> int(1) ["rows"]=> array(1) { [0]=> object(stdClass)#1136 (6) { ["components"]=> array(33) { [0]=> object(stdClass)#1135 (6) { ["base"]=> object(stdClass)#1134 (2) { ["currency"]=> string(3) "CNY" ["issuer"]=> string(34) "rPT74sUcTBTQhkHVD54WGncoqXEAMYbmH7" } ["counter"]=> object(stdClass)#1133 (1) { ["currency"]=> string(3) "XRP" } ["amount"]=> string(17) "5033298.132349431" ["count"]=> int(1337) ["rate"]=> string(9) "3.1398800" ["converted_amount"]=> string(18) "1603021.9952863243" }
0
   echo $vol->rows[0]->components[0]->converted_amount;

You are mixing array and object. Your output is an object so you have to access it like one otherwise if you want to treat it like an array you have to convert it to an array. As for now you can use the above code.

A better solution which i think fits your problem is that you loop around your nested array like:

foreach($vol->rows[0]->components as $data){
echo $data->converted_amount;
}

Comments

0

Try this:

foreach ($getexvolume->rows[0]->components as $vol) {
       echo $vol->converted_amount;
}

Comments

0

The object you have is a mix of Arrays and Objects. Arrays can be addressed as $array['value'] but Objects must be addressed as $object->value.

echo $vol->rows[0]->components[0]->converted_amount;

However since you have multiple components, you will need a nested loop:

foreach ($getexvolume as $vol)
{
  foreach($vol->rows as $row)
  {
    foreach($row->component as $component)
    {
      echo $component->converted_amount;
    }
  }
}

(pseudocode - not tested).

Ideally the variable would be normalised as a multidimensional array or nested object first so you don't have to worry about syntax.

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.