getting data from array within an object [duplicate] - php

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

Related

Php get from array

I have small problem, I can't get info from an array.
This is outcome when I var_dump variable.
array(1) {
["server"]=> array(34) {
["dedicated"]=> string(1) "d"
["game_descr"]=> string(14) "Counter-Strike"
["game_dir"]=> string(7) "cstrike"
["gq_address"]=> string(13) "185.119.89.91"
["gq_dedicated"]=> string(1) "d"
["gq_gametype"]=> string(14) "Counter-Strike"
["gq_hostname"]=> string(26) "Assassin'S CSDM [de_dust2]"
["gq_joinlink"]=> string(36) "steam://connect/185.119.89.91:27031/"
["gq_mapname"]=> string(8) "de_dust2"
["gq_maxplayers"]=> int(32)
["gq_mod"]=> string(7) "cstrike"
["gq_name"]=> string(18) "Counter-Strike 1.6"
["gq_numplayers"]=> int(32)
["gq_online"]=> bool(true)
["gq_password"]=> int(0)
["gq_port_client"]=> int(27031)
["gq_port_query"]=> int(27031)
["gq_protocol"]=> string(6) "source"
["gq_transport"]=> string(3) "udp"
["gq_type"]=> string(4) "cs16"
["hostname"]=> string(26) "Assassin'S CSDM [de_dust2]"
["map"]=> string(8) "de_dust2"
["max_players"]=> int(32)
["num_bots"]=> int(0)
["num_players"]=> int(32)
["os"]=> string(1) "l"
["password"]=> int(0)
["players"]=> array(0) { }
["port"]=> int(27031)
["protocol"]=> int(48)
["secure"]=> int(1)
["steamappid"]=> int(10)
["teams"]=> array(0) { }
["version"]=> string(13) "1.1.2.7/Stdio"
}
}
How to get hostname for example?
I tried like $info['hostname'], $info[0]['hostname']; But nothing works, why?
array(1) { ... }
The array has one thing in it.
["server"]=> array(34) { ... }
… which is another array with 34 things in it.
You are ignoring the outer array. You need to get the inner array and then access the item you want from that.
$info['server']['hostname']

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']

Cake PHP how to add array to find fields

I'm using cakephp 2.x and I would like to get certain element as field from my model. the problem is with my structure. If I use find all then I recieve this:
array(9) {
[0]=> ...
[1]=>
array(7) {
["Person"]=>
array(18) {
["id"]=>
int(2)
["imie"]=>
string(6) "STEFAN"
["nazwisko"]=>
string(8) "ŚMIAŁY"
["tel"]=>
string(13) "+55 648989748"
["mob"]=>
string(13) "+87 489747489"
["email"]=>
string(25) "HSDJKAHSDJK#HSDFJASHF.CIJ"
["tytul_naukowy"]=>
string(7) "TEZ NIE"
["data_od"]=>
string(10) "2017-02-16"
["data_do"]=>
NULL
["object_id"]=>
int(-1)
["object_type_id"]=>
int(2)
["duty_id"]=>
int(28)
["created"]=>
string(22) "2017-02-16 13:22:32+01"
["modified"]=>
string(22) "2017-02-16 13:22:32+01"
["deleted"]=>
int(0)
["additional_email"]=>
string(17) "UDIHAS#HFSDKFH.OK"
["description"]=>
string(4) "OPIS"
["displayName"]=>
string(15) "STEFAN ŚMIAŁY"
}
["Duty"]=>
array(3) {
["id"]=>
int(28)
["nazwa"]=>
string(20) "KOORDYNATOR RECEPCJI"
["atrybut"]=>
string(1) "W"
}
["ObjectType"]=>
array(3) {
["id"]=>
int(2)
["nazwa"]=>
string(4) "Obks"
["model"]=>
string(3) "Obk"
}
["Cro"]=>
array(12) {
["id"]=>
NULL
["created"]=>
NULL
["nazwa"]=>
NULL
["tel"]=>
NULL
["fax"]=>
NULL
["email"]=>
NULL
["www"]=>
NULL
["modified"]=>
NULL
["deleted"]=>
NULL
["cro_type_id"]=>
NULL
["displayName"]=>
NULL
["objectTypeIdWithId"]=>
NULL
}
["Attachment"]=>
array(0) {
}
["Specialization"]=>
array(0) {
}
["Obk"]=>
array(1) {
[0]=>
array(13) {
["id"]=>
int(2)
["nazwa"]=>
string(10) "OBK Z ID 2"
["tel"]=>
string(13) "+48 123456890"
["fax"]=>
string(13) "+48 123456789"
["email"]=>
string(15) "EMAIL#EMAIL.COM"
["www"]=>
string(11) "WWW.WWW.WWW"
["modified"]=>
string(22) "2017-03-20 15:11:31+01"
["created"]=>
string(22) "2014-05-12 21:50:17+02"
["deleted"]=>
int(0)
["local_order"]=>
int(3)
["displayName"]=>
string(10) "OBK Z ID 2"
["objectTypeIdWithId"]=>
string(3) "2_2"
["PeopleObk"]=>
array(3) {
["id"]=>
int(1)
["person_id"]=>
int(2)
["obk_id"]=>
int(2)
}
}
}
}
[2]=>
array(7) {
["Person"]=>
array(18) {
["id"]=>
int(3)
["imie"]=>
string(5) "KAMIL"
["nazwisko"]=>
string(10) "STEFAŃSKI"
["tel"]=>
string(13) "+23 123123123"
["mob"]=>
string(13) "+21 412312312"
["email"]=>
string(16) "ASDASD#SDFDSF.LS"
["tytul_naukowy"]=>
string(3) "NIE"
["data_od"]=>
string(10) "2017-02-22"
["data_do"]=>
NULL
["object_id"]=>
int(-1)
["object_type_id"]=>
int(2)
["duty_id"]=>
int(26)
["created"]=>
string(22) "2017-02-22 16:14:32+01"
["modified"]=>
string(22) "2017-02-22 16:14:32+01"
["deleted"]=>
int(0)
["additional_email"]=>
string(22) "ADAM.2313#WEBIMPULS.PL"
["description"]=>
string(4) "TEST"
["displayName"]=>
string(16) "KAMIL STEFAŃSKI"
}
["Duty"]=>
array(3) {
["id"]=>
int(26)
["nazwa"]=>
string(41) "KOORDYNATOR OŚRODKÓW BADAŃ KLINICZNYCH"
["atrybut"]=>
string(1) "W"
}
["ObjectType"]=>
array(3) {
["id"]=>
int(2)
["nazwa"]=>
string(4) "Obks"
["model"]=>
string(3) "Obk"
}
["Cro"]=>
array(12) {
["id"]=>
NULL
["created"]=>
NULL
["nazwa"]=>
NULL
["tel"]=>
NULL
["fax"]=>
NULL
["email"]=>
NULL
["www"]=>
NULL
["modified"]=>
NULL
["deleted"]=>
NULL
["cro_type_id"]=>
NULL
["displayName"]=>
NULL
["objectTypeIdWithId"]=>
NULL
}
["Attachment"]=>
array(0) {
}
["Specialization"]=>
array(0) {
}
["Obk"]=>
array(1) {
[0]=>
array(13) {
["id"]=>
int(3)
["nazwa"]=>
string(10) "OBK Z ID 3"
["tel"]=>
string(13) "+48 123456890"
["fax"]=>
string(13) "+48 123456789"
["email"]=>
string(15) "EMAIL#EMAIL.COM"
["www"]=>
string(11) "WWW.WWW.WWW"
["modified"]=>
string(22) "2017-03-20 10:45:24+01"
["created"]=>
string(22) "2014-05-16 09:09:33+02"
["deleted"]=>
int(0)
["local_order"]=>
int(1)
["displayName"]=>
string(10) "OBK Z ID 3"
["objectTypeIdWithId"]=>
string(3) "2_3"
["PeopleObk"]=>
array(3) {
["id"]=>
int(2)
["person_id"]=>
int(3)
["obk_id"]=>
int(3)
}
}
}
}
And I want just to retrieve Person.id, Person.displayName and Obk.0.id. The two first are no problem, but wheneve I try to get Obk.0.id I'm getting error: SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM for Table Obk.
How can I get something like that? I want to set the value in codition later, but it block my work.
In CakePHP 2 you can specify the fields you want returned by a find() query using the fields option like:-
$this->Foo->find('all', [
'fields' => ['Foo.id', 'Foo.name']
]);
This also works with contained models that have a belongsTo or hasOne relationship:-
$this->Foo->find('all', [
'fields' => ['Foo.id', 'Foo.name', 'Bar.name'],
'contain' => ['Bar']
]);
However, if the contained model is a hasMany or HABTM relationship you need to specify the fields of that model on the contain:-
$this->Foo->find('all', [
'fields' => ['Foo.id', 'Foo.name'],
'contain' => [
'Bar' => ['fields' => 'Bar.name']
]
]);
This is because CakePHP will be performing multiple SQL queries, so the contain needs to have the parameters for the query being run for the relevant model.

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.

Can't Parse JSON using 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}

Categories