简体   繁体   中英

How i can this json data to write in php

I am sending request using curl. this is json data :

{
    "users": [
        {
            "userId": "123",
           "userLink" : "www.example .com",    
            "userType": "au"
        }
    ]
}

I am trying this but i need same above json:

 $post = array(
           "users" => array(
                "userId" => "123",
                "userLink" => "link",
                "userType" => "au"
           )
         );
      
       $out = array_values($post);
       $post = json_encode($out); 
       print_r($post);

You need an additional array dimension, and if you use brackets [ ] you can easily see how they look the same:

 $post = [
             "users" => [
                  [
                    "userId" => "123",
                    "userLink" => "link",
                    "userType" => "au"
                  ]
             ]
         ];
  
$post = json_encode($post); 

Also, don't use array_values ; that will remove the string key users .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM