1$myArr = array("apple", "banana", "mango", "jackfruit");
2
3$toJSON = json_encode($myArr);
4
5echo $toJSON;
1<?php
2 // JSON string
3 $someJSON = '[{"name":"Jonathan Suh","gender":"male"},{"name":"William Philbin","gender":"male"},{"name":"Allison McKinnery","gender":"female"}]';
4
5 // Convert JSON string to Array
6 $someArray = json_decode($someJSON, true);
7 print_r($someArray); // Dump all data of the Array
8 echo $someArray[0]["name"]; // Access Array data
9
10 // Convert JSON string to Object
11 $someObject = json_decode($someJSON);
12 print_r($someObject); // Dump all data of the Object
13 echo $someObject[0]->name; // Access Object data
14?>
15
1$personJSON = '{"name":"Johny Carson","title":"CTO"}';
2
3$person = json_decode($personJSON);
4
5echo $person->name; // Johny Carson
1//2 ways
2 //this is for string from $_REQUEST,$_POST to array
3$jsonText = $_REQUEST['myJSON'];
4$decodedText = html_entity_decode($jsonText);
5$myArray = json_decode($decodedText, true);
6
7//this is for json to array
8$assosiative_array = json_decode(json_encode($jsonText),true);
1<?php
2
3$myArr = '{
4 "neworder": {
5 "-newfolder": "NO",
6 "auth": {
7 "-extra": "8",
8 "-login": "login",
9 "-pass": "pass"
10 },
11 "order": {
12 "-orderno": "111111",
13 "barcode": "111111",
14 "sender": {
15 "company": "Ministry of Internal Affairs",
16 "person": "I. I. Ivanov",
17 "phone": "123-45-67",
18 "town": "Saint-Petersburg",
19 "address": "Petrovka Str., 38, room 35",
20 "date": "2021-03-22",
21 "time_min": "09:00",
22 "time_max": "14:00"
23 },
24 "receiver": {
25 "company": "Ministry of Internal Affairs",
26 "person": "Tom Wale",
27 "phone": "123-45-67",
28 "zipcode": "125480",
29 "town": {
30 "-regioncode": "78",
31 "-country": "RU",
32 "#text": "Saint-Petersburg"
33 },
34 "address": "Petrovka Str., 38, room 35",
35 "pvz": "124",
36 "inn": "1112223335",
37 "date": "2021-03-22",
38 "time_min": "09:00",
39 "time_max": "14:00",
40 "deliveryPIN": "1234",
41 "coords": {
42 "-lat": "55.680327",
43 "-lon": "37.604456"
44 }
45 },
46 "return": "NO",
47 "weight": "5.1",
48 "return_weight": "5.1",
49 "quantity": "2",
50 "paytype": "CASH",
51 "service": "2",
52 "return_service": "1",
53 "type": "3",
54 "return_type": "3",
55 "courier": "22",
56 "price": "387.5",
57 "deliveryprice": {
58 "-VATrate": "20",
59 "#text": "150"
60 },
61 "inshprice": "387.5",
62 "receiverpays": "NO",
63 "discount": "120",
64 "enclosure": "Kids toys",
65 "instruction": "Check in the presence of the buyer, sign acceptance certificate",
66 "department": "Accounting",
67 "pickup": "NO",
68 "acceptpartially": "NO",
69 "costcode": "cc12345",
70 "items": {
71 "item": [
72 {
73 "-extcode": "abc123",
74 "-quantity": "1",
75 "-mass": "0.2",
76 "-retprice": "37.5",
77 "-VATrate": "0",
78 "-barcode": "2345625213125",
79 "-textArticle": "1",
80 "-article": "1",
81 "-volume": "3",
82 "-origincountry": "AUT",
83 "-GTD": "321546654",
84 "-excise": "15.20",
85 "-suppcompany": "LLC \"Miller and Partners\"",
86 "-suppphone": "79161234567",
87 "-suppINN": "1112223334",
88 "-governmentCode": "11223311",
89 "#text": "Race car"
90 },
91 {
92 "-extcode": "abc124",
93 "-quantity": "2",
94 "-mass": "2",
95 "-retprice": "100",
96 "-inshprice": "100",
97 "-VATrate": "10",
98 "-barcode": "4645625213138",
99 "-article": "2",
100 "-length": "10",
101 "-width": "20",
102 "-height": "30",
103 "-origincountry": "004",
104 "#text": "Princess castle"
105 },
106 {
107 "-extcode": "abc125",
108 "-quantity": "3",
109 "-mass": "0.3",
110 "-retprice": "50",
111 "-inshprice": "50",
112 "-barcode": "2345625213126",
113 "-itemcode": "44123",
114 "-article": "3",
115 "-type": "1",
116 "#text": "Clay mass"
117 }
118 ]
119 },
120 "packages": {
121 "package": [
122 {
123 "-strbarcode": "ORD0000001",
124 "-mass": "1",
125 "-message": ""
126 },
127 {
128 "-strbarcode": "ORD0000002",
129 "-mass": "2.5",
130 "-message": "",
131 "-length": "10",
132 "-width": "20",
133 "-height": "30"
134 }
135 ]
136 },
137 "deliveryset": {
138 "-above_price": "100",
139 "-return_price": "1000",
140 "-VATrate": "10",
141 "below": [
142 {
143 "-below_sum": "500",
144 "-price": "500",
145 "-self-closing": "true"
146 },
147 {
148 "-below_sum": "2000",
149 "-price": "300",
150 "-self-closing": "true"
151 }
152 ]
153 },
154
155 "overall_volume": "81",
156 "userid": "123",
157 "groupid": "456"
158 }
159 }
160}';
161 print_r(json_decode($myArr)); ?>
162
1<?php
2 // Loop through Array
3 $someArray = ...; // Replace ... with your PHP Array
4 foreach ($someArray as $key => $value) {
5 echo $value["name"] . ", " . $value["gender"] . "<br>";
6 }
7
8 // Loop through Object
9 $someObject = ...; // Replace ... with your PHP Object
10 foreach($someObject as $key => $value) {
11 echo $value->name . ", " . $value->gender . "<br>";
12 }
13?>
14