user2242965
user2242965

Reputation: 51

PHP indexes my array by adding keys, but I don't want it to?

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..

    ]
}

Upvotes: 0

Views: 399

Answers (2)

SHyx0rmZ
SHyx0rmZ

Reputation: 136

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"
            }
        }
    ]
}

Upvotes: 3

malcanso
malcanso

Reputation: 100

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?

Upvotes: 0

Related Questions