JSON object passed in the php script [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a PHP script and JSON object with some values is being passed in the the PHP get function. I have tried different ways to decode JSON but failed.
The code I have tried is :
$get_order_info = $_GET['orderInfo'];
$order_json = json_decode($get_order_info, true);
echo $order_json->{'mealsInfo'};
The JSON string is :
{
"mealsInfo" : [
{
"DrinkSize" : 1,
"MealQuantity" : 1,
"MealId" : "57",
"addons" : [
{
"addOnID" : 1,
"addonTitle" : "spicy"
},
{
"addOnID" : 3,
"addonTitle" : "Thin Base"
}
],
"FriesSize" : 2
}
],
"TransactionID" : "56",
"OrerType" : "PickUp",
"frenchiseInfo" : {
"storeName" : "Dubai Downtown Franchise",
"OrderCollectionTime" : "06:12:50 PM",
"FranchiseId" : "4"
},
"customerinfo" : {
"Instructions" : "Test instruction",
"CustomerName’" : "Talat",
"Area" : "al Riga",
"City" : "Dubai",
"Phone" : "0559467800",
"Email" : "test#test.com",
"Address" : "al nouf tower"
},
"status" : "pending",
"totalPrice" : 51
}
Can somebody please help me to decode it in a correct way?
Thanks in advance !

You are passing true as second parameter to json_decode, it will return and array not object. Try with -
$order_json = json_decode($get_order_info, true);
echo $order_json['mealsInfo'][0]['DrinkSize'];

$get_order_info = $_POST['orderInfo'];
$order_json = json_decode($get_order_info, true);
echo $order_json->{'mealsInfo'};

Try this.
$order_json = json_decode($get_order_info, true);
var_dump($order_json->{'mealsInfo'});

Related

How to filter an array in PHP by fields with empty values and multiple criteria? [closed]

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

How to Read This below JSON Code Data in PHP for every value [closed]

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 1 year ago.
Improve this question
{
"data": {
"orderId": "10946506",
"status": "FAILED",
"mobile": "9795681183",
"amount": "100.00",
"transId": "",
"error_code": "119",
"service": "Airtel",
"bal": "",
"creditUsed": "0",
"resText": "Ip 185.27.134.67 Not Configured",
"gstMode": "P2P",
"tdsAmount": "0"
}
}
You can do that using PHP native function json_decode, here the example :
<?php
$json ='{"data":{"orderId":"10946506","status":"FAILED","mobile":"9795681183","amount":"100.00","transId":"","error_code":"119","service":"Airtel","bal":"","creditUsed":"0","resText":"Ip 185.27.134.67 Not Configured","gstMode":"P2P","tdsAmount":"0"}}';
$result = json_decode($json);
//Array
print_r($result);
//single data
echo $result->data->orderId;
?>

multi level nested JSON output using PHP through MySQL and PDO

I'm trying to create a JSON output for a PHP website project. I have done quite a bit of research that ended up completely confusing me in the end.
First of, I'm not sure what would be best between PDO or mysqli but I had to move forward and go with PDO. I understand the process of connecting to the server - selecting the database and table. I'm sturggling with outputing the data in the actual nested JSON format that I so cafully planned.
While I am not 100% sure I got it all right
I think I just need to keep doing it with the right code
until everythy makes sense...
Please help!
I am looking to get to somthing like this for my JSON output:
maincategory = {
"subcategory1" : {
"group" : {
"subgroup" :{
"name" : "Jason Lengstorf",
"age" : "24","gender" : "male",
"height" : "6.3",
"weight" : "164lbs"
}
}
},
"subcategory2" : {
"group" : {
"subgroup" :{
"name" : "Jason Lengstorf",
"age" : "24","gender" : "male",
"height" : "6.3",
"weight" : "164lbs"
}
}
}
}
Thanks in advance for your help!

how to convert this json data in php

I am using an api which is giving me a very strange json format.. i'm posting its data which i am getting thru it..
info = { "title" : "Asian Dad: B Again!? (you die)", "image" : "http://i.ytimg.com/vi/IN7o2Iy89WQ/default.jpg", "length" : "2", "status" : "serving", "progress_speed" : "", "progress" : "", "ads" : "", "pf" : "", "h" : "f53762dab34022e9d851ab71e0bf166f" };
I'm trying to print this data in php but i'm not able to do that..nothing is showing on my webpage,....
My code are..
First i tried,
<?php
$url="http://www.website-name.com/a/itemInfo/?video_id=IN7o2Iy89WQ&ac=www";
$info=file_get_contents($url);
$info=json_decode($info,true);
echo $info;
?>
My second attempt was,
<?php
$url="http://www.website-name.com/a/itemInfo/?video_id=IN7o2Iy89WQ&ac=www";
$info=file_get_contents($url);
$info=json_decode($info,true);
$info->h;
?>
My last attempt was,
<?php
$url="http://www.website-name.com/a/itemInfo/?video_id=IN7o2Iy89WQ&ac=www";
$info=file_get_contents($url);
$info=json_decode($info,true);
$info['h'];
?>
Nothing is happening..
please somebody help me
API Url converted...
The page is sending
"info = { "title" : "Asian Dad: B Again!? (you die)", "image" : "http://i.ytimg.com/vi/IN7o2Iy89WQ/default.jpg", "length" : "2", "status" : "serving", "progress_speed" : "", "progress" : "", "ads" : "", "pf" : "", "h" : "058ce93db26fce4a9f1cb41ae2e7c1bb" };"
You cannot use json_decode on this because the info = and the ; at the end are not json. You have to strip the info = and the ;.
$url="http://www.website-name.com/a/itemInfo/?video_id=IN7o2Iy89WQ&ac=www";
$info = file_get_contents($url);
$info = trim($info, "info = ");
$info = rtrim($info, ";");
$json = json_decode($info, true);
echo $json['status'];
The data I got from the URL in your example is
info = { "title" : "Asian Dad: B Again!? (you die)", "image" : "http://i.ytimg.com/vi/IN7o2Iy89WQ/default.jpg", "length" : "2", "status" : "serving", "progress_speed" : "", "progress" : "", "ads" : "", "pf" : "", "h" : "5cddd4d1667f24aa9a0f5a6cc21e24e3" };
That's an executable JavaScript snippet, not actually JSON. The reason your php is failing is due to the 'info =' part... json_encode returns null on decoding failure.
While this is an assignment of a variable to a JavaScript object, that would work as JSON too if you removed the 'info =' and semicolon. Assuming the responses are predictable, you could do this with php str_replace, but finding an API that returns JSON for your source would be a more reliable and clean solution.
If you're getting NULL when you var_dump($info) on the second line as you mentioned in comments, then file_get_contents() doesn't retrieve the data.
It's most likely because allow_url_fopen is set to false in PHP.ini
From the json_decode() documentation:
Takes a JSON encoded string and converts it into a PHP variable.
If 2nd parameter is set to true it returns an array !
SO your first and second attempt is wrong.! It should work third attempt. Just check if you retrieve the data.

parse a dump file like xml file as array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What I have :
I have a software dump data like exactly below file :
DMPDATA = {
["invent1"] = {
["1000:1"] = {
["I"] = "6948",
["C"] = 1,
["G2"] = "0",
["G3"] = "0",
["G1"] = "0",
},
["0000:10"] = {
["I"] = "39622",
["C"] = 1,
["G2"] = "0",
["G3"] = "0",
["G1"] = "0",
},
},
["invent2"] = {
["M:1"] = 60116,
["M:3"] = 32246,
["M:2"] = 41252,
},
["invent3"] = {
["47465"] = 5,
["12970"] = 5,
},
["invent4"] = {
{
["F"] = 0,
["V"] = 0,
["N"] = "Classic",
}, -- [1]
{
["F"] = 16,
["V"] = 3500,
["N"] = "Horde",
}, -- [2]
},
["invent6"] = {
["class"] = "WARRIOR",
["gender"] = 2,
},
}
Question:
I want to parse above data as array , I try to do but don't know whats better way .
How can parse files like above code with PHP to have a all data as Array ?
This looks like LUA code. Have you tried the Lua class in PHP?
http://www.php.net/manual/en/lua.eval.php
Here's a guy that has a similar problem with a WoW Addon Lua file:
I need a tool to parse Lua tables, preferrably in Ruby or Java
EDIT:
Try this tool. It also links to a PHP script you might use. http://fin.instinct.org/lua/
EDIT 2:
This is basically what you need. As you can see you're not the first person who wants to parse WoW Lua dumps to PHP arrays =)
http://fin.instinct.org/lua/lua2phparray.phps

Categories