Php json object to array

I have a json string i which has a object in one one there is one json array & another object i want to first the json array then convert into php array & other json object into the php variable.Please tell me how to do this. I have stdclass but i am not able to get accurate data.

Json String

{
    "data": [
    {
        "ques_name": "no",
        "ques_id": "1"
    }, {
        "ques_name": "yes",
        "ques_id": "2"
    }, {
        "ques_name": "no",
        "ques_id": "3"
    }, {
        "ques_name": "yes",
        "ques_id": "4"
    }, {
        "ques_name": "no",
        "ques_id": "5"
    }, {
        "ques_name": "yes",
        "ques_id": "6"
    }, {
        "ques_name": "no",
        "ques_id": "7"
    }
    ],
    "UserId": 163
}

I used following code to get the array but it gives me array of size 14 where as size should be 7

    $params=$_GET['params'];
    $arr=array();
    $decode=json_decode($params);
    $arr=$decode->data;
    print_r($arr);


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 JSON

PHP has some built-in functions to handle JSON.

First, we will look at the following two functions:

  • json_encode()
  • json_decode()

PHP - json_encode()

The json_encode() function is used to encode a value to JSON format.

Example

This 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 »

Example

This 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.

Example

This 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.

Example

This 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 Values

Here are two examples of how to access the decoded values from an object and from an associative array:

Example

This 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;
echo $obj->Ben;
echo $obj->Joe;
?>

Run Example »

Example

This 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"];
echo $arr["Ben"];
echo $arr["Joe"];
?>

Run Example »


PHP - Looping Through the Values

You can also loop through the values with a foreach() loop:

Example

This 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) {
  echo $key . " => " . $value . "
";
}
?>

Run Example »

Example

This 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) {
  echo $key . " => " . $value . "
";
}
?>

Run Example »



  1. HOME
  2. PHP

By Parth Patel on Sep 09, 2021

The json_decode() is an inbuilt function in php which is used to convert JSON encoded string to appropriate variable in php.

Generally, json_decode is used to convert json to array in PHP but it has other usecases as well.

Syntax:

json_decode(
    string $json,
    ?bool $associative = null,
    int $depth = 512,
    int $flags = 0
): mixed

Parameters:

  • json: The JSON string passed to be decoded to php variable. This function only works with UTF-8 encoded strings.
  • assoc: It is a boolean variable. When true is passed, JSON object will be converted into associative array; when false is passed, JSON object will be returned as stdClass object; when NULL is passed, it will return associative array or object depending on the JSON_OBJECT_AS_ARRAY flag.
  • depth: Maximum nesting depth of the JSON to be decoded.
  • flags: It includes bitmask of JSON_OBJECT_AS_ARRAY, JSON_BIGINT_AS_STRING,, JSON_THROW_ON_ERROR and other JSON constants.

Return:

The json_decode() function decodes the JSON string to appropriate PHP type based on the parameter.

When true, false, or null is passed for JSON, the function returns same true, false, or null respectively. If the JSON failed to be decoded or the JSON is deeper than given depth then null gets returned.

How to decode json to array in PHP

Example:



// Store JSON data in a PHP variable
$json = '{"John":20,"Harry":30,"Dave":40,"Tony":50}';
 
var_dump(json_decode($json,true));

?>

Output:

array(4) {
  ["John"]=>
  int(20)
  ["Harry"]=>
  int(30)
  ["Dave"]=>
  int(40)
  ["Tony"]=>
  int(50)
}

How to decode json to object in PHP

If you don't pass second parameter, or pass false, json_decode() will parse JSON to stdClass object therefore you can use "→" arrow notation to access the object properties.

Example:



// Store JSON data in a PHP variable
$json = '{"email":""}';

$obj = json_decode($json);
print $obj->email;

?>

Output:

Conclusion

Here's the simple explanation to how json_encode() works and how you can parse json to array or class objects in PHP.

Adios

How to JSON to array PHP?

PHP and JSON.
The json_encode() function is used to encode a value to JSON format..
The json_decode() function is used to decode a JSON object into a PHP object or an associative array..
The json_decode() function returns an object by default. ... .
You can also loop through the values with a foreach() loop:.

What is JSON format in PHP?

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.

What json_decode () function will return?

The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.

Does json_decode return array?

json - json_decode does not return array in php - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.