How to convert JSON string to an array in PHP? Explain with example?

Posted by

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

we can convert JSON string to an array with the help of  json decode() function. This functon accepts the JSON string as the first parameter and a few additional parameters to control the process of converting JSON to PHP array.

we use the json decode() function to convert JSON string to an array in php.

JSON STRING TO ARRAY

example

<?php

	$json_string = '{"mp": "gwalior", "bhopal": "delhi", "mumbai": "gujrat"}';
			
	  echo "<pre>";
			
	print_r (json_decode($json_string));
			
	echo "</pre>";
			
			
?>

output

stdClass Object
(
    [mp] => gwalior
    [bhopal] => delhi
    [mumbai] => gujrat
)
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x