1<?php
2$data = ['name' => 'John', 'age' => 35];
3header('Content-type: Application/json');
4echo json_encode($data);
5
1$personJSON = '{"name":"Johny Carson","title":"CTO"}';
2
3$person = json_decode($personJSON);
4
5echo $person->name; // Johny Carson
1$postedData = $_POST["JSONfullInfoArray"];
2$tempData = str_replace("\\", "",$postedData);
3$cleanData = json_decode($tempData);
4var_dump($cleanData);
1json_encode used when PHP retrieve data and convert Array() to [] !!!!
2$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
3echo json_encode($arr);
4//output
5{"a":1,"b":2,"c":3,"d":4,"e":5}
6access from js file data.a, data.b,data.c...
7----------------------------------------------
8 $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
9json_decode($json, true); //true turns object to associative array;
10
11//output
12array(5) {
13 ["a"] => int(1)
14 ["b"] => int(2)
15 ["c"] => int(3)
16 ["d"] => int(4)
17 ["e"] => int(5)
18}
19
20
1// Checks if json
2function isJson($string) {
3 json_decode($string);
4 return json_last_error() === JSON_ERROR_NONE;
5}
6
7// example
8if (isJson($string) {
9 // Do your stuff here
10}