Home > Back-end >  how to change the object in collection to [] with conditional
how to change the object in collection to [] with conditional

Time:10-28

I have the collection below

[
    {
        "versions": [
            {
                "id": null,
                "name": null,
                "price": null
            }
        ]
    }
]

but I want if id,name, price null versions set to be [] like:

[
    {
        "versions": [   
        ]
    }
]

so anyone can help me to do this? I have searched for many solutions on the internet but no one works!! tks guys!!!

CodePudding user response:


$filtered = $collection->filter(function ($value, $key) {
    return $value != null;
});

CodePudding user response:

but in the collection, I also have "productName" like

[{
"productName": abc,
"version":[{
"id":null,
"name":null,
"price":null,
}]
}]

so how can I access the versions id to check if it null it changes to be []

  • Related