CakePHP hash extract from JSON data - php

I am trying to extract some part of a JSON formatted string in CakePHP using the Hash::extract method .
Here is the JSON string:
[[{"name":"Atkins Global","y":{"count":96,"type":"1"}},
{"name":"HFT","y":{"count":444,"type":"1"}},
{"name":"Catalyst","y":{"count":8,"type":"1"}},
{"name":"BGL","y":{"count":2,"type":"1"}}]]
Here is what I used in CakePHP to retrieve the above JSON:
debug(json_encode(Hash::extract($data['type'], '{s}')));
$data['type'] holds the data above.
Is there a way I can grab the type values only? Not sure how I can do this in CakePHP. Would appreciate if anyone can help.

From an answer by Roberto Atkinson, you have to do json_decode() first to convert a JSON string to a PHP array, instead of encoding an array to JSON. Reference: json_decode()
In your case, try:
//from JSON string to PHP array; force returned objects to be converted into associative arrays
$data['type'] = json_decode($data['type'], true);
//now you can extract type values from data
$output = Hash::extract($data['type'], '{s}'));
In addition, according to CakePHP official API documentation: Hash::extract(), the first parameter should be an array.
Parameters (array|ArrayAccess $data The data to extract from., string $path The path to extract.)

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().

How I can retrieve the length of a part of json object using PHP?

I have retrieved a PHP variable session that contains a JSON object. The JSON object is the following:
var jsonObject = {
"menu":{
"intro":{
"intosub":"sub_1"
},
"vis":{
"visub":"sub_2"
}
}
};
How can I know the length of a part of this JSON object? That is, I want to know how much elements that contain the key Intro and the same with key vis.
I have reviewed some post about this issue, and I found that I have to change the structure of JSON object, but this is not possible. Is there any other way in order to do this?
First, parse json to php associative array, use json_decode(your_var, true) . You will get a multidimensional array. Then use php count function on desired subarray

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.

PHP and JSON datatype in MySQL

Despite of recent implementation of JSON datatype to MySQL I can't find any word on it in the related PHP documentation.
My question is: will PHP automatically convert cells of JSON column to the actual values - arrays or literals - or will it provide just json-encoded strings. Like:
$sql_query = "SELECT JSON_ARRAY(1,2,3)";
$result = mysqli_query($sql_query);
$value = mysqli_fetch_row($result)[0];
// what is a $value? Array(1,2,3) or a string "[1,2,3]"
// do I have to use json_decode() to get an actual array here?
(Don't have MySQL 5.7 at hand right now, so can't check it myself.)
i think with serialize and unserialize you get whatever you want.
so just store all values using serialize and you can get that values with unserialize.
so just check serialize and unserialize.

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