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

matthew's avatar

How to get a nested array to a collection in Laravel

I have a response from an api, that returns something like so :

array:162 [▼
"WAN" => array:4 [▼
"available" => "1153.65000000"
"onOrder" => "0.00000000"
"btcValue" => "1.05385928"
"btcTotal" => "1.05385928"
 ]
"ONT" => array:4 [▼
"available" => "961.95000000"
"onOrder" => "0.00000000"
"btcValue" => "0.83237534"
"btcTotal" => "0.83237534"
]
"NCASH" => array:4 [▼
"available" => "114194.00000000"
"onOrder" => "0.00000000"
"btcValue" => "0.59495074"
"btcTotal" => "0.59495074"
]
....

Which is all good and dandy, however i would much prefer to work with collection methods.

I have tried to convert to a collection with the collect() method as so .

$collection = collect($returned, true);

However i am still not able to traverse the data like so :

$collection->WAN->available

Instead i get the following in my $results :

Collection {#408 ▼
#items: array:162 [▼
"WAN" => array:4 [▼
  "available" => "1153.65000000"
  "onOrder" => "0.00000000"
  "btcValue" => "1.05085979"
  "btcTotal" => "1.05085979"
]
"ONT" => array:4 [▼
  "available" => "961.95000000"
  "onOrder" => "0.00000000"
  "btcValue" => "0.83170197"
  "btcTotal" => "0.83170197"
]
"NCASH" => array:4 [▼
  "available" => "114194.00000000"
  "onOrder" => "0.00000000"
  "btcValue" => "0.59951850"
  "btcTotal" => "0.59951850"
]
....

So after plenty of mooching about i found this :

$results = json_encode($returned);
$results = json_decode($results);

$collection = collect($results, true);

This turns out to be a step in the right direction, yet i am still not able to traverse the data in a full collection style.

I now have the following returned to me :

 Collection {#572 ▼
 #items: array:162 [▼
 "WAN" => {#409 ▼
  +"available": "1153.65000000"
  +"onOrder": "0.00000000"
  +"btcValue": "1.05270563"
  +"btcTotal": "1.05270563"
 }
 "ONT" => {#411 ▼
  +"available": "961.95000000"
  +"onOrder": "0.00000000"
  +"btcValue": "0.81842706"
  +"btcTotal": "0.81842706"
 }
 "NCASH" => {#412 ▼
  +"available": "114194.00000000"
  +"onOrder": "0.00000000"
  +"btcValue": "0.60294432"
  +"btcTotal": "0.60294432"
 }

Which i can traverse like so :

$collection['WAN']->available

How can i make it like :

$collection->WAN->available

I have now ran out of ideas, and i cannot seem to find anything that might address this problem.

Any ideas?

Thanks in advance.

0 likes
8 replies
jlrdw's avatar

I'd first study the code behind collection you we'll see it's just aray manipulation.

PHP arrays can be tricky.

matthew's avatar

Tricky for sure, i am fairly new to php and Laravel, trying to learn, will keep on tinkering, hopefully i will stumble upon something :(

jlrdw's avatar

Where does the array come from. I don't understand wanting to convert to a collection. In other words what are you trying to do once it's a collection?

Also see this https://laracasts.com/discuss/channels/laravel/recursive-conversion-of-array-to-collection?page=1

But you can do anything with an array as well, just a little tricky. I'd avoid working with arrays at all cost.

Just get DB results and work with that, all eloquent and querybuilder methods available.

Cronix's avatar

Are you only wanting to use the $collection->WAN->available syntax, or are there other collection methods you want to use?

If it's just the syntax, I wouldn't use collections, I'd just:

$results = json_decode(json_encode($returned));

dump($results->WAN->available); // "1153.65000000"
1 like
matthew's avatar

@jldrw The array is coming from an external API call, and after watching the Refactoring to Collections intro by Adam Wathan, i have decided this is the route i want to take for code readability.

Possibly i could save the api data to the database and then pull from there, however this api data is not necessary to store and will be updated within mere seconds.

@Cronix I just figured out i can do as you have said, but i do need to use collection methods, filter() , pluck() etc.

jlrdw's avatar

Did you see the link, it has a link to a github package that does what you need.

matthew's avatar

@jlrdw Yes i did see this before, but it not achieving what i am looking for. I am starting to think i am thinking about this wrong, and what i want to achieve is not possible :)

Rethink required it seems.

Please or to participate in this conversation.