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

mahbubrn's avatar

how to get specific column data from multi array collection

I have collection which is:

[
{
    "date": "2022-10-09",
    "invoices": [
    {
        "title": "allowance",
        "credit": 200,
        "debit": null
    },
    {
        "title": "license",
        "credit": 100,
        "debit": null
    },
    {
        "title": "donation",
        "credit": null,
        "debit": 4000
    },
    {
        "title": "deposit",
        "credit": null,
        "debit": 2000
    }
    ],
    "balance": "7080.00"
},
{
    "date": "2022-10-07",
    "invoices": [
    {
        "title": "license",
        "credit": 2000,
        "debit": null
    }
    ],
    "balance": "1380.00"
},
{
    "date": "2022-10-08",
    "invoices": [
    {
        "title": "admission",
        "credit": null,
        "debit": 1000
    }
    ],
    "balance": "3380.00"
}
]

I want to get from here only debit and credit column. Has any direct way to get data like array column from here without loop ? I tried to array_column but its not working here because its a collection and If I do toArray() its not working because its return me data as date.

so I don't understand how to direct get column (credit/debit) from here in this collection.

0 likes
4 replies
Sinnbeck's avatar

How did it get in this shape in the first place? Seems like it would be easier to simply query the database

mahbubrn's avatar

@Sinnbeck in the database table has lots of column and data. and there structure has variations. So first I have get some data from table using select() and after that I made this collection by following different methods. Its not possible to get data directly from database.

Sinnbeck's avatar

@mahbubrn ok that sounds strange. Anyways. What debit and credit are you trying to get. Just the first? Or a sum for a certain day?

MohamedTammam's avatar
$onlyColumns = collect($yourArray)->map(fn($record) => $record['invoices'])->map(fn($r) => [
	'credit' => $r['credit'],
	'debit' => $r['debit'],
])

Please or to participate in this conversation.