This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
How can i enter this array and grab the orderId ?
array(1) {
["data"] => array(1) {
["orderId"] => int(100)
}
}
ive tried to
print_r ["array"]["1"]["array"]["1"]["orderId"];
ive tried
foreach($array as $data)
echo ["data"]["orderId"] ;
Also note, i am receiving this json as a webhook and decoding it to php object with json_decode($json, true);
What am i missing? Should be simple but i cannot get it.
Thanks
Assuming the JSON data you're receiving is in the $array variable:
$orderId = $array["data"]["orderId"];
Explanation:
First you grab the "data" property from the array
That data property contains another array which has its own properties.
From that array, you can grab the "orderId" property.
I hope this helps :)
If your array is named $that, use $that['data']['orderId'];
Try this $array["data"]["orderId"] `
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I have this JSON
{
"AED_USD": {
"2019-08-01": 0.272242
}
}
When dumping this as an array I have:
array(1) { ["AED_USD"]=> array(1) { ["2019-08-01"]=> float(0.272242) } }
I am trying to reach get the float value of 0.272242.
How do I get this value in PHP?
You have to json_decode the string with true as second parameter to make it an array, and then just echo what you want using the name of the keys you have. Like so:
$string = '{
"AED_USD": {
"2019-08-01": 0.272242
}
}';
$decode = json_decode($string, true);
echo $decode['AED_USD']['2019-08-01'];
To get the value in PHP, you can do
$value = $yourArray['AED_USD']['2019-08-01'];
If you want more information: https://www.php.net/manual/pt_BR/language.types.array.php
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I cannot access the contents within the orders array. I am currently doing this to no avail, and am wondering what I am doing wrong:
the $json object is a json response from a rest api.
$orderData = $json['orders'];
foreach($orderData['orders'] as $order){
}
{
"errors":[
],
"orders":[
{
"note":"",
"estimated_shipping_fee":"0.00",
"payment_method":"PAY_CYBERSOURCE",
"escrow_amount":"12.95",
"message_to_seller":"",
"shipping_carrier":"Singpost - Normal Mail",
"currency":"SGD",
"create_time":1532064559,
"pay_time":1532064618,
"recipient_address":{
"town":"",
"city":"",
"name":"Teo",
"district":"",
"country":"SG",
"zipcode":"41253",
"full_address":"In some street somewhjere",
"phone":"23154991",
"state":""
},
"days_to_ship":3,
"tracking_no":"",
"order_status":"SHIPPED",
"note_update_time":0,
"update_time":1532082525,
"goods_to_declare":false,
"total_amount":"12.95",
"service_code":"",
"country":"SG",
"actual_shipping_cost":"",
"cod":false,
"items":[
{
"weight":1.0,
"item_name":"ABC",
"is_wholesale":false,
"item_sku":"5123433123",
"variation_discounted_price":"12.95",
"variation_id":0,
"variation_name":"",
"item_id":1126534500,
"variation_quantity_purchased":1,
"variation_sku":"",
"variation_original_price":"12.95"
}
],
"ordersn":"3589290984539"
}
]
}
Trying to access the variables directly, by say json['orders'][0]['payment_method'] is not working.
convert it to array from json using json_decode(); then you can easily access it and if you want to access using jquery then just convert it object using jQuery.parseJSON();
just use json_decode for retrieving the object and then $obj->note or you can turn it into an array: $array = get_object_vars($obj); like in this answer .
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
How can I iterate to get id value? This is my array:
[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]
tried
<?php
foreach($faculty as $value)
{
echo $value['Id'];
}
?>
Gives an error
Use of undefined constant Id - assumed Id
This is a json which is basically a string, to be more precise the given json contains a list (currently 1 element):
[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]
You need to convert it to an array first:
$jsonValues = json_decode($json, true); //here you will have an array of users (1 now)
foreach($jsonValues as $faculty) //for each user do something
{
echo $faculty['Id'];
}
This is JSON format. First you have to decode it. Example:
$a = '[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]';
$dec = json_decode($a);
echo $dec[0]->Id;
Result: 216
Decoded you have an array, containing exactly one object. You have to access the object properties with -> then.
With JSON [] brackets means an array, while {} brackets mean objects. Learn more: https://en.wikipedia.org/wiki/JSON
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
Some fields save data in the following way in one of my database field:
{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}
(* Don't know how it's called so i wrote string in the title)
How can i access data in particular for every "value".
One way could be to explode for every field but is there a better way to do this?
$ask_for_price_variable = [value from field];
if ($ask_for_price_variable == 'YES') {
// Do something
}
EDIT: As i said i did not know how it was called "JSON" so i could not search for it. Thank you all for the answers.
It is json data. You can access it using json_decode in php
$json = '{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}';
$data = json_decode($json,true);
$ask_for_price_variable = $data['ask_for_price_en-GB'];
Use json_decode
$str = '{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}';
print_r(json_decode($str, true));
You need to study about JSON type and how to encode or decode to get the data.
try this
echo "<pre>";
print_r( json_decode('{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}'));
to view the result , click run or hit f9, here
try json_decode function
$str = '{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}';
$data = json_decode($str, true);
print_r($data);
output:
Array ( [per_meter_en-GB] => TEST_FOR_TEST [roll_40_en-GB] => [ask_for_price_en-GB] => YES )
here you can access required value from $data array eg. echo $data['per_meter_en-GB']; will output TEST_FOR_TEST
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
So I've been stumped and I'm not sure how I would continue this
as an example let's just use books.com as the URL and let's say the JSON response from the URL is
[{"title":"first_title","description":"second_title"},
{"title":"second_title","description":"second_description"}]
How would I print all of the titles (just the titles) without knowing exactly how many there are.
I know that I would need to loop through the JSON but I'm unsure how, if I could have any guidance that would be fantastic.
You should get more familiar with json_decode() and foreach().
First you need to decode json (into array in this example) and then iterate through all elements.
Example of working code:
<?php
$json = '[{"title":"first_title","description":"second_title"},
{"title":"second_title","description":"second_description"}]';
$jsonArray = json_decode($json,true);
foreach($jsonArray as $entry) {
echo $entry['title'].'<br>';
}
Output:
first_title
second_title
This key is to actually convert the JSON response into a PHP associative array by using json_decode and then loop through it.
// Convert the JSON into a PHP associative Array
$response = json_decode($curlResponse,true);
// Loop through the array
foreach ($response as $value) {
echo $value['title'];
echo '<br/>';
}