I'd first study the code behind collection you we'll see it's just aray manipulation.
PHP arrays can be tricky.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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.
Please or to participate in this conversation.