0

Consider one simple array.

<?php
$a = array('a','b','c');
?>

how can i generate a json array as given below.

{
'0':'a',
'1':'b',
'2':'c'
}

Normal json_encode function returns ['a','b','c']

2 Answers 2

5

Pass JSON_FORCE_OBJECT as second argument to the json_encode function.

http://php.net/manual/en/function.json-encode.php

json_encode($a, JSON_FORCE_OBJECT);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

Working perfectly. I tried

<?php
        $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

        $new_arr = json_encode($arr);

        print_r($new_arr);
        ?>

Output

{"a":1,"b":2,"c":3,"d":4,"e":5} 

-

Thanks

1 Comment

Its not what the OP wants.He had already specified the sample output.

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.