Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    311
    Thanks
    0
    Thanked 1 Time in 1 Post

    Array object iteration

    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:

    PHP Code:
    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?

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,537
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.

  • #3
    New Coder
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    0
    Thanked 5 Times in 5 Posts
    You have an array of objects so you need to reference the contents of the array as an object:

    PHP Code:

    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 . . .

    PHP Code:

    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+ . . .



    Quote Originally Posted by Jacobb123 View Post
    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:

    PHP Code:
    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?
    Last edited by jamied_usa; 11-08-2010 at 01:44 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •