...

View Full Version : Array object iteration



Jacobb123
11-07-2010, 05:13 AM
I tried looking on google for this but wasn't sure what I was looking for:

I have an array object that looks like this:


Array
(
[0] => stdClass Object
(
[uid] => 1335845740
[changed_fields] => Array
(
[0] => name
[1] => picture
)

[time] => 232323
)

[1] => stdClass Object
(
[uid] => 1234
[changed_fields] => Array
(
[0] => friends
)

[time] => 232325
)

)

I just can't figure out how to iterate through it with a foreach loop. Can someone point me in the right direction on how to do this?

Keleth
11-07-2010, 05:19 AM
Well, assuming you have your class setup properly, and you can't access the class members directly without class methods, something like foreach ($allObjects as $object) will let you loop through your objects (as of PHP 5 anyway), then in the loop you'd have to call the class methods that would pull the data in the class itself.

jamied_usa
11-08-2010, 02:16 PM
You have an array of objects so you need to reference the contents of the array as an object:




for($i = 0; $i < count(yourObjArr); $i++);
{

$uid = yourObjArr[$i]->uid;
$changed_fields_array = yourObjArr[$i]->changed_fields;
$time = yourObjArr[$i]->time;

}



. . or convert it to an associative array . . .




for($i = 0; $i < count(yourObjArr); $i++);
{

newArray[] = array('uid' => yourObjArr[$i]->uid,
'changed_fields' => yourObjArr[$i]->changed_fields,
'time' => yourObjArr[$i]->time);


}

print_r(newArray);



As Keleth mentioned, this type of referencing may only work for PHP5+ . . .




I tried looking on google for this but wasn't sure what I was looking for:

I have an array object that looks like this:


Array
(
[0] => stdClass Object
(
[uid] => 1335845740
[changed_fields] => Array
(
[0] => name
[1] => picture
)

[time] => 232323
)

[1] => stdClass Object
(
[uid] => 1234
[changed_fields] => Array
(
[0] => friends
)

[time] => 232325
)

)

I just can't figure out how to iterate through it with a foreach loop. Can someone point me in the right direction on how to do this?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum