0

What i'm trying to do is get the each array key and then associate the first "price" and "quantity" with each array key. My array:

{
  "32557580": {
    "bids": [
      {
        "price": 2500,
        "quantity": 38265661
      },
      {
        "price": 2439,
        "quantity": 57414444
      },
      {
        "price": 2381,
        "quantity": 1092179
      },
      {
        "price": 2174,
        "quantity": 27140648
      }
    ],
    "offers": [
      {
        "price": 2564,
        "quantity": 33634591
      },
      {
        "price": 2597,
        "quantity": 27842125
      },
      {
        "price": 2632,
        "quantity": 925092
      },
      {
        "price": 2667,
        "quantity": 3565173
      },
      {
        "price": 2778,
        "quantity": 27589980
      }
    ]
  },
  "32557581": {
    "bids": [
      {
        "price": 4854,
        "quantity": 33786947
      },
      {
        "price": 4808,
        "quantity": 22881344
      },
      {
        "price": 4762,
        "quantity": 2513747
      },
      {
        "price": 4717,
        "quantity": 2650000
      },
      {
        "price": 4587,
        "quantity": 15714786
      }
    ],
    "offers": [
      {
        "price": 4950,
        "quantity": 31749492
      },
      {
        "price": 5000,
        "quantity": 3193999
      },
      {
        "price": 5051,
        "quantity": 2292463
      },
      {
        "price": 5102,
        "quantity": 34770816
      },
      {
        "price": 5128,
        "quantity": 2605693
      }
    ]
  },
  "32557582": {
    "bids": [
      {
        "price": 2532,
        "quantity": 60354703
      },
      {
        "price": 2500,
        "quantity": 113667648
      },
      {
        "price": 2439,
        "quantity": 5125100
      },
      {
        "price": 2222,
        "quantity": 120803051
      }
    ],
    "offers": [
      {
        "price": 2564,
        "quantity": 1492990
      },
      {
        "price": 2597,
        "quantity": 22121811
      },
      {
        "price": 2632,
        "quantity": 42119270
      },
      {
        "price": 2667,
        "quantity": 43680406
      },
      {
        "price": 2703,
        "quantity": 1176966
      }
    ]
  }
}

Example:

Id: 32557580
Price: 2500
Quantity: 38265661
Id: 32557581
Price: 4854
Quantity: 33786947

etc..

This is what i got so far

$obj = json_decode($result,true);
$all_keys = array_keys($obj);
foreach ($all_keys as $key => $value) {
    echo 'Id: '.$value.'<br>';
}

Output from this is:

Id: 32557580
Id: 32557581
Id: 32557582

I'm not really sure where to go from here, i have tried look at multiple questions and answers elswere. I tried adding another foreach inside the one i got but i only got the price and quantity from the first array key.

All help with getting me on the right track is appreciated.

1 Answer 1

2

Just loop the main array and get the first from bids and use that:

foreach($obj as $key => $value) {
    $first = reset($value['bids']);
    echo "ID: $key<br />";
    echo "Price: " . $first['price'] . "<br />";
    echo "Quantity: " . $first['quantity'] . "<br />";
}

Or use the index, which in this case is 0 but may not always be depending on the JSON:

foreach($obj as $key => $value) {
    echo "ID: $key<br />";
    echo "Price: " . $value['bids'][0]['price'] . "<br />";
    echo "Quantity: " . $value['bids'][0]['quantity'] . "<br />";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh. That makes sense. Thank you. :)

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.