Jump to: navigation, search

PHP Array Convert to JSON

From w3cyberlearnings

Contents

PHP Array to JSON data

Convert an array to json by using json_decode() function.

Example 1:Simple Array

<?php

$actor = array(
	 'Greta Garbo',
	 'Andy Garcia',
	 'Ava Gardner',
	 'Lucy Johnson'
);

$json_actor = json_encode($actor);

print_r($json_actor);
?>

Output

[
"Greta Garbo",
"Andy Garcia",
"Ava Gardner",
"Lucy Johnson"
]

Example 2: Complex Array

<?php

$my_experience = array(
	 'work', 'life', 'happy',
	 'happy2' => array('go out', 'race car', 'programming'),
	 'programming' => 'php'
);

$json = json_encode($my_experience);

print_r($json);


?>

Output

{
0: "work",
1: "life",
2: "happy",
happy2: [
"go out",
"race car",
"programming"
],
programming: "php"
}

Related Links


--PHP Array Introduction-- PHP Access Array-- PHP Array Convert to Object-- PHP Array Convert to JSON-- PHP Array Convert to Class-- PHP Array Convert to String--

Navigation
Web
SQL
MISC
References