0

I have an array that looks like this

Array
(
    [0] => Array
        (
            [string] => something
            [name] => test
            [int] => 1
        )
    [1] => Array
        (
            [string] => another
            [name] => test2
            [int] => 43
        )
)

Which i want to convert to a specific JSON layout looking like this

[
    {
        "string": "something",
        "name": "test",
        "int": "1"
    },
    {
        "string": "another",
        "name": "test2",
        "int": "43"
    }
]

The problem is that, when i use the built in PHP function json_encode my output does not look the way i want but instead look like this

{"0":{"string":"something","name":"test","int":1},"1":{"string":"another","name":"test2","int":43}}

Basicly is there a way to remove the indexes from the conversion ?

2
  • 1
    Probably you have unset some items or your index keys are strings (even if they looks as numeric). Short solution: use json_encode( array_values( $yourArray ) ). Better solution: show the code in which you create the array. Commented Mar 20, 2016 at 22:32
  • use array_values stackoverflow.com/questions/20372982/… Commented Mar 20, 2016 at 22:34

1 Answer 1

0

Please try this snippet code :

$data = [];
$data[0] = array(
    "string" => "something",
    "name" => "test",
    "int" => 1
);
$data[1] = array(
    "string" => "another",
    "name" => "test2",
    "int" => 43
);

echo json_encode($data);

Output :

[{"string":"something","name":"test","int":1},{"string":"another","name":"test2","int":43}]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.