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

ssuhat's avatar

PHP Flatten an multidimensional array

I've got an multidimensional array.

{
    "id": "13",
    "name": "Example",
    "location_name": "NY",
    "phone": [
        {
            "number": "0617357707"
        }
    ],
    "facilities": {
        "data": [
            {
                "name": "AC"
            },
            {
                "name": "Wi-Fi"
            }
        ]
    }
}
  

The problem I want to solve is at "facilities". Inside facilities, there is a data object. How can I remove the data without removing the content "data"?

Expected result:

{
    "id": "13",
    "name": "Example",
    "location_name": "NY",
    "phone": [
        {
            "number": "0617357707"
        }
    ],
    "facilities": [
        {
            "name": "AC"
        },
        {
            "name": "Wi-Fi"
        }
    ]
}
  

I've tried using array_walk or flatten but not getting it right.

0 likes
3 replies
ssuhat's avatar

@dudod thats working perfectly. If i have another array like facilities, maybe like galleries, phones inside it. I should keep do that?

Please or to participate in this conversation.