0

I currently have an array like this (All arrays in the question are in JSON format):

{
    "array":[
        "item",
        "item2",
        "item3"

       //etc..

    ]
}

I add an array to this array with ['array'][] = array("x"=>"y","z"=>"a");

and now my array looks like this:

{
    "array":{
        "0":"item",
        "1":"item2",
        "2":"item3",
        "newItem": {
            "x":"y",
            "z":"a"
        }

       //etc..

    }
}

How do I remove the "0" and the "1" from the array, so I just have this?:

{
    "array":[
        "item",
        "item2",
        "item3",
        "newItem": {
            "x":"y",
            "z":"a"
        }

       //etc..

    ]
}
1
  • How did the "newItem" key get into the array? I don't see it in your code examples Commented Oct 5, 2013 at 0:24

2 Answers 2

3

So let's take your original JSON as input

{
    "array":[
        "item",
        "item2",
        "item3"
    ]
}

You can use json_decode() to convert the JSON data to PHP. Judging from your array modification code you want to provide true as the second argument to json_decode()

$data = json_decode("{\"array\":[\"item\",\"item2\",\"item3\"]}", true);

You can then add data

$data['array'][] = array("x"=>"y","z"=>"a");

Now, you can use json_encode() to convert the PHP data to JSON again

echo json_encode($data);

This will print

{
    "array":[
        "item",
        "item2",
        "item3",
        {
            "x":"y",
            "z":"a"
        }
    ]
}

You can alternatively pass JSON_FORCE_OBJECT as the second argument to json_encode() to force conversion of arrays to objects and thus the creation of keys

echo json_encode($data, JSON_FORCE_OBJECT);

This alternative call will print

{
    "array":{
        "0":"item",
        "1":"item2",
        "2":"item3",
        "3":{
            "x":"y",
            "z":"a"
        }
    }
}

Note, that the call without JSON_FORCE_OBJECT does not have the array indices, while your JSON data has them. The reason is in the different way you modify your data. You probably use the following code

$data['array']['newItem'] = array("x"=>"y","z"=>"a");

By providing a key for this new item it has to be represented as an object in JSON. The array now contains an object and all the other items are promoted to objects, too.

To fix your problem you should modify your data like so

$data['array'][] = array('newItem' => array("x"=>"y","z"=>"a"));

This will give you

{
    "array":[
        "item",
        "item2",
        "item3",
        {
            "newItem":{
                "x":"y",
                "z":"a"
            }
        }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

0

I don't think you can have a PHP array without keys, and I don't think you can have JSON without keys. PHP will always add keys if you don't specify them http://php.net/manual/en/language.types.array.php.

What does "I currently have an array like this" mean? Does your JSON look like this? And does it work?

4 Comments

My JSON looks like the first one, and it works. I would like to add an array with a key to that array. However, when I add it to the first array, I get the second array (with the keys as indexes). I would like to remove the "0" and "1" keys and just have the third result.
JSON can contain arrays. The given JSON should validate.
Yes, all of the JSONs validate... I was just looking to have them without the keys.
If you introduce a new key into a PHP array you will end up with an associative array. Associative arrays will become json objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.