PHP array and JSON conversion

This section describes

  • PHP array to JSON
  • JSON to PHP array

1. Conversion between array PHP and ISON

  • As of PHP5.2, PHP provides native functions to convert JSON strings to PHP structures.
    • You can convert an array into a JSON-formatted string using the function json_encode()
    • JSON strings can be converted into PHP arrays using the function json_decode()

PHP array to ISON

  • The PAP scatter group can be converted to a J5ON string using the function json_encode()
  • Syntax: string json_encode (mixedoplions =0])
    • Return value as a JSON string on success, false on failure
  • Parameter Description:
    • Parameter 1: Mandatory, the value to be encoded, can be any data type (usually arrays and objects) except for the resource type
    • Parameter 2: Optional
  • Example:
    • <? PHP // Convert a PHP array to a JSON string$a1 = array(
          "k1"= >"v1"."k2"= >"v2"."k3"= >"v3");$a2 = array(
          "k1"= >"v4"."k2"= >"v5"."k3"= >"v6");$aa = array($a1.$a2);$jsonStr = json_encode($aa); // Print directlyecho($jsonStr); ? >Copy the code

  • Interface encapsulation:
    • <? PHP // Convert a PHP array to a JSON string$a1 = array(
          "k1"= >"v1"."k2"= >"v2"."k3"= >"v3");$a2 = array(
          "k1"= >"v4"."k2"= >"v5"."k3"= >"v6");$aa = array($a1.$a2);$jsonStr = json_encode($aa);$result = array(
          "resultCode"= > 200,"message"= >"successed"."data"= >$aa);$resultStr = json_encode($result);echo($resultStr) ;
      
      ?> 
      Copy the code

  • Application scenario: In general, all PHP interfaces encapsulate the returned data as an array, convert it to a JSON string and return it to the client

3. JSON to PHP array

  • JSON strings can be converted into PHP arrays using the function json_decode()
  • Syntax: mixed json_decode (stringassoc])
    • Take a JSON-formatted string and convert it to a PHP variable
  • Parameter Description:
    • Parameter 1: a string in json format
    • Parameter 2: Defaults to false and returns a PHP object, or true returns a PHP array
  • Example:
    • <? PHP //JSON string to PHP array$json = '[{"name":"carry", "sex":"man"}, {"name":"jack", "sex":"man"}, {"name":"amy", "sex":"woman"}]' ;
      
      echo ($json) ;
      
      ?>
      Copy the code

  • Return PHP object
  • <? PHP //JSON string to PHP array$json = '[{"name":"carry", "sex":"man"}, {"name":"jack", "sex":"man"}, {"name":"amy", "sex":"woman"}]' ;
    
    $arr = json_decode($json); var_dump($arr) ;
    
    ?>
    Copy the code

  • Return PHP array
  • <? PHP //JSON string to PHP array$json = '[{"name":"carry", "sex":"man"}, {"name":"jack", "sex":"man"}, {"name":"amy", "sex":"woman"}]' ;
    
    $arr = json_decode($json.true); var_dump($arr) ;
    
    ?>
    Copy the code

  • Read the data
  • <? PHP //JSON string to PHP array$json = '[{"name":"carry", "sex":"man"}, {"name":"jack", "sex":"man"}, {"name":"amy", "sex":"woman"}]' ;
    
    $arr = json_decode($json.true); // Print the first person's nameecho($arr[0] ["name"]) ;
    
    ?>
    Copy the code

  • Application scenario: When the client sends a request, if the value of one parameter contains multiple values, for example, for multiple queries, the client sends the parameters in JSON string format