How to decode a json data array? [duplicate] - php

This question already has answers here:
json_decode to array
(12 answers)
Closed 11 months ago.
The json data object below is what's returned from a custom Google search API request. I need to extract each of the "url" elements and place them into an array (using PHP).
myArray = {url1, url2, url3, etc...}
How?
data = '{
"responseData":
{
"results":
[
{
//etc
}
]
}

Am I right that you have JSON string? Use json_decode to decode it. After that you can use
array_map(function($x){
return $x->url;
},$var->responceData->results);
(Requires PHP 5.3 for anonymous function, you can use no anonymous ones if use PHP5.2 or older)
For later versions:
function smth($x){
return $x->url;
}
array_map('smth',$var->responceData->results);

You can use json_decode to get an array corresponding to your JSON and then analyze it like you would do for a normal array.

You might want to read up on json_decode

Try using:
$myObject=json_decode($myJSONstring);
Here's the reference.
Then do:
$urlArray=array();
foreach($myObject->responseData->results as $myResult) {
foreach($myResult as $myAttribute => $myValue) {
$urlArray[] = $myValue;
}
}
$urlArray will be what you're looking for.

Related

filter JSON by object property and return entire object [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I am trying to use PHP to search the following JSON using CarReg to return the CarID
[
{
"CarID": "f11gh126-dee8-46ef-9665-69119c354575",
"CarReg": "ABCD"
},
{
"CarID": "e258f6d4-4503-5d7e-b25c-1fb9767061e2",
"CarReg": "DEFG"
}
]
How would I retrieve the CarID by searching for the CarReg
First you should convert JSON to array using json_decode() and then use array_filter() to filtering items of array.
$arr = json_decode($json, true);
$carId = array_filter($arr, function($item){
return $item['CarReg'] == 'ABCD';
})[0]['CarID'];
// f11gh126-dee8-46ef-9665-69119c354575
Check result in demo

How do I access this JSON information (PHP)? [duplicate]

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 .

PHP decode JSON get uname? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
How do I get the uname value from this JSON into PHP variable? to use through my page?
{
"token": "iutiutiut-0jjjjj0-97987g",
"auth": {
"id": 1,
"app_id": 1,
"user": {
"uname": "foo",
"role": "member"
}
}
}
thanks for some reason just can't get it and I can't find examples similar anywhere on google or I am not calling it correctly.
could you also tell me the correct terminology so I know as well thanks
What you're trying to do is decode JSON. PHP has a built in function for this aptly named... json_decode. Assuming your JSON is a string ($json_string), here is how you would decode it:
$obj = json_decode($json_string);
$uname = $obj->auth->user->uname;
Or, if you prefer the array syntax, use the second argument in json_decode:
$arr = json_decode($json_string, true);
$uname = $obj['auth']['user']['uname'];

Parsing JSON from a URL (PHP CURL) [duplicate]

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/>';
}

php json decode with variables that contains dashes [duplicate]

This question already has answers here:
How do I access this object property with an illegal name?
(2 answers)
Closed 10 months ago.
{"general":{
"round-corner":"0",
"border-stroke":"2",
"background-color":"#ffffff"
}
}
I have this json string, I know that php variable names doesn't support dashes. So what to do in this case ?
When dealing with valid json you don't need to do anything special to use the result in php as long as you don't use extract().
Admiditly it looks cleaner to let json_decode return an array here as Jay Bhatt suggests but you are also free to use a normal object as return (which is an instance of stdclass).
The properties of the returned object can be nearly anything. You just need to use the property name as a php-string instead of a hardcoded literal.
$obj->{'a sentence with spaces and umlauts äüö is valid here'}
<?php
$json = <<<JSON
{"general":{
"round-corner":"0",
"border-stroke":"2",
"background-color äü??$%§":"#ffffff"
}
}
JSON;
$obj = json_decode($json);
$keyName = "round-corner";
var_dump($obj->general->{'round-corner'});
var_dump($obj->general->$keyName);
var_dump($obj->general->{'background-color äü??$%§'});
Result
You can use an array format like this. Hyphened keys will work.
<?php
$json = '{"general":{
"round-corner":"0",
"border-stroke":"2",
"background-color":"#ffffff"
}
}';
$array = json_decode($json, true);
echo $array['general']['border-stroke']; // prints 2
?>
Here's a demo

Categories