Can't Parse JSON using PHP - php

I have this JSON response:
object(stdClass)#1 (3) { ["jsonrpc"]=> string(3) "2.0" ["id"]=> int(1)
["result"]=> array(305) { [0]=> object(stdClass)#2 (4) { ["acct"]=>
string(16) "CtjSZlND9uw6-wCQ" ["customid"]=> string(0) "" ["status"]=>
string(8) "disabled" ["name"]=> string(34) "Adrenaline Offroad and
Performance" } [1]=> object(stdClass)#3 (4) { ["acct"]=> string(16)
"CtjSZVNxEYQkvQDy" ["customid"]=> string(0) "" ["status"]=> string(8)
"disabled" ["name"]=> string(10) "Akins Ford" } [2]=>
object(stdClass)#4 (4) { ["acct"]=> string(16) "CtjSZlOMywBP1wDa"
["customid"]=> string(0) "" ["status"]=> string(8) "disabled"
["name"]=> string(20) "Akins Service Center" } [3]=>
object(stdClass)#5 (4) { ["acct"]=> string(16) "CtjSZ1G2OVQD6QDX"
["customid"]=> string(0) "" ["status"]=> string(6) "active" ["name"]=>
string(17) "Ames Ford Lincoln" } [4]=> object(stdClass)#6 (4) {
["acct"]=> string(16) "CtjSZVKCfAsRCQDn" ["customid"]=> string(0) ""
["status"]=> string(8) "disabled" ["name"]=> string(17) "Anthony GMC
Buick" }
How do I parse things like ["id"], ["acct"], ["status"].

From your var_dump I see that is already parsed.
So you can access it like:
$myObject->jsonrpc you will get a string 2.0
$myObject->id you will get an int 1
$myObject->result you will an array with objects

Just add a second parameter (true) to json_decode function.
Like: $response = json_decode($json, true);
When second parameter is TRUE, returned objects will be converted into associative arrays.
For next time, check the php documentation using php.net/{function_name}

Related

How do I parse out this object/array in php?

I have an object I need to parse out and insert into SQL but I am having a hard time figuring out what I need to do as far as the order of fields in the syntax listed below.
Here is the object vardump:
object(EmailCheck\Object\ResponseObject)#25 (1) {
["response":protected]=> array(6) {
["status"]=> string(7) "success"
["total_results"]=> int(6)
["total_pages"]=> int(1)
["query"]=> array(8) {
["job_id"]=> int(2562625)
["valids"]=> int(1)
["invalids"]=> int(1)
["disposables"]=> int(1)
["catchalls"]=> int(1)
["unknowns"]=> int(1)
["page"]=> int(0)
["items_per_page"]=> int(10)
}
["results"]=> array(6) {
[0]=> array(2) {
["data"]=> array(4) {
["email"]=> string(20) "chris#example.com"
[0]=> string(1) "1"
["ID"]=> string(1) "1"
["EMAIL"]=> string(20) "chris#example.com"
}
["verification"]=> object(EmailCheck\Object\VerificationObject)#7 (1) {
["response":protected]=> array(7) {
["result"]=> string(5) "valid"
["flags"]=> array(3) {
[0]=> string(7) "has_dns"
[1]=> string(10) "has_dns_mx"
[2]=> string(16) "smtp_connectable"
}
["suggested_correction"]=> string(0) ""
["address_info"]=> object(EmailCheck\Object\ResponseObject)#9 (1) {
["response":protected]=> array(9) {
["original_email"]=> string(20) "chris#example.com"
["normalized_email"]=> string(20) "chris#example.com"
["addr"]=> string(6) "chris"
["alias"]=> string(0) "" ["host"]=> string(13) "example.com"
["fqdn"]=> string(13) "example.com"
["domain"]=> string(9) "example"
["subdomain"]=> string(0) ""
["tld"]=> string(3) "com"
}
}
["email"]=> string(20) "chris#example.com
["result_integer"]=> int(0)
["credits_info"]=> object(EmailCheck\Object\ResponseObject)#8 (1) {
["response":protected]=> array(0) {
}
}
}
}
}
I know the syntax is something like this but it is not returning anything:
echo $object->data->email;
I need to extract the following fields: result, normalized_email, email
This is what ended up working. I did not need to reference the "response" portion of the object.
$object->results[0]['data']['email']

How to get the count of each field inside a group tab object

This is my object and I want to get the number of fields in each tab. I'm thinking of skipping the field if its type is a Tab then start counting the fields before the next Type of tab..
Anyway here is my object below:
array(49) {
[0]=>
array(12) {
["key"]=>
string(19) "field_596796ae0c4c4"
["label"]=>
string(7) "GENERAL"
["name"]=>
string(0) ""
["_name"]=>
string(0) ""
["type"]=>
string(3) "tab"
["order_no"]=>
int(0)
["instructions"]=>
string(0) ""
["required"]=>
int(0)
["id"]=>
string(10) "acf-field-"
["class"]=>
string(3) "tab"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(0) ""
}
}
["allorany"]=>
string(3) "all"
}
["field_group"]=>
int(165)
}
[1]=>
array(18) {
["key"]=>
string(19) "field_5964e9206df82"
["label"]=>
string(5) "Brand"
["name"]=>
string(5) "brand"
["_name"]=>
string(5) "brand"
["type"]=>
string(8) "taxonomy"
["order_no"]=>
int(1)
["instructions"]=>
string(0) ""
["required"]=>
int(1)
["id"]=>
string(15) "acf-field-brand"
["class"]=>
string(8) "taxonomy"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(7) "regular"
}
}
["allorany"]=>
string(3) "all"
}
["taxonomy"]=>
string(8) "pa_brand"
["field_type"]=>
string(6) "select"
["allow_null"]=>
int(0)
["load_save_terms"]=>
int(0)
["return_format"]=>
string(2) "id"
["field_group"]=>
int(165)
["multiple"]=>
int(0)
}
[2]=>
array(12) {
["key"]=>
string(19) "field_596796da0c4c5"
["label"]=>
string(6) "LAUNCH"
["name"]=>
string(0) ""
["_name"]=>
string(0) ""
["type"]=>
string(3) "tab"
["order_no"]=>
int(2)
["instructions"]=>
string(0) ""
["required"]=>
int(0)
["id"]=>
string(10) "acf-field-"
["class"]=>
string(3) "tab"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(0) ""
}
}
["allorany"]=>
string(3) "all"
}
["field_group"]=>
int(165)
}
[3]=>
array(15) {
["key"]=>
string(19) "field_5964e99e6df84"
["label"]=>
string(20) "Announced (Globally)"
["name"]=>
string(18) "announced_globally"
["_name"]=>
string(18) "announced_globally"
["type"]=>
string(11) "date_picker"
["order_no"]=>
int(3)
["instructions"]=>
string(23) "Date globally announced"
["required"]=>
int(0)
["id"]=>
string(28) "acf-field-announced_globally"
["class"]=>
string(11) "date_picker"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(7) "regular"
}
}
["allorany"]=>
string(3) "all"
}
["date_format"]=>
string(4) "mmyy"
["display_format"]=>
string(5) "MM yy"
["first_day"]=>
int(1)
["field_group"]=>
int(165)
}
or this object that I simplified and run in Javascript:
var productObject = [
{
"type":"tab",
"value":"GENERAL"
},
{
"type":"taxonomy",
"value":"56",
"label":"Brand"
},
{
"type":"tab",
"value":"LAUNCH"
},
{
"type":"text",
"name":"announced_ph",
"label":"Announced(Philippines)",
"value":"07072017"
},{
"type":"text",
"name":"announced_global",
"label":"Announced(Global)",
"value":"09092017"
},{
"type":"tab",
"value":"NETWORK"
},{
"type":"text",
"name":"sim_type",
"label":"SIM",
"value":"dualnano"
},{
"type":"text",
"name":"broadband_speed",
"label":"Broadband Speed",
"value":"LTE Cat6"
},{
"type":"text",
"name":"broadband_speed",
"label":"Broadband Speed",
"value":"LTE Cat6"
}
];
I need to know the number of fields/objects after an object with a type of "tab". Because I need to know it for the rowspan value of my table.
The ouput I'm thinking should be:
Tab "General" has 1 field
Tab "Launch" has 2 field
Tab "Network" has 3 field
Thanks!
Loop over the object, then just check if the type is equal to tab.
Something like if (productObject.type == 'tab').
If it is equal to tab, set a counter variable to 0, then just increment it until you reach the next tab value.
my_counter = 0
tab_type = ''
for po in productObject:
if po['type'] == 'tab':
my_counter = 0
tab_type = po['value']
my_counter += 1
Something like this.
Using the productObject, I'll based it on the key value
//Getting all "value" keys
var arr = $.map(productObject,function(k,v){
if(k.type == "tab"){
return k.value;
}
});
//Counting each duplicates
var map = arr.reduce(function(prev, cur) {
prev[cur] = (prev[cur] || 0) + 1;
return prev;
}, {});
//Returns JSON
console.log(JSON.stringify(map));
Sample Output:
{"GENERAL":1,"LAUNCH":1,"NETWORK":1}

How to get object like value:item:private from PHP Array

I have the array like this:
} ["items":"Jcart":private]=> array(3) {
[0]=>
string(1) "3"
[1]=>
string(1) "2"
[2]=>
string(7) "ABC-123" }
How to get the "items" values in a php variable?
///////
Te complete object is:
object(Jcart)#1 (8) {
["config"]=>
array(12) {
["jcartPath"]=>
string(6) "jcart/"
["checkoutPath"]=>
string(12) "checkout.php"
["item"]=>
array(6) {
["id"]=>
string(10) "my-item-id"
["name"]=>
string(12) "my-item-name"
["price"]=>
string(13) "my-item-price"
["qty"]=>
string(11) "my-item-qty"
["url"]=>
string(11) "my-item-url"
["add"]=>
string(13) "my-add-button"
}
["paypal"]=>
array(5) {
["id"]=>
string(38) "seller_1282188508_biz#conceptlogic.com"
["https"]=>
bool(true)
["sandbox"]=>
bool(false)
["returnUrl"]=>
string(0) ""
["notifyUrl"]=>
string(0) ""
}
["currencyCode"]=>
string(3) "USD"
["csrfToken"]=>
bool(false)
["text"]=>
array(14) {
["cartTitle"]=>
string(13) "Shopping Cart"
["singleItem"]=>
string(4) "Item"
["multipleItems"]=>
string(5) "Items"
["subtotal"]=>
string(8) "Subtotal"
["update"]=>
string(6) "update"
["checkout"]=>
string(8) "checkout"
["checkoutPaypal"]=>
string(20) "Checkout with PayPal"
["removeLink"]=>
string(6) "remove"
["emptyButton"]=>
string(5) "empty"
["emptyMessage"]=>
string(19) "Your cart is empty!"
["itemAdded"]=>
string(11) "Item added!"
["priceError"]=>
string(21) "Invalid price format!"
["quantityError"]=>
string(38) "Item quantities must be whole numbers!"
["checkoutError"]=>
string(34) "Your order could not be processed!"
}
["button"]=>
array(4) {
["checkout"]=>
string(0) ""
["paypal"]=>
string(0) ""
["update"]=>
string(0) ""
["empty"]=>
string(0) ""
}
["tooltip"]=>
bool(true)
["decimalQtys"]=>
bool(false)
["decimalPlaces"]=>
int(1)
["priceFormat"]=>
array(3) {
["decimals"]=>
int(2)
["dec_point"]=>
string(1) "."
["thousands_sep"]=>
string(1) ","
}
}
["items":"Jcart":private]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "2"
}
["names":"Jcart":private]=>
array(2) {
[3]=>
string(12) "Hockey Stick"
[2]=>
string(13) "Baseball Mitt"
}
["prices":"Jcart":private]=>
array(2) {
[3]=>
string(5) "33.25"
[2]=>
string(5) "19.50"
}
["qtys":"Jcart":private]=>
array(2) {
[3]=>
string(1) "1"
[2]=>
string(2) "20"
}
["urls":"Jcart":private]=>
array(2) {
[3]=>
string(15) "http://bing.com"
[2]=>
string(16) "http://yahoo.com"
}
["subtotal":"Jcart":private]=>
float(423.25)
["itemCount":"Jcart":private]=>
int(21)
}
I just need the values in ["items":"Jcart":private]
I assume you got this by casting an object to an array. If that's the case, you shouldn't be doing that! The property is private because you're not supposed to access it directly because it's not a public API. The object should have a public method which you're supposed to call instead, that's the public API. Something like $foo->getItems() or such. Read the documentation and/or source code.

getting data from array within an object [duplicate]

I'm new to PHP and am trying to access the value of "contactId" from the following:
object(stdClass)#2 (4) {
["contacts"]=> array(1) {
[0]=> object(stdClass)#3 (18) {
["email"]=> string(24) "joewilliams#icontact.com"
["firstName"]=> string(3) "Joe"
["lastName"]=> string(8) "Williams"
["prefix"]=> string(3) "Mr."
["suffix"]=> string(3) "Jr."
["fax"]=> string(0) ""
["phone"]=> string(10) "8668039462"
["street"]=> string(21) "2365 Meridian Parkway"
["street2"]=> string(0) ""
["city"]=> string(6) "Durham"
["state"]=> string(2) "NC"
["postalCode"]=> string(5) "27713"
["createDate"]=> string(19) "2013-03-30 21:22:28"
["status"]=> string(6) "normal"
["bounceCount"]=> NULL
["contactId"]=> string(8) "67295434"
}
}
["limit"]=> int(20)
["offset"]=> int(0)
["total"]=> int(1)
}
How can I retrieve the value of contactId?
Many thanks!
These are not arrays, but an object containing an array containing an object. PHP object access syntax is with ->
$variableName->contacts[0]->contactId

How can I access the images in an Instagram JSON? Use objects or an associative array?

Since I stumbled over some wrong solutions (at least always pointing into the right direction) for having your instagram photos pulled onto my site, I started looking for new ways. And I had to implement SSL into the cURL scripting part to get the instagram feed.
However I am stuck in the following part: How can I tell my script to show my images?
I found out, that $result->data works, but if I continue this line with ->images PHP gives me the "Trying to get property of non-object". And the output of the decoded JSON is still lacking overviewability. I pasted here the code as far as I am right now, debugging mode.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.instagram.com/v1/users/myUserID/media/recent/?access_token=myAccessToken');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
curl_close($ch);
$result=json_decode($result);
$data=($result->data);
var_dump ($data->images);
// echo "<img src="$result->images->thumbnail->url">"
?>
Here is the output of the json_decode:
array(2) {
[0]=> object(stdClass)#4 (15) {
["attribution"]=> NULL
["tags"]=> array(4) {
[0]=> string(5) "remix"
[1]=> string(9) "trncfrmcn"
[2]=> string(4) "tygn"
[3]=> string(9) "mastering"
}
["type"]=> string(5) "image"
["location"]=> NULL
["comments"]=> object(stdClass)#5 (2) {
["count"]=> int(0)
["data"]=> array(0) { }
}
["filter"]=> string(7) "Toaster"
["created_time"]=> string(10) "1373149059"
["link"]=> string(34) "http://instagram.com/p/bcOOGMF5Vi/"
["likes"]=> object(stdClass)#6 (2) {
["count"]=> int(6)
["data"]=> array(4) {
[0]=> object(stdClass)#7 (4) {
["username"]=> string(13) "itsallaboutva"
["profile_picture"]=> string(77)
"http://images.ak.instagram.com/profiles/profile_186336970_75sq_1341852049.jpg"
["id"]=> string(9) "186336970"
["full_name"]=> string(16) "Victoria Anwuzia"
}
[1]=> object(stdClass)#8 (4) {
["username"]=> string(15) "eatmybeatsordie"
["profile_picture"]=> string(76)
"http://images.ak.instagram.com/profiles/profile_32709002_75sq_1355602472.jpg"
["id"]=> string(8) "32709002"
["full_name"]=> string(12) "D-Mite Beats"
}
[2]=> object(stdClass)#9 (4) {
["username"]=> string(12) "joetranmusic"
["profile_picture"]=> string(76) "http://images.ak.instagram.com/profiles/profile_32545994_75sq_1362449260.jpg"
["id"]=> string(8) "32545994"
["full_name"]=> string(8) "Joe Tran"
}
[3]=> object(stdClass)#10 (4) {
["username"]=> string(14) "xariahmarshall"
["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_275171230_75sq_1362937374.jpg"
["id"]=> string(9) "275171230"
["full_name"]=> string(14) "xariahmarshall"
}
}
}
["images"]=> object(stdClass)#11 (3) {
["low_resolution"]=> object(stdClass)#12 (3) {
["url"]=> string(79) "http://distilleryimage2.s3.amazonaws.com/ded954f8e68911e2adc122000a1f9ace_6.jpg"
["width"]=> int(306)
["height"]=> int(306)
}
["thumbnail"]=> object(stdClass)#13 (3) {
["url"]=> string(79) "http://distilleryimage2.s3.amazonaws.com/ded954f8e68911e2adc122000a1f9ace_5.jpg"
["width"]=> int(150)
["height"]=> int(150)
}
["standard_resolution"]=> object(stdClass)#14 (3) {
["url"]=> string(79) "http://distilleryimage2.s3.amazonaws.com/ded954f8e68911e2adc122000a1f9ace_7.jpg"
["width"]=> int(612)
["height"]=> int(612)
}
}
["users_in_photo"]=> array(0) { }
["caption"]=> object(stdClass)#15 (4) {
["created_time"]=> string(10) "1373149155"
["text"]=> string(34) "#Mastering #TYGN #TRNCFRMCN #Remix"
["from"]=> object(stdClass)#16 (4) {
["username"]=> string(12) "liebdich9vol"
["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_444541508_75sq_1372633384.jpg"
["id"]=> string(9) "444541508"
["full_name"]=> string(11) "Ian Maybach"
}
["id"]=> string(18) "494333402642093342"
}
["user_has_liked"]=> bool(false)
["id"]=> string(28) "494332600473064802_444541508"
["user"]=> object(stdClass)#17 (6) {
["username"]=> string(12) "liebdich9vol"
["website"]=> string(0) ""
["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_444541508_75sq_1372633384.jpg"
["full_name"]=> string(11) "Ian Maybach"
["bio"]=> string(0) ""
["id"]=> string(9) "444541508"
}
}
[1]=> object(stdClass)#18 (15) {
["attribution"]=> NULL
["tags"]=> array(3) {
[0]=> string(6) "studio"
[1]=> string(6) "garden"
[2]=> string(4) "view"
}
["type"]=> string(5) "image"
["location"]=> NULL
["comments"]=> object(stdClass)#19 (2) {
["count"]=> int(0)
["data"]=> array(0) { }
}
["filter"]=> string(7) "Toaster"
["created_time"]=> string(10) "1372661045"
["link"]=> string(34) "http://instagram.com/p/bNrZ-sl5Xp/"
["likes"]=> object(stdClass)#20 (2) {
["count"]=> int(4)
["data"]=> array(4) {
[0]=> object(stdClass)#21 (4) {
["username"]=> string(9) "seanbabas"
["profile_picture"]=> string(76) "http://images.ak.instagram.com/profiles/profile_30025104_75sq_1369077409.jpg"
["id"]=> string(8) "30025104"
["full_name"]=> string(10) "Sean Babas"
}
[1]=> object(stdClass)#22 (4) {
["username"]=> string(12) "ginocolletti"
["profile_picture"]=> string(75) "http://images.ak.instagram.com/profiles/profile_4634400_75sq_1364847565.jpg"
["id"]=> string(7) "4634400"
["full_name"]=> string(13) "Gino Colletti"
}
[2]=> object(stdClass)#23 (4) {
["username"]=> string(13) "marygraceklee"
["profile_picture"]=> string(75) "http://images.ak.instagram.com/profiles/profile_3269702_75sq_1373251095.jpg"
["id"]=> string(7) "3269702"
["full_name"]=> string(13) "Marygrace Lee"
}
[3]=> object(stdClass)#24 (4) {
["username"]=> string(10) "cyonthefly"
["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_225204950_75sq_1368426695.jpg"
["id"]=> string(9) "225204950"
["full_name"]=> string(21) "!CY ÊŽlÉŸÇɥʇuo ✈"
}
}
}
["images"]=> object(stdClass)#25 (3) {
["low_resolution"]=> object(stdClass)#26 (3) {
["url"]=> string(79) "http://distilleryimage9.s3.amazonaws.com/9f5f0300e21911e2929322000a9e0719_6.jpg"
["width"]=> int(306)
["height"]=> int(306)
}
["thumbnail"]=> object(stdClass)#27 (3) {
["url"]=> string(79) "http://distilleryimage9.s3.amazonaws.com/9f5f0300e21911e2929322000a9e0719_5.jpg"
["width"]=> int(150)
["height"]=> int(150)
}
["standard_resolution"]=> object(stdClass)#28 (3) {
["url"]=> string(79) "http://distilleryimage9.s3.amazonaws.com/9f5f0300e21911e2929322000a9e0719_7.jpg"
["width"]=> int(612)
["height"]=> int(612)
}
}
["users_in_photo"]=> array(0) { }
["caption"]=> object(stdClass)#29 (4) {
["created_time"]=> string(10) "1372900093"
["text"]=> string(21) "#studio #view #garden"
["from"]=> object(stdClass)#30 (4) {
["username"]=> string(12) "liebdich9vol"
["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_444541508_75sq_1372633384.jpg"
["id"]=> string(9) "444541508"
["full_name"]=> string(11) "Ian Maybach"
}
["id"]=> string(18) "492244118468531915"
}
["user_has_liked"]=> bool(false)
["id"]=> string(28) "490238835760272873_444541508"
["user"]=> object(stdClass)#31 (6) {
["username"]=> string(12) "liebdich9vol"
["website"]=> string(0) ""
["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_444541508_75sq_1372633384.jpg"
["full_name"]=> string(11) "Ian Maybach"
["bio"]=> string(0) ""
["id"]=> string(9) "444541508"
}
}
}
My aim is to have only one picture on my site but navigation arrows to switch to the next one. How would that be achieved?
To access infos from StdClass you can do this:
foreach($data->images as $i){
$i->url;
}
You can force a casting to array too:
foreach($data->images as $i){
$i = $i->toArray();
$i['url'];
}
And you can get the json decode on array form:
$data = json_decode($json,true); //the second param true means, bring me it like array.
as per http://php.net/json_decode, you need to specify you want an associative array instead of an object from json_decode:
so use json decode like this
$result=json_decode($result, true);
Now you will get array and you can easily access data.

Categories