Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I wanted this type of json using php and mysql. I tried a lot but the json which I get are duplicate data and very complex.
JSON I need:
{"data":[
{"Maharashtra":[
{"Mumbai":[
{"place_name":"Gateway of India"},
{"place_name":"Marine Lines"},
{"place_name":"Juhu"}
]},
{"Pune":[
{"place_name":"Singhad"},
]}
}],
{"Goa":[
{"Panji":[
{"place_name":"panji"}
]}
]}
}]}
PHP code:
<?php
require('database.php');
$counter=0;
$state = "SELECT distinct(s.state_name)
, ac.city_name
FROM all_state s, all_city ac
WHERE s.s_id = ac.state_id;";
$resultState = $conn->query($state);
$return_arr['data'] = array();
while($row = $resultState->fetch_assoc()){
$getStateName = $row['state_name'];
$getCityName = $row['city_name'];
$state_array[$getStateName] = array($getCityName);
array_push($return_arr['data'], $state_array);
}
echo json_encode($return_arr);
your query only returns states and cities so data like:
{"place_name":"Gateway of India"},
{"place_name":"Marine Lines"},
{"place_name":"Juhu"}
is not there: to push the data you are getting in to propper array use this:
while($row = $resultState->fetch_assoc()){
$stateArray[$row['state']][$row['city']] = array(); // the empty array is there to push your remaining data in to
}
$result = array('data' => $stateArray);
echo json_encode($result);
UPDATE TO ANSWER COMMENT:
You can add details to your array like this (or any other way, there are many, this is the most basic):
$stateArray[$stateYouWant][$cityYouWant]['place_names'] = array('Gateway of India', 'Marine Lines', 'Juhu');
This will give ytou something like:
{"Maharashtra":[
{"Mumbai":[
{"place_names":[
"Gateway of India",
"Marine Lines",
"Juhu"]}
]},
The variation you want:
{"Mumbai":[
{"place_name":"Gateway of India"},
{"place_name":"Marine Lines"},
{"place_name":"Juhu"}
]},
Is not possible because json is actually a string representation of associative array (or JS object) and that means you can not have same name keys on the same array level.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
My API call returns the following JSON output before json_decode:
{
"projects": [
{
"project_id": 00000001,
"name": "A title",
"price": "0.99",
"country": "US",
"platform_types": [
"android_phone",
"ios_phone",
"ios_tablet",
"android_kindle",
"android_tablet",
"desktop"
],
"comment": "A text of a comment"
}
{
"project_id": 00000002,
"name": "Another title",
"price": "1.03",
"country": "US",
"platform_types": [
"android_phone",
"ios_phone",
"ios_tablet",
"android_kindle",
"android_tablet",
"desktop"
],
"comment": "Another text of a comment"
}
]
}
The following code parses the multi-level json and shows the whole projects list:
$json = file_get_contents($url, false, $context);
$result = json_decode($json, true);
foreach($result['projects'] as $project) {
$project_id = $project['project_id'];
$name = $project['name'];
$price = $project['price'];
$country = $project['country'];
#no values for the fields, that's why commented
#$android_phone = $project['platform_types']['android_phone'];
#$ios_phone = $project['platform_types']['ios_phone'];
#$ios_tablet = $project['platform_types']['ios_tablet'];
#$android_kindle = $project['platform_types']['android_kindle'];
#$android_tablet = $project['platform_types']['android_tablet'];
#$desktop = $project['platform_types']['desktop'];
$comment = $project['comment'];
echo $project_id,'<br>',$name,'<br>',$price,'<br>',$country,'<br>',$comment,'<br>';
}
I got the following output:
00000001
A title
0.99
US
A text of a comment
00000002
Another title
1.03
US
Another text of a comment
The questions are:
How to list available device types (the fields have names only and no values)?
How to filter the array based on certain criteria (price must be equal or higher $1.00)?
How to filter elements based on fields without values (show projects just for Android devices)?
Question 1
to list all the available device types per project as a json you should access it's corresponding tree
foreach($result['projects'] as $project) {
$project_device_types = $project['plateform_type'];
echo '<pre>'.$project_device_types.'<pre>';
}
Question 2
to filter the array use [array_filter][1] in
$filtered = array_filter($result['projects'], function($project) {
if($project['price'] >= 90) {
return true
}
});
// use it instead of the $result variable
foreach($filtered as $project) {
$project_id = $project['project_id'];
$name = $project['name'];
$price = $project['price'];
}
Question 3
you can follow the exact pattern as the second question and that is by using array_filter so eg. let's say you want only the android devices
array_filter($result['projects'], function($project) {
if(in_array($project['platform_types'], "Android")) { // check if the plateform type include android by using [in_array][1]
return true
}
});
NB: in_array is case sensitive so make sure that the capitalization is correct
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
The website http://tracker.ets2map.com/v2/fullmap contains the data I wish to retrieve every 10 seconds using the following code:
<?php
$content = file_get_contents("http://tracker.ets2map.com/v2/fullmap");
$result = json_decode($content);
print_r($result);
I wish to get the info in the form
[{"name":"\u0420\u041e\u0421\u0421\u0418\u042f","x":-11409,"y":11749,"id":73469},{"name":"NikJZX","x":-12305,"y":-6239,"id":401390},{"name":"Efremov777","x":-12390,"y":-5636,"id":1755318}, ...]
But using the code above the data is all scrambled
Thanks for any help.
Edit:
I realise now that scramble is very misleading.
I have fixes my original problem, but now the data returns with a / in it.
Eg.
"{\"Trucks\": {\"1743637\": {\"name\": \"benanayan2\", \"h\": 0.55, \"p_id\": \"2043\", \"server\": 2, \"mp_id\": 1743637, \"t\": 1511366599, \"online\": true, \"y\": -27679, \"x\": 7203}, \"1229525\": {\"name\": \"BoeinGTranSErtu\u011frul15\", \"h\": 2.81, \"p_id\": \"868\", \"server\": 2, \"mp_id\": 1229525, \"t\": 1511366599, \"online\": true, \"y\": -9884, \"x\": -8956}, \"1717847\": {\"name\": \"[VNS] PH\u1ea0M \u0110I\u1ec6P\", \"h\": 2.2, \"p_id\": \"176\", \"server
<?php
$content = file_get_contents("http://tracker.ets2map.com/v2/fullmap");
$result = json_decode($content,true);
echo '<pre>';
print_r($result);
I think you mean that your print_r messes the data in printing. Try my code to get the data print clear and readable. And also when you decode to array , use true , otherwise you end up with an std class object not easily parsed.
This question already has answers here:
Sort multi-dimensional array by specific key
(6 answers)
Closed 6 years ago.
I have the following JSON:
[{
"id":"1",
"testimony":"I recently went to Perceptive Mind on behalf of a client to get assistance in the web-development of an online invoicing system.\n\t\t\t\t\t\t\t\t\t\t\t\t\tThe work they did for me was done quickly and with great care. What I found most notable about Perceptive Mind was that it worked with my budget, I didn't feel overcharged and had plenty of tech support and communication throughout the project.\n\t\t\t\t\t\t\t\t\t\t\t\t\tI will definitely be using Perceptive Mind in the future!",
"author":"Miles Hellyer",
"title":"Founder",
"company":"Chalk Marketing",
"sort_order":"200"
},
{
"id":"2",
"testimony":"Just like to thank Nick for\r\n\t\t\t\t\t\t\t\t\t\t\t\t\this outstanding and professional work, he has created a great website\r\n\t\t\t\t\t\t\t\t\t\t\t\t\twhich suits our needs. He was efficient, helpful and very\r\n\t\t\t\t\t\t\t\t\t\t\t\t\tfriendly! Definitely would recommend using his services",
"author":"Jennifer Mouhaweg",
"title":"Manager",
"company":"The Lockout Guys",
"sort_order":"400"
}]
And here's my code to decode that JSON and put it into a PHP array:
$testimonials_array;
$testimonials_table_json = json_decode(file_get_contents('../app/database/testimonials.JSON'),true);
foreach($testimonials_table_json as $row) {
$testimonial = $this->model('TestimonialModel');
$testimonial->testimony = $row['testimony'];
$testimonial->author = $row['author'];
$testimonial->title = $row['title'];
$testimonial->company = $row['company'];
$testimonials_array[] = $testimonial;
}
return $testimonials_array;
My question is, if I add in a $testimonial->sort_order= $row['sort_order']; can I use that to sort the $testimonial array? And is there a way to sort it before the foreach loop?
Use usort:
usort($testimonials_table_json, function ($a, $b) {
return $b['sort_order'] - $a['sort_order'];
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to send the string response of a particular request from ftp Interface. I am using below code..
if($s == 'Successfull') {
$rid = 's'.$request_id;
}
if($s == 'Unsuccessfull') {
$rid = 'e'.$request_id;
}
echo "<b style='font-size: 16px;color:#59AB8F;'>Your Request ID:".$rid.".......<b>\n";
printf("<br>");
header($rid);
Why not set a response code with a JSON response?
if($s == 'Successfull'){
http_response_code(201);
$data = [ 'id' => $request_id ];
}
else {
http_response_code(400);
}
if (isset($data)) {
header('Content-Type: application/json');
echo json_encode($data);
}
That would be the normal way of handling an API POST response. 201 signifies a resource was created, 400 signifies a bad request (you can change this if a different response code matches better). Then the client can read in the JSON data and get the id.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I use some website that give me information about IP but the information that that website return in JSON and I don't know the JSON. I want to use this to check the user if it is from IR do something but I dont know how to use JSON in php,
Here is the JSON that the website return:
{"address":"0.0.0.0.0","country":"IR","stateprov":"somewhere ","city":"Tehr\somewhere (somewhere)"}
I want to save the country in a variable and add this code to my website:
<?php
if($country == 'IR'){
//Do somethong
}
$country is the country name that return from the website,
You'll need to use json_decode().
$s = '{"address":"0.0.0.0.0","country":"IR","stateprov":"somewhere ","city":"Tehrsomewhere (somewhere)"}';
$d = json_decode($s);
Which returns:
stdClass Object
(
[address] => 0.0.0.0.0
[country] => IR
[stateprov] => somewhere
[city] => Tehrsomewhere (somewhere)
)
That would allow you to check the country/other fields like this:
if($d->country == 'IR') {
// do something
}
NOTE: you had an error (invalid json) in your "city" field, the \ makes it invalid.
Example
You can ensure that your json is valid by checking it at JSON Lint.
I think you are looking for the function json_decode.It decodes the JSON string
See the documentation here
You have to first decode this json string.
$data = '{"address":"0.0.0.0.0","country":"IR","stateprov":"somewhere ","city":"Tehr\somewhere (somewhere)"}';
$decodeData = json_decode($data);
Then use this decode json string in php like this.
if($decodeData->country == 'IR'){
//Do somethong
}
First of all i would like to inform you that given json is not valid. "city" : "Tehr\somewhere (somewhere)" is not valid because of "\".
So change it into below given format.
$jsonEncode = { "address": "0.0.0.0.0","country": "IR","stateprov": "somewhere ","city": "There somewhere (somewhere)"}
$jsonDecode = json_decode($jsonEncode,true);
Now you will get the value in array format.
Array(
[address] => 0.0.0.0.0
[country] => IR
[stateprov] => somewhere
[city] => There somewhere (somewhere)
);
print_r($jsonDecode['city']); will give you city name or details