PHP Object to Array Convert using JSON Decode

[ad_1]

PHP JSON decode; In this tutorial, we will learn how to convert json object to array in php using json_decode().

Through this tutorial, we will take some examples like convert JSON string to array PHP, convert JSON string to multidimensional array PHP and JSON decode and access object value PHP using the json_decode() function.

PHP: json_decode() | How to decode json to array in PHP

Defination:- The PHP json_decode() function, which is used to decode or convert a JSON object to a PHP object.

Syntax:

The syntax of JSON decode function is:-

json_decode(string, assoc, depth=500, options)

Parameters of json_decode() function

  • json: It holds the JSON string which need to be decode. It only works with UTF-8 encoded strings.
  • assoc: It is a boolean variable. If it is true then objects returned will be converted into associative arrays.
  • depth: It states the recursion depth specified by user.
  • options: It includes bitmask of JSON_OBJECT_AS_ARRAY, JSON_BIGINT_AS_STRING,, JSON_THROW_ON_ERROR.

Ex 1 – Convert JSON String to PHP array

Let’s take the first example, here we will convert the JSON string to PHP array using the json_decode() function. See the example below:

<?php

$jObject = '{"Sam":23,"John":32,"Joe":41,"Elvish":43}';

var_dump(json_decode($jObject, true));
?>

The output of the above code is: Array ( [Sam] => 23 [John] => 32 [Joe] => 41 [Elvish] => 43 )

Ex 2 – json String to Multidimensional Array PHP

Let’s take the second example of json_decode() function, Here we will take to convert json multiple objects to a multidimensional array in PHP. Let’s see example:

<?php

$jObject = '[
    {
        "title": "PHP",
        "category": "PHP"
    },
    {
        "title": "JSON PHP",
        "category": "PHP"
    },
    {
        "title": "JSON string to array php",
        "category": "php"
    }
]';
//json_decode multidimensional array php
print_r(json_decode($jObject, true));
?>

The output of the above code is:

Array ( 
   [0] => Array ( PHP Object to Array Convert using JSON Decode => PHP [category] => PHP ) 
   [1] => Array ( PHP Object to Array Convert using JSON Decode => JSON PHP [category] => PHP ) 
   [2] => Array ( PHP Object to Array Convert using JSON Decode => JSON string to array php [category] => php ) 
 ) 

Ex 3 – json decode and access object value php

Let’s take third example, in this example we will decode the json object first and after access the value by key. Let’s see the example:

<?php

$jsonObject= '{
    "title": "PHP JSON decode example",
    "category": "PHP"
}';


$res = json_decode($jsonObject);

// access title of reponse object
echo $res->title;

?>

The output of the above code is: ” PHP JSON decode example

Conclusion

PHP JSON decode; In this tutorial, we have learned how to convert json object to array in php using json_decode().

Recommended Posts

  1. PHP JSON encode – Convert Array To JSON or Object To JSON
  2. Functions: Remove First Character From String PHP
  3. Remove Specific/Special Characters From String In PHP
  4. How to Replace First and Last Character From String PHP
  5. Reverse String in PHP
  6. Array Push, POP PHP | PHP Array Tutorial
  7. PHP Search Multidimensional Array By key, value and return key
  8. remove duplicates from multidimensional array PHP
  9. PHP Remove Duplicate Elements or Values from Array PHP

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00