Handling data in a PHP JSON Object

Active3 hr before
Viewed126 times

8 Answers


If you use json_decode($string, true), you will get no objects, but everything as an associative or number indexed array. Way easier to handle, as the stdObject provided by PHP is nothing but a dumb container with public properties, which cannot be extended with your own functionality.,Connect and share knowledge within a single location that is structured and easy to search.,How do I work with data from this object. As an array? Only really need to extract data from the [name] values., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

You mean something like this?

< ? php

$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl, 0, null, null);
$json_output = json_decode($json);

foreach($json_output - > trends as $trend) {
   echo "{$trend->name}\n";
load more v

Both human and machine readability is high for the JSON data format. In this format, the data structure can be of the two structural format that is in the form of an object or array.,In the JSON introduction, we saw that the format can be varied based on the data structure. Let us see the examples for these two JSON formats object and array.,By sending TRUE to this optional parameter, the decoding result will be returned in a form of an array instead of an object. The default value of this param is FALSE as specified in the syntax.,In the above example, by removing the second parameter we can get the decoded data in the form of an object. The JSON to Object conversion code and the output is shown below.

JSON object format consists of a collection of key-value pairs of data. Below structure shows the example of a valid JSON object structure.

   "FIFA_World_Cup_Winners": {
      "2018": "France",
      "2014": "Germany",
      "2010": "Spain",
      "2006": "Italy",
      "2002": "Brazil"
load more v

Decoding JSON data is as simple as encoding it. You can use the PHP json_decode() function to convert the JSON encoded string into appropriate PHP data type. The following example demonstrates how to decode or convert a JSON object to PHP object.,You can also force json_encode() function to return an PHP indexed array as JSON object by using the JSON_FORCE_OBJECT option, as shown in the example below:,In PHP the json_encode() function is used to encode a value to JSON format. The value being encoded can be any PHP data type except a resource, like a database or file handle. The below example demonstrates how to encode a PHP associative array into a JSON object:,JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode(), respectively. Both functions only works with UTF-8 encoded string data.

   "book": {
      "name": "Harry Potter and the Goblet of Fire",
      "author": "J. K. Rowling",
      "year": 2000,
      "genre": "Fantasy Fiction",
      "bestseller": true
load more v

Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as true, false and null respectively. null is returned if the json cannot be decoded or if the encoded data is deeper than the nesting limit. , In the event of a failure to decode, json_last_error() can be used to determine the exact nature of the error. , When true, JSON objects will be returned as associative arrays; when false, JSON objects will be returned as objects. When null, JSON objects will be returned as associative arrays or objects depending on whether JSON_OBJECT_AS_ARRAY is set in the flags. ,json_encode() - Returns the JSON representation of a value

object(stdClass) #1 (5) {
    ["a"] = > int(1)["b"] => int(2)["c"] => int(3)["d"] => int(4)["e"] => int(5)

array(5) {
   ["a"] => int(1)["b"] => int(2)["c"] => int(3)["d"] => int(4)["e"] => int(5)
load more v

Many web applications use this data format to exchange data over the Internet.,This is how a JSON object looks like:,This is how the final JSON looks like:,exchange data over the Internet between HTTP services

   "Name": "Alex",
   "Age": 37,
   "Admin": true,
   "Contact": {
      "Site": "alexwebdevelop.com",
      "Phone": 123456789,
      "Address": null
   "Tags": [
load more v

PHP JSON tutorial shows how to work with JSON in PHP. , JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easily read and written by humans and parsed and generated by machines. The application/json is the official Internet media type for JSON. The JSON filename extension is .json. , In this tutorial, we have worked with JSON data in plain PHP, Symfony, Slim, and Laravel. , In the following example, we use JavaScript fetch API to get the JSON data from a PHP script.

< ? php

$data = ["falcon", "sky", "cloud", "orange", "wood", "forest"];

echo json_encode($data);
load more v

json_encode(mixed $value); transforms a PHP data structure into a JSON object,json_decode(string $json); transforms a JSON object into a PHP data structure,Since version 5.2, PHP features two built-in functions to transform your PHP data structures into JSON and vice versa:,Now, slapping any PHP data structure into the former function won't always work the way you expect it to. Here's what you need to pay attention to.

< ? php$myArray = array('Audi', 'Mercedes', 'VW');
echo json_encode($myArray); // ["Audi","Mercedes","VW"]
load more v

header('Content-Type: application/json');

$colors = array("red", "blue", "green");
echo json_encode($colors);
load more v