panoply panoply - 3 months ago 7x
PHP Question

Retrieving all number row from JSON response

Using Guzzle I am hooking into an API. The API will provide me with a valid JSON object that contains values I require. I have tried several methods to retrieve an array of values located within the response but at this point can only get one per request via the number of the row:

Here is my current code:

public function index () {
//$value = "07332927917462,07332927917455";
$client = new Client();
$response = $client->get('',
[ 'query' => [
'secret' => '2acbefghijklmnop',
'created' => '2016-01-01']]);

$data = json_decode($response->getBody()->getContents());
$array = $data->products;
$collect = collect($array);
$unique = $collect->unique('ean');

return dd($unique[0]->ean);

In my
return dd($unique[0]->ean);
I am receiving only one EAN value (via the number), so it will look like this;


When I try to call it using
return dd($unique->ean);
I receive an error:

Laravel response

Bascially, what I need to do is get all the rows, not just one. so the request would be something like (for example purposes)
return dd($unique[all]->ean);
but I am not sure how to approach this?

Any help would be greatly appreciated.

Here is my response using just



What result do you exactly wanna get?

You could use "reduce" to get a single value from your collection.

Or, if you just want to get a list of "ean"'s, use "map" and return only the ean for every row:

$eans = $unique->map(function($item){
    return $item->ean;

There doesn't seem to be an "extract" method, but this should also work:

$eans = $unique->keyBy('ean')->keys();

For reference, here are all the collection methods: