Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

galironfydar's avatar

Best way to comsume this API response

So, I have the following API response:

{
  "resourceType" : "Parameters",
  "parameter" : [
    {
      "name" : "read_receipt",
      "part" : [
        {
          "name" : "documentIdentifier",
          "valueString" : "1"
        },
        {
          "name" : "read",
          "valueBoolean" : true
        }
      ]
    },
    {
      "name" : "read_receipt",
      "part" : [
        {
          "name" : "documentIdentifier",
          "valueString" : "2"
        },
        {
          "name" : "read",
          "valueBoolean" : false
        }
      ]
    }
  ]
}

I'm interested in the "read" column for a given id. I JSON decode the response and loop through the parameter currently.

$values = [];
foreach ($response->parameter as $parameter) {
    $part = $parameter->part;
    $values[$part[0]->valueString] = $part[1]->valueBoolean;
}

This is my solution to get the ID => readValue. But this seems super messy. There much be a better way? I suppose the part array indexes could also change as well, I have no control over what gets returned from the API. So I would need a way to get the read value from a documentIdentifier ID.

I'm not actually using Laravel for this project, so I'm trying to do this without reaching for collections. Am, I wasting my time. Is processing the response the only way to achieve this?

0 likes
5 replies
automica's avatar
automica
Best Answer
Level 54

@galironfydar this works.

$values = [];

foreach ($response->parameter as $parameter) {

    foreach ($parameter->part as $part) {

        if ($part->name == 'documentIdentifier') {
            $name = $part->valueString;
        }
        if ($part->name == 'read') {
            $value = $part->valueBoolean;
        }
    }

    if (isset($name) && isset($value)) {
        $values[$name] = $value;
    }
}

its a bit more robust that what you've got. Could probably be improved again if I can remember how to do it without collections :P

BTW if you did want to use collections but not use Laravel, you can just pull in the package https://github.com/tighten/collect

galironfydar's avatar

@automica Yeah that's a lot more robust. Checks for the key at least. It's been a long day, don't know why I didn't think of that :P

I don't understand by people make API responses like this. The keys have nothing of value, not sure if it could be processed better. All the API calls follow the same structure.

But, I'll see if someone can prove me wrong.

Thanks for the collect package, didn't know about that either.

automica's avatar

@galironfydar it feels a bit clumsy doing foreach but its easy to forget that in the end, what ever the single method you use is called, under the hood, they're either array_map or foreach.

galironfydar's avatar

Yeah, maybe I should hide it behind a class since all the requests essentially look the same.

$nicerArray = API::parse($response)

Please or to participate in this conversation.