Good day!

The point is, I mean this API for our IOS-er, and it soars that the array that I give it does not have a name. It looks like this about me

[ {"name":"admin"} ] 

, and he wants it to be so

 [ user:{"name":"admin"} ] 

How do I name the array?

Thank!

  • one
    A couple of questions. 1 What is not like an array? 2. Why does he want invalid JSON? If you give a name to an array, it should look like this: {"user" : [ {"name" : "admin" } ]} - VAndrJ

2 answers 2

When converting to json, associative arrays are converted to objects, to get an array in json, the input must have an indexed array whose keys are in order, otherwise an object will be created. Therefore, the option [user:{"name":"admin"}] not possible, since it is associative, instead you can get only {"user":{"name":"admin"}}

 //{"name":"admin"} echo json_encode([ 'name' => 'admin', ]); //[{"name":"admin"}] echo json_encode([ [ 'name' => 'admin', ] ]); //{"user":{"name":"admin"}} echo json_encode([ 'user' => [ 'name' => 'admin', ], ]); //["name","admin"] echo json_encode([ 0 => 'name', 1 => 'admin', ]); //{"1":"name","0":"admin"} echo json_encode([ 1 => 'name', 0 => 'admin', ]); //{"user":[{"name":"admin"}]} echo json_encode([ 'user' => [ [ 'name' => 'admin', ], ], ]); 

    How do I name the array?

     {"user" : [ {"name" : "admin" } ]} 

    The answer is taken from the comment. thanks @VAndrJ