PHP display array in array in array - php

I want know how to access to array in array in array in php.
The result with $user is this:
'pages':{'x','y','z','access':{'a':3,'b':6,'c':8,'contact':2}}
How I can access to contact, please?

First of all this is JSON (probably), so before we can access it with php we need to decode it using json_decode which will give you a php object. (i made it valid JSON)
$jsonString = '{"pages":{"x": 0,"y": 0,"z": 0,"access":{"a":3,"b":6,"c":8,"contact":2}}}';
$phpObject = json_decode($jsonString);
var_dump($phpObject->pages->access->contact);
// prints int(2)

Use json_decode to convert this to array in PHP (if it is JSON)

It seems your string missing {} around.
{
"id":1,"active":1,"canAccess":{"entities":{"1":{"name":"blablabla","services":{"‌​45":{"name":"xxx"}}}}},"blabla":null,"pages":{"acces":{"notifications":1,"contact‌​":2}}
}
then it is a valid JSON string.
var_dump(json_decode($string,true));

Related

Get Json Value with extra quotes using php

Get all value from json using json_decode. Given name is Juliet"es this json. If using json_decode this array will be change null value. how do i get this json to array
$jsonobj = '{"Name":"Juliet"es","Maths":37,"English":43}';
if anyone having any idea please post answer.
Try $convertedJson = json_decode(addslashes($jsonobj));.
But has suggested by #Barmar and #CBroe , always generated JSON strings from array or object with json_encode().

Not sure why I can't get the values of this array or decode the json

I am making a call to an API which should return a JSON array. I receive this:
Array
(
[0] => {"msg_id":"0t5OxT1ZP9VcF45L2","_text":"I want to reserve","entities":{"intent":[{"confidence":1,"value":"make_reservation","type":"value"}]}}
)
I know it's JSON because a) the docs say that's what I should be getting and b) I ran isJson($response) and got true.
I have tried to use json_decodebut the code just dies when I do (it errs saying it's expecting a string and got an array which makes sense but if I do json_encode that would just further encode the json from what I can understand).
As I understand it, I just need a way to traverse this array and get the "value:" key inside entities: intent:. However I can't figure out how to get it or where I'm wrong.
I have tried doing:
$val = $jsonArray[0]['entitites']['intent'][0]['value'] but nothing comes out.
You are trying to decode a PHP array that has encoded values.
You should try json_decode($jsonArray[0]) instead, so that you decode the value of the first array key, as that is the actual json string.
The data you posted is a php array where the value of the first element of the array is a json string.
json_decode($response[0]);

How to parse a json object into a url parameter string in PHP?

I have the following json encoded object:
{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}
and I want to convert this into url string so I can use it with my REST API function for example like this one:
search?search=asdadd%2C+United+Kingdom&type=tutor
I have found many functions in javascript to do this but I haven't been able to find anything in PHP. What is the function in PHP to do this?
The following query string:
?username=my_username&email=my_email&password=12345678&confirm_password=12345678
.. will turn into:
{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}
If you use json_enconde.
To reverse the process, you need to use json_decode as well as http_build_query.
First, turn the JSON into an associative array with json_decode:
$json = '{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}';
$associativeArray = json_decode($json, true);
Now use http_build_query with the new associative array we've built:
$queryString = http_build_query($associativeArray);
Result: username=my_username&email=my_email&password=12345678&confirm_password=12345678.

How receive json object from client into a php 2D array at server?

I want to fill a 2D array in php from a JSON object in javascript at client. Can anybody help me to do this functionality?
From the top of my head, without knowing anything about your code
you could use json_decode
use it like this:
$array = json_decode($_POST['data'], true);
#check the second parameter set to true, otherwise you will get a stdclass.
json_decode()
http://php.net/manual/en/function.json-decode.php

reading json encoded php array using ajax request

I have a php array with key, value pairs. I have json encoded it. And using ajax.Request i called the php file in which that array is there. Now i have to access both key and value pairs. Could anyone let me know how to do that?
You need to parse the JSON.
You can use the JSON library.
You can use a library method like jQuery's $.parseJSON().
If the JSON is trusted, you can use eval().
Code in javascript "json.js" is required. you can download this.
var votedCommentString = getCookie("votedComment"); // get value of voted comment cookie
votedComment = JSON.parse(votedCommentString); // extract json object into array
votedComment[votedComment.length] = id; // add more data in array
var cookieData = JSON.stringify(votedComment); // convert array into json object
setCookie("votedComment", cookieData, 365); // and again set cookie
In PHP you can access this in following way
$cookieData = (array)json_decode($_COOKIE["votedComment"]); // extract json object into array
print_r($cookieData);
Use
json_encode($array_variable) // to convert array in to json
As you said the array is in php file which is called via ajax, you can simply decode the json encoded string and retrieve the array with keys and values respectively.
Simply use json_decode() function to retrieve the array.

Categories