1

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.

6
  • Your json representation is wrong. It's neither an object nor an array. Is only the facilities relevant to your question? Is the results parts of an object or an array? Commented Oct 28, 2015 at 15:04
  • oh yes thanks for pointing. i copy from my fractal result so it got "results". @k0pernikus hi i've update with my answer. is it correct? yes i got my expected result. but should i keep doing that everytime i want to remove "data"? Commented Oct 28, 2015 at 15:06
  • Don't add answers within the question. And shouldn't you know if an answer works for you? ;) Your JSON is still broken. Check it here before posting: jsonlint.com Commented Oct 28, 2015 at 15:10
  • @k0pernikus jsonlint said my json is valid. thanks. Commented Oct 28, 2015 at 15:11
  • The formatting implied differently. Fixed that for you. Commented Oct 28, 2015 at 15:17

1 Answer 1

3

Looks like you could just do:

$results->facilities = $results->facilities->data

Working example:

<?php

$var = json_decode('{
    "id": "13",
    "name": "Example",
    "location_name": "NY",
    "phone": [
        { "number": "0617357707" }
    ],
    "facilities": {
        "data": [
            { "name": "AC" }, { "name": "Wi-Fi" }
        ]
    }
  }');
var_dump($var);

$var->facilities = $var->facilities->data;
var_dump($var);
Sign up to request clarification or add additional context in comments.

4 Comments

not working. "Trying to get property of non-object". and if i do this, i won't get the other result right?
I've added a working example. Obviously, you should first check if 'facilities' and 'data' exists before accessing that data. From the error you're getting, I'd say that 'facilities' does not exist in one of your results.
Hi. i managed to answer slightly different from yours. $result['facilities'] = $result['facilities']['data']; i don't know why i keep getting "trying to get propery of non object" using yours
This answer is expecting an object while your actual data structure is an array. json_decode will turn JSON object into PHP object, while json_encode encode both associative array and object into JSON object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.