JSON (Javascript Object Notation) is one of the most popular and lightweight data-interchange format. It is very easy for machine to parse and for human to read. JSON is a text format which is completely language independent.
JSON is built on two structures:
- Name/Value Pair : In different languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
- Ordered list of values : In most languages, this is realized as an array, vector, list, or sequence
If you want to read more about JSON you can visit json.org
Also Read:
- Create and Drop MYSQL Database using PHP
- PHP Login and Remember me Script using Cookie
- PHP Download, Crop and Save Image in a Folder from URL
PHP JSON:
PHP has json extension with 4 functions which are:
Function | Description |
---|---|
json_encode ($value, $options, $depth) | It returns the JSON representation of a value.
|
json_decode($json, $assoc, $depth, $options) | Decodes json string.
|
json_last_error_msg() | Returns the error of the last json_encode() or json_decode() call. |
json_last_error() | Returns the last error occurred. |
PHP Encode and Decode Mysql Data into JSON
I will use both MYSQLi procedural and object oriented in my example to encode and decode mysql data into json. I will create a demo database with customers table and fetch records.
Create Database:
1 2 3 |
create database demo; |
Create Table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
CREATE TABLE `customers` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `first_name` VARCHAR(255) NULL DEFAULT NULL, `last_name` VARCHAR(255) NULL DEFAULT NULL, `address` VARCHAR(255) NULL DEFAULT NULL, `email` VARCHAR(255) NULL DEFAULT NULL, `phone` VARCHAR(255) NULL DEFAULT NULL, `country` VARCHAR(255) NULL DEFAULT NULL, `city` VARCHAR(255) NULL DEFAULT NULL, PRIMARY KEY (`id`) ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB AUTO_INCREMENT=1 ; |
Insert Records in Table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (30, 'Daphne', 'Corkery', '339 Angel Cliffs Suite 830\nLake Chaunceyburgh, NY 07754-0015', 'casey.wintheiser@example.net', '259.748.0377', 'Indonesia', 'Powlowskifurt'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (31, 'Melvin', 'Ward', '891 Marvin Path\nCorkeryshire, MD 03561-1051', 'edmund.huel@example.com', '879-914-5175 x884', 'Holy See (Vatican City State)', 'Carleeville'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (32, 'Chaim', 'Gorczany', '7814 Reichel Corner Suite 259\nRebaport, MI 56331-5271', 'welch.ena@example.com', '(438) 742-7902 x61501', 'Czech Republic', 'North Bartholome'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (33, 'Glenna', 'Blick', '787 Felicita Meadows Suite 154\nLeonardoborough, CA 20969', 'considine.jon@example.com', '1-462-734-4174', 'Thailand', 'Helenaville'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (34, 'Stephon', 'Kerluke', '32673 Windler Cove\nKutchfort, MD 49339-7545', 'kihn.keshaun@example.com', '1-629-944-0402 x3905', 'Azerbaijan', 'New Davionport'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (35, 'Gregory', 'Kuvalis', '7875 Brigitte Hills\nDianamouth, NC 99458', 'dasia.schimmel@example.org', '(847) 719-0192', 'Turks and Caicos Islands', 'Beckershire'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (36, 'Nathan', 'Hane', '8778 Nader Branch Suite 521\nNorth Germanville, MT 91405', 'crist.mckayla@example.org', '(316) 229-3693 x1819', 'Germany', 'Port Dorthystad'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (37, 'Mazie', 'Tromp', '93613 Becker Courts\nSouth Shayneton, MT 63141-1374', 'dudley10@example.org', '1-557-810-3554', 'Guadeloupe', 'Lake Vicenta'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (38, 'Carolyn', 'Anderson', '921 Gwendolyn Manor\nEast Alfonso, NC 47005', 'bosco.myrtis@example.com', '203-860-5133 x0686', 'Guernsey', 'Mohammedbury'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (39, 'Chloe', 'Murray', '73183 Adolf Avenue Suite 755\nWest Cassie, SD 97551-5896', 'bschiller@example.org', '368.775.6726', 'Holy See (Vatican City State)', 'New Lelandside'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (40, 'Wilfrid', 'Watsica', '486 Jerry Fords Suite 256\nDanialshire, ND 98164', 'qdickinson@example.com', '729-334-7231', 'Mongolia', 'Port Horaceside'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (41, 'Lesley', 'Cronin', '4383 Haley Flats\nRoyalmouth, LA 67647', 'sterry@example.com', '1-867-221-0503', 'Spain', 'New Blaise'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (42, 'Alvina', 'Kihn', '894 Josefa Inlet\nNorth Darrinstad, AZ 62534-4682', 'braun.kendall@example.org', '1-928-653-3678 x80094', 'Aruba', 'North Emil'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (43, 'Jeromy', 'Parisian', '90405 Kulas Hollow\nNitzscheville, MD 20391', 'qlind@example.net', '1-283-883-3495 x00732', 'Palestinian Territories', 'Port Ariel'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (44, 'Charlotte', 'Ankunding', '646 Wintheiser Route\nPort Okey, IN 00324-6544', 'davon03@example.net', '1-802-256-0061 x409', 'Romania', 'Rosalynberg'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (45, 'Aylin', 'Prohaska', '417 Rosamond Point\nNew Ron, OH 00669', 'federico47@example.com', '+1 (943) 947-3756', 'Honduras', 'Beerside'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (46, 'Dock', 'Pacocha', '291 Kilback Port\nPattiechester, NC 44357', 'grant.gina@example.net', '467.677.3837 x940', 'Liechtenstein', 'Celineland'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (47, 'Kale', 'Prohaska', '63357 Guillermo Fort\nEast Dannie, AZ 24764', 'fern78@example.org', '832-443-7011', 'Mayotte', 'Fadelstad'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (48, 'Devyn', 'Mitchell', '583 Urban Cape Apt. 064\nWest Korbin, PA 28722-8553', 'edison.monahan@example.com', '(657) 400-2755 x035', 'Egypt', 'Harberton'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (49, 'Madisen', 'Crooks', '440 Roselyn Locks\nEast Haskell, OH 48441', 'erica.ankunding@example.net', '1-945-849-2727', 'Saint Helena', 'Noemiborough'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (50, 'Omari', 'Haley', '298 Verlie Curve Apt. 763\nNew Magnolia, NV 74636-8955', 'kareem.johns@example.org', '216.746.9391 x80494', 'Guadeloupe', 'Quitzonberg'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (51, 'Karen', 'Waelchi', '389 Alena Street Suite 814\nEast Rylee, VT 06976-3147', 'darrick.kuvalis@example.org', '327.553.6823', 'Bermuda', 'Michelland'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (52, 'Hadley', 'Herman', '568 Murphy Highway\nReingerborough, WV 53936', 'chane@example.net', '1-805-840-0864', 'Mongolia', 'Lake Linnea'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (53, 'Trystan', 'Champlin', '65667 Gisselle Flat Suite 198\nSchadenport, NH 07053-6974', 'pcartwright@example.net', '273-684-0325 x50686', 'Luxembourg', 'Zboncakburgh'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (54, 'Ulises', 'Orn', '3026 Boyle Ferry\nLake Lialand, CO 71400-3367', 'eondricka@example.com', '1-665-585-0846', 'British Virgin Islands', 'West Titofurt'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (55, 'Virginie', 'Smith', '20456 Casper Brook\nGottliebbury, DC 65297-6576', 'kertzmann.iliana@example.net', '569.646.0127', 'Mexico', 'Ebertbury'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (56, 'Valentina', 'Ankunding', '819 Cummings Freeway\nSouth Lionel, CO 79787-5561', 'walter.dulce@example.com', '(332) 790-5751 x01397', 'Bouvet Island (Bouvetoya)', 'West Jana'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (57, 'Rylee', 'Kovacek', '2675 Brennon Flats\nPort Alyshamouth, OR 50225-0336', 'windler.magdalen@example.net', '541.615.4727 x7679', 'Saint Helena', 'Doyleview'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (58, 'Leslie', 'Bode', '392 Pablo Haven\nAmbroseburgh, AR 70034', 'epagac@example.org', '1-779-614-8339 x75231', 'Svalbard & Jan Mayen Islands', 'New Izaiah'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (59, 'Nat', 'Heidenreich', '3510 Vandervort Cove Suite 834\nNew Ewell, ID 15004-7348', 'ohaag@example.org', '(967) 453-9989', 'Macedonia', 'West Modestoshire'); INSERT INTO `customers` (`id`, `first_name`, `last_name`, `address`, `email`, `phone`, `country`, `city`) VALUES (60, 'Fletcher', 'Willms', '91362 Turner Mission Apt. 548\nFlatleyside, ME 36522', 'chyna68@example.net', '553.857.5290', 'Uruguay', 'Lubowitzfurt'); |
MYSQLI Procedural Encode JSON Data:
In the above example I just picked 10 records from customers table and put them in $array
array. Then I used json_encode function to convert $array
into json.
Output:
1 2 3 |
[{"id":"1","first_name":"Alvina","last_name":"Champlin","address":"3605 Larson Meadow\nCasandraville, VT 73844-0293","email":"schiller.thomas@example.net","phone":"+15718182740","country":"Congo","city":"North Brody"},{"id":"2","first_name":"Jeanette","last_name":"Johns","address":"38265 Stehr Course Apt. 958\nSouth Berthafort, AK 47740","email":"miracle72@example.com","phone":"1-761-330-5631 x8750","country":"Isle of Man","city":"North Josephine"},{"id":"3","first_name":"Felicity","last_name":"Walsh","address":"392 Kautzer Mountain\nAronstad, FL 34152","email":"zulauf.melody@example.net","phone":"1-528-589-0695","country":"Belize","city":"Katrineberg"},{"id":"4","first_name":"Alexandria","last_name":"Goodwin","address":"7192 Dietrich Unions Suite 178\nNorth Fatimamouth, AL 54635-8353","email":"reilly94@example.org","phone":"1-380-388-6370 x8648","country":"Guam","city":"Collierville"},{"id":"5","first_name":"Francis","last_name":"Paucek","address":"19435 Cicero Well\nWest Jarrell, MA 51035-4794","email":"juana.fahey@example.org","phone":"1-995-393-1347 x986","country":"Bouvet Island (Bouvetoya)","city":"New Eliseohaven"},{"id":"6","first_name":"Jerrod","last_name":"Kilback","address":"359 Roob Fall Suite 950\nEast Jaida, ME 49692-0730","email":"cecelia.kub@example.net","phone":"836.979.1645 x343","country":"India","city":"Boyerhaven"},{"id":"7","first_name":"Royal","last_name":"Waelchi","address":"29651 Moen Shores Apt. 710\nWest Ninabury, AL 37371-4514","email":"bkoss@example.org","phone":"937-421-6387 x182","country":"Netherlands Antilles","city":"Lake Lorinemouth"},{"id":"8","first_name":"Prudence","last_name":"Padberg","address":"11889 Cathy Stravenue\nWalshfort, IA 12348-3147","email":"amelia24@example.net","phone":"(802) 955-6535 x78249","country":"Peru","city":"Albinaborough"},{"id":"9","first_name":"Wilmer","last_name":"Stoltenberg","address":"324 Izaiah Courts\nShyannville, MN 58488","email":"auer.amber@example.org","phone":"+1.425.306.7890","country":"United Kingdom","city":"East Helmer"},{"id":"10","first_name":"Bulah","last_name":"Rohan","address":"4859 Blake Islands Suite 518\nKleinton, OR 86307","email":"brayan99@example.net","phone":"(412) 934-6791","country":"Georgia","city":"Adahview"}] |
MYSQLI Procedural Decode JSON Data:
By default json_decode function decodes json string as an object. If you want to convert json string into array then you have to pass TRUE as a second argument in json_decode function.
JSON decode as an Object:
1 2 3 4 5 6 |
<?php $jsonDecodeObject = json_decode($jsonEncode); print_r($jsonDecodeObject); ?> |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
Array ( [0] => stdClass Object ( [id] => 1 [first_name] => Alvina [last_name] => Champlin [address] => 3605 Larson Meadow Casandraville, VT 73844-0293 [email] => schiller.thomas@example.net [phone] => +15718182740 [country] => Congo [city] => North Brody ) [1] => stdClass Object ( [id] => 2 [first_name] => Jeanette [last_name] => Johns [address] => 38265 Stehr Course Apt. 958 South Berthafort, AK 47740 [email] => miracle72@example.com [phone] => 1-761-330-5631 x8750 [country] => Isle of Man [city] => North Josephine ) [2] => stdClass Object ( [id] => 3 [first_name] => Felicity [last_name] => Walsh [address] => 392 Kautzer Mountain Aronstad, FL 34152 [email] => zulauf.melody@example.net [phone] => 1-528-589-0695 [country] => Belize [city] => Katrineberg ) ) |
JSON decode as an Array:
1 2 3 4 5 6 7 |
<?php $jsonDecodeArray = json_decode($jsonEncode,true); print_r($jsonDecodeArray); ?> |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
Array ( [0] => Array ( [id] => 1 [first_name] => Alvina [last_name] => Champlin [address] => 3605 Larson Meadow Casandraville, VT 73844-0293 [email] => schiller.thomas@example.net [phone] => +15718182740 [country] => Congo [city] => North Brody ) [1] => Array ( [id] => 2 [first_name] => Jeanette [last_name] => Johns [address] => 38265 Stehr Course Apt. 958 South Berthafort, AK 47740 [email] => miracle72@example.com [phone] => 1-761-330-5631 x8750 [country] => Isle of Man [city] => North Josephine ) [2] => Array ( [id] => 3 [first_name] => Felicity [last_name] => Walsh [address] => 392 Kautzer Mountain Aronstad, FL 34152 [email] => zulauf.melody@example.net [phone] => 1-528-589-0695 [country] => Belize [city] => Katrineberg ) ) |
MYSQLI Object Oriented Encode JSON Data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php $db = new mysqli('localhost','root','','demo'); if($db->connect_error) { die('Database Connection Error: '.$db->connect_errno); } $qry = "select * from customers limit 10"; $rs = $db->query($qry); $rows = $rs->fetch_all(MYSQLI_ASSOC); $jsonEncode = json_encode($rows); echo $jsonEncode; ?> |
In the above code, I fetched 10 records from customers table and store them in $rows
variable. On this point you may be wondering how can I fetch all records without while loop. In mysqli object oriented, there is a function fetch_all() which fetch all the records in given query.
Output:
1 2 3 |
[{"id":"1","first_name":"Alvina","last_name":"Champlin","address":"3605 Larson Meadow\nCasandraville, VT 73844-0293","email":"schiller.thomas@example.net","phone":"+15718182740","country":"Congo","city":"North Brody"},{"id":"2","first_name":"Jeanette","last_name":"Johns","address":"38265 Stehr Course Apt. 958\nSouth Berthafort, AK 47740","email":"miracle72@example.com","phone":"1-761-330-5631 x8750","country":"Isle of Man","city":"North Josephine"},{"id":"3","first_name":"Felicity","last_name":"Walsh","address":"392 Kautzer Mountain\nAronstad, FL 34152","email":"zulauf.melody@example.net","phone":"1-528-589-0695","country":"Belize","city":"Katrineberg"},{"id":"4","first_name":"Alexandria","last_name":"Goodwin","address":"7192 Dietrich Unions Suite 178\nNorth Fatimamouth, AL 54635-8353","email":"reilly94@example.org","phone":"1-380-388-6370 x8648","country":"Guam","city":"Collierville"},{"id":"5","first_name":"Francis","last_name":"Paucek","address":"19435 Cicero Well\nWest Jarrell, MA 51035-4794","email":"juana.fahey@example.org","phone":"1-995-393-1347 x986","country":"Bouvet Island (Bouvetoya)","city":"New Eliseohaven"},{"id":"6","first_name":"Jerrod","last_name":"Kilback","address":"359 Roob Fall Suite 950\nEast Jaida, ME 49692-0730","email":"cecelia.kub@example.net","phone":"836.979.1645 x343","country":"India","city":"Boyerhaven"},{"id":"7","first_name":"Royal","last_name":"Waelchi","address":"29651 Moen Shores Apt. 710\nWest Ninabury, AL 37371-4514","email":"bkoss@example.org","phone":"937-421-6387 x182","country":"Netherlands Antilles","city":"Lake Lorinemouth"},{"id":"8","first_name":"Prudence","last_name":"Padberg","address":"11889 Cathy Stravenue\nWalshfort, IA 12348-3147","email":"amelia24@example.net","phone":"(802) 955-6535 x78249","country":"Peru","city":"Albinaborough"},{"id":"9","first_name":"Wilmer","last_name":"Stoltenberg","address":"324 Izaiah Courts\nShyannville, MN 58488","email":"auer.amber@example.org","phone":"+1.425.306.7890","country":"United Kingdom","city":"East Helmer"},{"id":"10","first_name":"Bulah","last_name":"Rohan","address":"4859 Blake Islands Suite 518\nKleinton, OR 86307","email":"brayan99@example.net","phone":"(412) 934-6791","country":"Georgia","city":"Adahview"},{"id":"11","first_name":"Roselyn","last_name":"Lockman","address":"4345 Nitzsche Mountains Suite 107\nEast Lavonne, PA 92446","email":"berneice.reinger@example.net","phone":"1-710-786-9448","country":"Monaco","city":"Eugeniahaven"},{"id":"12","first_name":"Lora","last_name":"Ryan","address":"51854 Stefan Mission Apt. 834\nNew Adolfotown, HI 18148","email":"carter.smitham@example.net","phone":"(280) 692-7491","country":"Bulgaria","city":"New Wilhelmineville"},{"id":"13","first_name":"Princess","last_name":"Sanford","address":"816 Welch Land Suite 770\nLuciusshire, MS 42451-5941","email":"wuckert.macey@example.net","phone":"937.706.9143","country":"Comoros","city":"Uptonton"},{"id":"14","first_name":"Yadira","last_name":"Luettgen","address":"16318 Bonita Bridge Apt. 709\nAthenaport, IL 26511","email":"zstrosin@example.net","phone":"+1-294-375-1907","country":"Vanuatu","city":"Zulaufborough"},{"id":"15","first_name":"Treva","last_name":"Jones","address":"10151 Shawna Shore Apt. 033\nMarjoriemouth, KY 22030-1574","email":"effie21@example.org","phone":"1-391-670-3346 x25463","country":"Cayman Islands","city":"Lake Flavio"},{"id":"16","first_name":"Lincoln","last_name":"Bode","address":"1414 Reinger Rest Apt. 707\nSouth Kayachester, AR 39004-3831","email":"richard81@example.net","phone":"+1.963.935.0710","country":"Turks and Caicos Islands","city":"North Adam"},{"id":"17","first_name":"Ilene","last_name":"Marks","address":"4231 Minnie Ferry\nMedhurstburgh, WY 70923-2667","email":"ludwig20@example.org","phone":"896.479.9846 x3199","country":"Heard Island and McDonald Islands","city":"New Yesseniaburgh"},{"id":"18","first_name":"Francisca","last_name":"Rowe","address":"853 Albina Groves\nAsamouth, NM 27737-1890","email":"odie.schowalter@example.com","phone":"528.595.8020","country":"Myanmar","city":"Murazikfort"},{"id":"19","first_name":"Alexie","last_name":"Langosh","address":"80539 Barrows Stream Apt. 651\nNorth Joshuahaven, CO 01023-3558","email":"fahey.cassie@example.net","phone":"(658) 878-7250 x92671","country":"Cocos (Keeling) Islands","city":"Pollyton"},{"id":"20","first_name":"Macey","last_name":"Towne","address":"3383 Andrew Lights\nSouth Lilianechester, DC 59528-5129","email":"wkozey@example.org","phone":"217.941.6891 x200","country":"Monaco","city":"Wolfshire"}] |
JSON decode as an Object:
1 2 3 4 5 6 |
<?php $jsonDecodeObject = json_decode($jsonEncode); print_r($jsonDecodeObject); ?> |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
Array ( [0] => stdClass Object ( [id] => 1 [first_name] => Alvina [last_name] => Champlin [address] => 3605 Larson Meadow Casandraville, VT 73844-0293 [email] => schiller.thomas@example.net [phone] => +15718182740 [country] => Congo [city] => North Brody ) [1] => stdClass Object ( [id] => 2 [first_name] => Jeanette [last_name] => Johns [address] => 38265 Stehr Course Apt. 958 South Berthafort, AK 47740 [email] => miracle72@example.com [phone] => 1-761-330-5631 x8750 [country] => Isle of Man [city] => North Josephine ) [2] => stdClass Object ( [id] => 3 [first_name] => Felicity [last_name] => Walsh [address] => 392 Kautzer Mountain Aronstad, FL 34152 [email] => zulauf.melody@example.net [phone] => 1-528-589-0695 [country] => Belize [city] => Katrineberg ) ) |
JSON decode as an Array:
1 2 3 4 5 6 |
<?php $jsonDecodeArray = json_decode($jsonEncode,true); print_r($jsonDecodeArray); ?> |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
Array ( [0] => Array ( [id] => 1 [first_name] => Alvina [last_name] => Champlin [address] => 3605 Larson Meadow Casandraville, VT 73844-0293 [email] => schiller.thomas@example.net [phone] => +15718182740 [country] => Congo [city] => North Brody ) [1] => Array ( [id] => 2 [first_name] => Jeanette [last_name] => Johns [address] => 38265 Stehr Course Apt. 958 South Berthafort, AK 47740 [email] => miracle72@example.com [phone] => 1-761-330-5631 x8750 [country] => Isle of Man [city] => North Josephine ) [2] => Array ( [id] => 3 [first_name] => Felicity [last_name] => Walsh [address] => 392 Kautzer Mountain Aronstad, FL 34152 [email] => zulauf.melody@example.net [phone] => 1-528-589-0695 [country] => Belize [city] => Katrineberg ) ) |