将非关联数组传递给json_encode()时会发生什么?

My code is :

<?php 
    $arr = array();
    array_push($arr,"One","Two","Three");
    print_r($arr);
    echo '<br>';
    echo json_encode($arr);
?>

As you can see, I'm passing a non-associative array to json_encode(). The Output I get is

Array ( [0] => One [1] => Two [2] => Three )
["One","Two","Three"]

What exactly is the second line of the output? If we pass an associative array to json_encode(), what is returned is a JSON Object, but this array that is returned is definitely does not look like a JSON object. So what is it?

Also, is there a way to convert a non-associative array to a JSON Object using json_encode()?

If you're trying to get it in proper object notation, try this:

echo json_encode($arr,JSON_FORCE_OBJECT);

Output:

{"0":"One", "1":"Two", "2":"Three"}

Refer to json_encode() options

Hi You can also try this

echo json_encode((object)$arr);

Output

{"0":"One","1":"Two","2":"Three"}