How to properly parse a stdobject

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

How to properly parse a stdobject

Post by lovelf »

How could I parse the following stdobject to just get two arrays, one with name and the other with email.

for example I'd want to come up with:

Code: Select all

$my_arr[0]='mauro@serviweb.es';
$my_arr2[0]='Mauro Francisco Serviweb.es';
to get the name I would need to iterate and concatenate givenName.middleName.familyName

yet there are plenty of those stdobjects which don't have those keys, only email for some of these it appears as it comes from Yahoo, for those it can be

Code: Select all

$my_arr[1]='email@domain.com';
$my_arr2[1]='';

Code: Select all

stdClass Object
(
[contact] => Array
(
[0] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777834
[isConnection] => false
[id] => 16777834
[fields] => Array
(
[0] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777834/email/16778439
[id] => 16778439
[type] => email
[value] => mauro@serviweb.es
[editedBy] => OWNER
)
[1] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777834/name/16778438
[id] => 16778438
[type] => name
[value] => stdClass Object
(
[givenName] => Mauro
[middleName] => Francisco
[familyName] => - Serviweb.es
[prefix] => 
[suffix] => 
[givenNameSound] => 
[familyNameSound] => 
)
[editedBy] => OWNER
)
)
[categories] => stdClass Object
(
[created] => 2013-02-20T13:56:57Z
[updated] => 2013-02-20T13:56:57Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777834/category/Gmail
[id] => 2
[name] => Gmail
)
)
[1] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777858
[isConnection] => false
[id] => 16777858
[fields] => Array
(
[0] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777858/email/16778474
[id] => 16778474
[type] => email
[value] => queenymj@naver.com
[editedBy] => OWNER
)
[1] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777858/name/16778473
[id] => 16778473
[type] => name
[value] => stdClass Object
(
[givenName] => 유미정
[middleName] => 
[familyName] => 
[prefix] => 
[suffix] => 
[givenNameSound] => 
[familyNameSound] => 
)
[editedBy] => OWNER
)
)
[categories] => stdClass Object
(
[created] => 2013-02-20T13:56:57Z
[updated] => 2013-02-20T13:56:57Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777858/category/Gmail
[id] => 2
[name] => Gmail
)
)
[2] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777861
[isConnection] => false
[id] => 16777861
[fields] => Array
(
[0] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777861/email/16778478
[id] => 16778478
[type] => email
[value] => ernesto@you-on-a-diet.net
[editedBy] => OWNER
)
[1] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777861/name/16778477
[id] => 16778477
[type] => name
[value] => stdClass Object
(
[givenName] => You
[middleName] => On
[familyName] => A Diet (Ernesto Martinez)
[prefix] => 
[suffix] => 
[givenNameSound] => 
[familyNameSound] => 
)
[editedBy] => OWNER
)
)
[categories] => stdClass Object
(
[created] => 2013-02-20T13:56:57Z
[updated] => 2013-02-20T13:56:57Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777861/category/Gmail
[id] => 2
[name] => Gmail
)
)
[3] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777827
[isConnection] => false
[id] => 16777827
[fields] => Array
(
[0] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777827/email/16778427
[id] => 16778427
[type] => email
[value] => cullensabcs@gmail.com
[editedBy] => OWNER
)
[1] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777827/name/16778426
[id] => 16778426
[type] => name
[value] => stdClass Object
(
[givenName] => Cullen's
[middleName] => 
[familyName] => Abc's
[prefix] => 
[suffix] => 
[givenNameSound] => 
[familyNameSound] => 
)
[editedBy] => OWNER
)
)
[categories] => stdClass Object
(
[created] => 2013-02-20T13:56:57Z
[updated] => 2013-02-20T13:56:57Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777827/category/Gmail
[id] => 2
[name] => Gmail
)
)
[4] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777688
[isConnection] => false
[id] => 16777688
[fields] => Array
(
[0] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777688/email/16778220
[id] => 16778220
[type] => email
[value] => abelfernandez@adinet.com.uy
[editedBy] => OWNER
)
[1] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777688/name/16778219
[id] => 16778219
[type] => name
[value] => stdClass Object
(
[givenName] => abelfernandez@adinet.com.uy
[middleName] => 
[familyName] => 
[prefix] => 
[suffix] => 
[givenNameSound] => 
[familyNameSound] => 
)
[editedBy] => OWNER
)
)
[categories] => stdClass Object
(
[created] => 2013-02-20T13:56:57Z
[updated] => 2013-02-20T13:56:57Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777688/category/Gmail
[id] => 2
[name] => Gmail
)
)
[5] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777883
[isConnection] => false
[id] => 16777883
[fields] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777883/email/16778511
[id] => 16778511
[type] => email
[value] => aboutcareers@yahoo-inc.com
[editedBy] => OWNER
)
[categories] => stdClass Object
(
[created] => 2013-02-20T13:56:57Z
[updated] => 2013-02-20T13:56:57Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777883/category/Gmail
[id] => 2
[name] => Gmail
)
)
[6] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777780
[isConnection] => false
[id] => 16777780
[fields] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777780/email/16778356
[id] => 16778356
[type] => email
[value] => administracion@outletmayorista.com
[editedBy] => OWNER
)
[categories] => stdClass Object
(
[created] => 2013-02-20T13:56:57Z
[updated] => 2013-02-20T13:56:57Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777780/category/Gmail
[id] => 2
[name] => Gmail
)
)
[7] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777689
[isConnection] => false
[id] => 16777689
[fields] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777689/email/16778221
[id] => 16778221
[type] => email
[value] => adohio@youthfullook.com
[editedBy] => OWNER
)
[categories] => stdClass Object
(
[created] => 2013-02-20T13:56:57Z
[updated] => 2013-02-20T13:56:57Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777689/category/Gmail
[id] => 2
[name] => Gmail
)
)
[8] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777707
[isConnection] => false
[id] => 16777707
[fields] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777707/email/16778247
[id] => 16778247
[type] => email
[value] => afiliados@888.com
[editedBy] => OWNER
)
[categories] => stdClass Object
(
[created] => 2013-02-20T13:56:57Z
[updated] => 2013-02-20T13:56:57Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777707/category/Gmail
[id] => 2
[name] => Gmail
)
)
[9] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777856
[isConnection] => false
[id] => 16777856
[fields] => stdClass Object
(
[created] => 2012-11-19T16:00:44Z
[updated] => 2012-11-19T16:00:44Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777856/email/16778470
[id] => 16778470
[type] => email
[value] => agentwebhelp@aol.com
[editedBy] => OWNER
)
[categories] => stdClass Object
(
[created] => 2013-02-20T13:56:57Z
[updated] => 2013-02-20T13:56:57Z
[uri] => http://social.yahooapis.com/v1/user/VTHOM3XZ6UPO3ISGP3ZD43PQOQ/contact/16777856/category/Gmail
[id] => 2
[name] => Gmail
)
)
)
)
I just can't get through with my mind on this. Any help, direction very much appreciated.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to properly parse a stdobject

Post by requinix »

Needing two arrays seems wrong but it's not like you explained what they're for.

Code: Select all

$my_arr = $my_arr2 = array();
foreach ($object->contact as $contact) {
	$name = $email = "";
	foreach ($contact->fields as $field) {
		if ($field->type == "name") {
			$name = trim("{$field->value->givenName} {$field->value->middleName} {$field->value->familyName}");
		}
		if ($field->type == "email") {
			$email = $field->value;
		}
	}
	$my_arr[] = $name;
	$my_arr2[] = $email;
}
Now your job is to explain to me how that works. Oh, and you might need an html_entity_decode() in there.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: How to properly parse a stdobject

Post by lovelf »

Thank you very much man, I spent days trying to figure out a solution for this.

It retrieves with Yahoo API SDK the latest a getallcontacts function that runs the yql and returns that, then with the arrays of name and email under a same key I use them to allow for invites by email or check for an existing user in the database registered under that email to allow to send a friend request, just like on facebook website under find friends using yahoo as the api. I have it working already flawlessly for gmail and windows live but this was missing.

Yet I encountered a php error,

Code: Select all

Trying to get property of non-object
as

Code: Select all

[fields] => Array
is inside another stdClass Object.

Please tell me how would the new syntax go to get to

Code: Select all

foreach ($contact->fields as $field) {
Being $contact->fields actually inside another stdClass Object.

Also, am I allowed to send a donation to a PayPal account or is this disallowed in the forums? Just fill like sending over some drinks.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: How to properly parse a stdobject

Post by lovelf »

It seems it works just perfectly, nevermind the errors, I just have to check for valid this and that to allow the parsing and it's done.

Thank you!!
Post Reply