php json_encode issue in utf-8 - php

I have following array in php
$array = array(
array('name'=>'abc', 'text'=>'اسلسصثصض صثصهخه عه☆anton & budi☆' ),
array('name'=>'xyz', 'text'=>'nice' ),
);
when i use json_encode, the result is different :
[
{
"name": "abc",
"text": "\u0627\u0633\u0644\u0633\u0635\u062b\u0635\u0636 \u0635\u062b\u0635\u0647\u062e\u0647 \u0639\u0647\u2606anton '<&>' budi\u2606"
},
{
"name": "xyz",
"text": "nice"
}
]
why the result is not like this ?
[
{
"name": "abc",
"text": "اسلسصثصض صثصهخه عه☆anton & budi☆"
},
{
"name": "xyz",
"text": "nice"
}
]
Thanks

Because PHP doesn't assume/allow a non-ASCII character set when encoding. Both results are equivalent when decoded.

Because it is encoded to JSON. With your first output, you are absolutely 100% certain that the browser will receive what you want it to. With the second, you cannot be sure.

Try this:
//for encode
json_encode(array_map('base64_encode', $array));
//and for decode
array_map('base64_decode', json_decode($array));
Hope it helps

Related

Filter response from json with php

for example, i have json result like this
[
{
"title": "x1",
"url": "domain.com"
},
{
"title": "x2",
"url": "example.com/"
},
{
"title": "x3",
"url": "site.com/cam"
},
]
The result of json will be randomly, i want to scrape ['x']['url'] path from that json, but the value of ['url'] must be "site.com/cam", and as you know, the results from json will change randomly, so I do not know which json path with value is "site.com/cam".
is there any suggestion? Thank you
Try this
$index = array_search('site.com/cam',array_column(json_decode($json),'url'));
Sandbox
https://3v4l.org/72tsZ
Output
$index = 2
Oh and there is a typo in your JSON
}, <-- remove comma here.
]

How to decode JSON to PHP array

I have a JSON output I would like to convert to a PHP array.
I tried with json_decode(), the problem is that there are arrays in the arrays.
They are the first weapons with PHP and I have never used JSON.
Can anyone help me?
Here is the JSON code:
{
"a": "text",
"b": "",
"c": [
{"name": "1", "id": "some text 1", "val": "x"},
{"name": "2", "id": "some text 2", "val": "x"},
{"name": "3", "id": "some text 3", "val": "x"}
]
}
I have to check that a variable is equal to name 1, contained in c, and if so, it also takes its id and val.
How can I do?
PS: I can compare two variables, but I do not know how to find name 1 and the corresponding data ..
$json = '{"a":"text","b":"","c":[{"name":"1","id":"some text 1","val":"x"},{"name":"2","id":"some text 2","val":"x"},{"name":"3","id":"some text 3","val":"x"}]}';
$json = json_decode($json,true);
echo $json["a"]."<br>";
echo $json["b"]."<br>";
echo $json["c"][1]["name"]."<br>";
echo "<pre>".print_r($json,true)."</pre>";

How to fetch PHP database values in Json?

I have to create Matrix tree view in my project. So am plan to use json. My question is how to fetch PHP values in Json ?. I did static matrix tree but i want dynamic. Thank you for advance.
My code is following:
<?php
include('db.php');
$select = mysql_query("select * from table1");
while($row = mysql_fetch_array($select))
{
?>
{
"name": "A", // Here database values come $row['name'];
"children": [
{
"name": "B",
"children": [
{"name": "B-1"}
]
},
{
"name": "C",
"children": [
{"name": "C-1", "size": 1082},
{"name": "C-2", "size": 1681}
]
},
{
"name": "D",
"children": [
{
"name": "D-1",
"children": [
{"name": "D-1 1", "size": 1302},
{"name": "D-1 2", "size": 6703}
]
},
{"name": "D-2", "size": 16540}
]
}
]
}
<?php
}
?>
In this example Im using the mysqli driver. Do not use the mysql driver.
you just need to convert your output data into a json object.
Its possible to extract all the rows at once which is going to give you a marginally less overhead.
$data = mysqli_fetch_all($select); // returns everything in an associative array
$json_data = json_encode($data); // converts that array to json.
if you need specific keys, then manipulate your query to rename columns as necessary eg.
$query = "select name as firstname from ....";
You can just retrieve data from your database and store it in arrays like you normally would. Then call PHP's built in function json_encode() to transform your PHP array into json (assuming your PHP array is well formed (which should be the case if you get it out of a database)).
You could argue that this is slower because you're iterating over the data twice instead of once, but it shouldn't matter, the complexity remains the same.

How to extract the JSON encoded POST parameters in PHP

I have passed JSON encoded parameters by POST which we have captured and decoded in another PHP file. I have used the following code to do that.
$entityBody = file_get_contents('php://input');
$entityBody = json_decode($entityBody, true);
I have passed the JSON encoded parameters as follows:
{
"id": "5",
"name": "abcd",
"imei": "1234"
}
Here my code works perfectly fine. However, I want to get all the parameters into a single object so that we can store them efficiently because otherwise there will be too many ifs and elses to get each parameter. So I have encoded the parameters as follows:
device = {
"id": "5",
"name": "abcd",
"imei": "1234"
}
But it is not working. Being new to JSON and PHP I do not know how to handle such cases. How can I achieve this?
use json_decode($_POST['device'], true) since your actually passing a parameter called 'device' to the php file.
You should pass json objects as follow:
{"device" : {
"id": "5",
"name": "abcd",
"imei": "1234"
}}
or if you have an array of devices
{"device" : [{
"id": "5",
"name": "abcd",
"imei": "1234"}
]}

Accessing data from a json structure

I am loading in a JSON feed from Facebook (snippet below).
{
"data": [
{
"id": "115972604762",
"from": {
"name": "Title Here",
"category": "Musicians",
"id": "20274769762"
},
"name": "It was an amazing gig!!",
"picture": "http://photos-h.ak.fbcdn.net/hphotos-ak-snc1/hs196.snc1/6616_115972604762_20274769762_2185148_6347071_s.jpg",
"source": "http://sphotos.ak.fbcdn.net/hphotos-ak-snc1/hs196.snc1/6616_115972604762_20274769762_2185148_6347071_n.jpg",
"height": 453,
"width": 604,
"images": [
{
I am loading it in using $data['pics'] = json_decode(file_get_contents('https://graph.facebook.com/'. $id .'/photos'));
How would I go about echo'ing out the from->name value to get the 'Title Here' value?
I think it should just be this:
$array = json_decode(file_get_contents('https://graph.facebook.com/'. $id .'/photos'));
echo $array["data"]["from"]["name"];
You can echo out the array using print_r($array) and then see the structure of your php array if it didn't work as expected.
First thing I would do is var_dump() the response, that would explain the exact structure of how PHP has decoded it. My guess is that $response['data']['from']['name'] might work.

Categories