How to get orderId from below array? - php

$receipt array give the below output. How to get orderId ?
pr($data); die;
to get orderId, $data['receipt']['orderId']; But its gives me error..
App\Model\Entity\PaymentDetail Object
(
[id] => 4
[user_id] => 4
[company_id] => 5
[receipt] => {"orderId":"GPA.3312-5688-8401-53436","packageName":"com.kenbar.mynus2","productId":"com.mynus2.subscription99","purchaseTime":1534914306517,"purchaseState":0,"purchaseToken":"gefffblagdakjleamfngklli.AO-J1Ow9Q5PncOoRk-oshlRBQ8kVqt3A4uIZuQi6InX7sr4bx2lNzjS-VjOXyMIwkl2G-afrI0fzoVLEADNZP2RWekoxwe4ko1M884JALYhaZsxo44U9DshbKJxbDNQHcCx9_z0yQpxc","autoRenewing":true}
)

$orderJson = json_decode(data['receipt'],true);
print_r($orderJson['orderId']);
As data['receipt'] contains a JSON string you need to parse it externally before consuming it as an object.

$orderId=json_decode($your_array->receipt);
echo $orderId->orderId;
You have a json as a string in your stdClass object field so you need to decode it first before you access it.
The above code will work for you.

You can use
$order_id = json_decode($data->reciept, true)["orderId"];

Related

Trying to get property of non-object in json_decode

i am new to JSON i have a json object retrieved from the database in the form of
Array
(
[0] => stdClass Object
(
[id] => 1
[data] => {"vehicle":[{"year":"2000","make":"Ac","model":"Aceca","acquired_year":"2016","acquired_month":"2","use":"Business","distance_driven_to_work_or_school":"2","distance_driven_manually":"10000"}],"first_name":"ADAS","last_name":"DSADSADA","email":"asddsa#sda.com","phone":"dsasasa","postal_code":"","drivers":[{"name":"ssada","birth_year":"2016","birth_month":"2","birth_day":"2","gender":"female","martial_status":"Single","license_number_provided":"yes","license_number":"asddasdas","license_type":"","training_completed":"","years_been_listed_on_auto_policy_in_north_america":"No Previous Experience","license_suspensions":"","accidents":"Select","convictions":"Select","cancellation_reason":"","cancellation_year":"","cancellation_month":"","cancellation_day":""}],"considering_renters_to_reduce_rate":"yes","install_winter_tires":"no","park_in_private_driveway":"yes","willing_to_install_device":"no","years_insured_with_current_company":"4 Years","how_heard_about_us":"asdaa"}
[date] => 2017-11-20 18:17:52
[status] => 0
)
)
now when i try to use json_decode to convert it into an array i am getting Trying to get property of non-object here's my code
<?php
echo "<pre>";
print_r($quotes); //works fine uptil here
$data = json_decode($quotes->data,true);//the error line
echo "<pre>";
print_r($data);
?>
i tried it a couple of ways but it is not working i tried some other solutions as well stil ending up getting errors any help?
It is because $quotes is an array of objects. Try $quotes[0]->data, e.g.:
$data = json_decode($quotes[0]->data,true);
// ------------------------^^^
You're receiving an array containing objects from the database. You're almost there but instead of
$data = json_decode($quotes->data,true);
You should use
$data = json_decode($quotes[0]->data,true);

Can't extract data from Json

I can't extract data from json that I got from an api.
I tried for hours, tried all kinds of formats. Read Stackoverflow threads like How do I extract data from JSON with PHP?, but I can't see what I am doing wrong.
This is the code so far:
$api_results = '{"status":"0000","data":{"opening_price":"6998000","closing_price":"7270000","min_price":"6750000","max_price":"7997000","average_price":"7188302.5804","units_traded":"78484.9241002","volume_1day":"78484.9241002","volume_7day":"335611.84181738","buy_price":"7268000","sell_price":"7274000","date":"1510563513031"}}';
$results = json_decode($api_results, true);
// Some variations I tried:
var_dump($results->status[1]);
var_dump($results->data[1]->opening_price);
var_dump($results["data"][1]["opening_price"]);
End result: NULL NULL NULL
What am I doing wrong?
Thanks for the answers! I will upvote the working ones. Seems I got confused in the formating!
<?php
$api_results = '{"status":"0000","data":{"opening_price":"6998000","closing_price":"7270000","min_price":"6750000","max_price":"7997000","average_price":"7188302.5804","units_traded":"78484.9241002","volume_1day":"78484.9241002","volume_7day":"335611.84181738","buy_price":"7268000","sell_price":"7274000","date":"1510563513031"}}';
$results = json_decode($api_results, true);
print_r($results['status']);
echo "</br>";
print_r($results['data']['opening_price']);
Try access your array that way.
The output is :
0000
6998000
Keep an eye for the nested arrays. You need to access their parent array first in order to get their values.
Have you read the documentation of json_decode() (or, at least, the accepted answer of the question you linked)? If you pass TRUE as the second argument to json_decode() (and there is no decent reason to not pass it) then it decodes the JSON to associative arrays and not objects.
The elements in a PHP array can be accessed using the square bracket syntax.
A simple call to print_r($results) tells its structure:
Array
(
[status] => 0000
[data] => Array
(
[opening_price] => 6998000
[closing_price] => 7270000
[min_price] => 6750000
[max_price] => 7997000
[average_price] => 7188302.5804
[units_traded] => 78484.9241002
[volume_1day] => 78484.9241002
[volume_7day] => 335611.84181738
[buy_price] => 7268000
[sell_price] => 7274000
[date] => 1510563513031
)
)
Now, accessing its items is a piece of cake:
echo($results['status']);
# 0000
echo($results['data']['opening_price']);
# 6998000
Remove true from json_decode so you will have object result like Demo
$results = json_decode($api_results);
var_dump($results->status);
var_dump($results->data->opening_price);
When you use json_decode with true the returned objects will be converted into associative arrays.
Use this code like i think work it fine..
<?php
$api_results = '{"status":"0000","data":{"opening_price":"6998000","closing_price":"7270000","min_price":"6750000","max_price":"7997000","average_price":"7188302.5804","units_traded":"78484.9241002","volume_1day":"78484.9241002","volume_7day":"335611.84181738","buy_price":"7268000","sell_price":"7274000","date":"1510563513031"}}';
$results = json_decode($api_results);
print_r($results);
var_dump($results->status);
$var = $results->data;
var_dump($var->opening_price);
?>
stdClass Object
(
[status] => 0000
[data] => stdClass Object
(
[opening_price] => 6998000
[closing_price] => 7270000
[min_price] => 6750000
[max_price] => 7997000
[average_price] => 7188302.5804
[units_traded] => 78484.9241002
[volume_1day] => 78484.9241002
[volume_7day] => 335611.84181738
[buy_price] => 7268000
[sell_price] => 7274000
[date] => 1510563513031
)
)
string(4) "0000"
string(7) "6998000"
Remove true from json_decode and try something like this:
var_dump($results->status);
var_dump($results->data->opening_price);
If you see {} it is refering to objects and [] indicates that it is an array. You're trying to show everything as if they were arrays
You have set the second parameter of json_decode() to true. that means the json will be converted to an array so you are not able to access the data using pointer -> (because it is not an object).
You may access the data like this:
var_dump($results['status'][0]);
var_dump($results['data'][0]['opening_price']);
P.S: Try var_dump($results) to see the exact data, so you know how to access each attribute.

Could not convert json string to array using PHP

I need one help. I am unable to convert string to json array using PHP. I am explaining my code below.
$education=$_POST['education'];
the above line give this output [{'name':'aaa','id':'12'},{'name':'bbb','id':'20'}]. But its coming as a string .I tried to convert into array like below but it gived output as null
$edu=json_decode($education,true);
print_r($edu);
It gives the empty output. Here I need to convert it to array and populate all data. Please help me.
Hi You need to make your json string like below:
$arr = '[{"name":"aaa","id":"12"},{"name":"bbb","id":"20"}]';
$a = json_decode($arr);
echo "<pre>";
print_r($a);
die;
it will show your output like below:
Array
(
[0] => stdClass Object
(
[name] => aaa
[id] => 12
)
[1] => stdClass Object
(
[name] => bbb
[id] => 20
)
)
In php, you can use this function to check valid json:
function _isJsonString($string) {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
You can also check online for valid json: http://json.parser.online.fr/
Hope this will help.

JSON decode in PHP from a url

Hello i am new with Json in php. I have a web service that gives me data in json format.
I take this data making decode put when i try to use this data i cant
Here is my code:
$url = "http://www.webinsurer.gr/....;
$json = json_decode(#file_get_contents($url), true);
and if i make debug i see the data i take :
[file] => C:\xampp\htdocs\development.insurancemarket.gr\mvc\protected\models\Ratingsmail.php
[line] => 18
[data] => Array
(
[0] => Array
(
[POL_EXPIREDATE] => 2014-05-19 12:00:00
[INCO_IWCODE] => 41
[INCO_DESC] => MAPFRE ASISTENCIA
[PACK_IWCODE] => 0
[PACK_DESC] =>
[OFFERCODE] =>
[PAYMENTCODE] =>
)
[1] => Array
(.....
But i dont now how to use that data. when i try this :
$b= $json->{1}->{'INCO_IWCODE'};
Debug::debuger($b);
the result is nothing
what is wrong? sorry for long post.
When setting the second argument on json_decode to true, you are actively asking for the data to be returned in an associative array and not objects. Thats why your code didn't work.
Demo
You are converting json to associative array. You need to use;
$b = $json["data"][1]["INCO_IWCODE"];
$a = $json[0]->INCO_IWCODE;
That worked for my guys. thanks you all!!!

Extract values from php array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code
I am using SOAP to get data from the server and in response i am getting a php array like this
Array
(
[BookResult] => stdClass Object
(
[PNR] => 5WPODU
[BookingId] => 31149
[Status] => stdClass Object
(
[StatusCode] => 03
[Description] => Fare is not available at the time of booking
[Category] => BK
)
[SSRDenied] => N
[ProdType] => Flight
)
)
All i want to know is how can i extract "PNR" and "StatusCode" value in separate variables so that i can store them in database.
Tried this not working
$p = (object) $array;
echo $p->StatusCode;
Try this:
$PNR = $array["BookResult"]->PNR;
$StatusCode= $array["BookResult"]->Status->StatusCode;
$array is an array. So first dive is $array['BookResult'].
BookResult is stdClass instance so next goes $array['BookResult']->Status (get object's property).
Status is also stdClass instance so get it's property: $array['BookResult']->Status->StatusCode
var_dump($array['BookResult']->PNR);
var_dump($array['BookResult']->Status->StatusCode);
Assuming results are being stored in $array
echo $array['BookResult']->Status->StatusCode;
echo $array['BookResult']->PNR;

Categories