How to get json object value in php
I have an url passing parameters use json_encode each values like follow: Show
It's returned a result as:
Can I use Thanks.
George G 7,13412 gold badges42 silver badges58 bronze badges asked Sep 14, 2012 at 17:08
2
answered Sep 14, 2012 at 17:12
Mihai IorgaMihai Iorga 38.6k15 gold badges107 silver badges106 bronze badges 4 json_decode will return the same array that was originally encoded. For instanse, if you
OR
These both will echo 84. I think json_encode and json_decode function names are self-explanatory... answered Sep 14, 2012 at 17:14
Vladimir HrabanVladimir Hraban 3,4333 gold badges24 silver badges43 bronze badges 0 What is JSON?JSON stands for JavaScript Object Notation, and is a syntax for storing and exchanging data. Since the JSON format is a text-based format, it can easily be sent to and from a server, and used as a data format by any programming language. PHP and JSONPHP has some built-in functions to handle JSON. First, we will look at the following two functions:
PHP - json_encode()The json_encode() function is used to encode a value to JSON format. ExampleThis example shows how to encode an associative array into a JSON object: $age = array("Peter"=>35, "Ben"=>37, "Joe"=>43); echo json_encode($age); Run Example » ExampleThis example shows how to encode an indexed array into a JSON array: $cars = array("Volvo", "BMW", "Toyota"); echo json_encode($cars); Run Example » PHP - json_decode()The json_decode() function is used to decode a JSON object into a PHP object or an associative array. ExampleThis example decodes JSON data into a PHP object: $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; var_dump(json_decode($jsonobj)); Run Example » The json_decode() function returns an object by default. The json_decode() function has a second parameter, and when set to true, JSON objects are decoded into associative arrays. ExampleThis example decodes JSON data into a PHP associative array: $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; var_dump(json_decode($jsonobj, true)); Run Example » PHP - Accessing the Decoded ValuesHere are two examples of how to access the decoded values from an object and from an associative array: ExampleThis example shows how to access the values from a PHP object: $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; $obj = json_decode($jsonobj); echo $obj->Peter; Run Example » ExampleThis example shows how to access the values from a PHP associative array: $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; $arr = json_decode($jsonobj, true); echo $arr["Peter"]; Run Example » PHP - Looping Through the ValuesYou can also loop through the values with a foreach() loop: ExampleThis example shows how to loop through the values of a PHP object: $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; $obj = json_decode($jsonobj); foreach($obj as $key => $value) { Run Example » ExampleThis example shows how to loop through the values of a PHP associative array: $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; $arr = json_decode($jsonobj, true); foreach($arr as $key => $value) { Run Example » How to get value of JSON in PHP?To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.
How can access JSON decoded data in PHP?PHP - Accessing the Decoded Values
$obj = json_decode($jsonobj);
What is File_get_contents PHP input?The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.
What is JSON decode in PHP?The json_decode() function is used to decode or convert a JSON object to a PHP object.
|