Articles

PHP Object to Array Convert using JSON Decode

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:

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:

The output of the above code is:

Array ( 
   [0] => Array ( PHP Object to Array Convert using JSON Decode => PHP PHP => PHP ) 
   [1] => Array ( PHP Object to Array Convert using JSON Decode => JSON PHP PHP => PHP ) 
   [2] => Array ( PHP Object to Array Convert using JSON Decode => JSON string to array php PHP => 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:

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

Images mentioned above related to PHP are either copyright property of respective image owners.

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is the owner of RabinsXP who is constantly working for increasing the Internet of Things (IoT) in Nepal. He also builds android apps and crafts beautiful websites. He is also working with various social services. The main aim of Lamichhane is to digitally empower the citizens of Nepal and make the world spiritually sound better both in terms of technology and personal development. Rabins is also the first initiator of Digital Nepal.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button