0

My code is now, I've made loads of changes

$query = '
    query{
  boards (ids: 1745132529){
    columns {
      id
      title
      type
    }
  }
}
';

$data = @file_get_contents($apiUrl, false, stream_context_create([
 'http' => [
  'method' => 'POST',
  'header' => $headers,
  'content' => json_encode(['query' => $query]),
 ]
]));

$responseContent = json_decode($data, true);
$res = (array_values($responseContent));

foreach ($res[0]["boards"][0]["columns"]as $key => $val) {
    if(is_array($val)) {
        echo "$key:\n<br>";
    } else {
        echo "$key => $val\n<br>";
    }
}

echo "<br><br>Ref: " . $res[0]["boards"][0]["columns"][2]["title"] . "<br><br>";

the output is now: 0: 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:

Ref: Client

if I try foreach ($res[0]["boards"][0]["columns"][0]["title"] as $key => $val) {

then I get just a blank output but $res[0]["boards"][0]["columns"][1]["title"] works if I just echo it.

How can I get all the values in my foreach - so I can get Name, Subitems, Client showing?

  • 1
    Read the accepted answere here: [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) to learn how to understand the data structure in front of you and how to access any part of it. – ADyson Apr 26 '22 at 15:32
  • thanks for pointing me that direction however im still unable to locate the correct output - I've tried several of the methods to get an output but I either get data: Array account_id : 10071830 or Array but I cant get it passed the first nested results... – TridentMarketingUK Apr 26 '22 at 16:04
  • Well what code did you write exactly? You have an array, then within that an associative array, then another array, then within that another associative array, then another array, and then the property you want is in the associative array below that (in the 3rd index of the last numeric array). – ADyson Apr 26 '22 at 16:08
  • @ADyson - I've found this which just produces a list of everything but I cannot work out how to pull out the right array `$jsonIterator = new RecursiveIteratorIterator( new RecursiveArrayIterator(json_decode($data, TRUE)), RecursiveIteratorIterator::SELF_FIRST); foreach ($jsonIterator as $key => $val) { if(is_array($val)) { echo "$key:\n
    "; } else { echo "$key => $val\n
    "; } }`
    – TridentMarketingUK Apr 26 '22 at 16:26
  • Why? Just access each element in turn, via the hierarchy I've described. Take it step by step if necessary so you can observe the result at each level and check you're on the right road. You don't need anything recursive - unless you're planning for an unpredictable data structure every time you run this code – ADyson Apr 26 '22 at 16:30
  • P.s don't put big chunks of code in the comments like that, it's hard to read...instead you can [edit] your question – ADyson Apr 26 '22 at 16:31
  • @ADyson - Ah I've just edited the original question - thanks so much for your help, I'm still learning :) – TridentMarketingUK Apr 27 '22 at 07:40
  • Thanks. So to be clear, are you expecting the data structure to be the same every time you run the code? Or are you iterating like that on purpose because you cannot predict the depth or precise structure in advance? – ADyson Apr 27 '22 at 08:21
  • @ADyson I believe the data will be in the same structure at all times, its pulling in via an API on Monday but it should be able to run the query to display the results in the same depth etc each time. – TridentMarketingUK Apr 27 '22 at 09:27
  • Ok., Well like I said, just go down the hierarchy one step at a time - `$responseContent[0]["boards"][0]["columns"][2]["type"]`. Demo: https://3v4l.org/qqDpn – ADyson Apr 27 '22 at 09:44
  • @ADyson - Thank you so much - I've updated the code in the question above asking how to generate all the data using the foreach... – TridentMarketingUK Apr 27 '22 at 11:34
  • Thanks, but please restore the data sample so we can see easily what we're dealing with – ADyson Apr 27 '22 at 11:36
  • `if I try foreach ($res[0]["boards"][0]["columns"][0]["title"] as $key => $val) {`...well title isn't an array, is it? It's clearly a string. If you want to loop through something, it'll need to be one of the items described as "array" in the data sample. – ADyson Apr 27 '22 at 11:37
  • @ADyson I tried `foreach ($res[0]["boards"][0]["columns"]as $key => $val) {` which should be the array but that didnt return anything either :( – TridentMarketingUK Apr 27 '22 at 11:44
  • Well that's a good start, but then `$val` each time will be the object containing all the fields for that record, so then you need to loop that to print each field within it. – ADyson Apr 27 '22 at 11:51
  • For example: https://3v4l.org/9ELhq – ADyson Apr 27 '22 at 11:52
  • P.S. `that didn't return anything either`, well unless you actually make some code to output something within the loop, it won't. If you do, of course you get errors because you can't immediately print an array visually (because PHP doesn't know how you want it to appear) - demo: https://3v4l.org/3SX93 . Maybe that's what you meant by that remark? – ADyson Apr 27 '22 at 11:53

0 Answers0