#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171

    Printing multiple dimentional array elements


    Hello;

    Can someone please teach me how to print arrays like this?
    PHP Code:
    Array
    (
        [
    0] => Array
            (
                [
    0] => stdClass Object
                    
    (
                        [
    id] => 81
                        
    [photo] => 81.jpg
                    
    )

                [
    1] => stdClass Object
                    
    (
                        [
    id] => 80
                        
    [photo] => 80.jpg
                    
    )
    .
    .
    .

               
            )


    I am confused with the difference between these:
    PHP Code:
    echo $results->id;
    echo 
    $results['id'];
    Echo 
    $results[0]['id']; 
    I have learned how it works but please also please explain this to clear things:
    PHP Code:
    foreach($results as $val =>$id)
     {
             
     }
    or
    while(
    $results $object->fetchArray())
      {
         echo 
    $results->id;

    Thanks
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,294
    Rep Power
    9400
    Originally Posted by zxcvbnm
    Can someone please teach me how to print arrays like this?
    That would be the output from print_r.

    Originally Posted by zxcvbnm
    I am confused with the difference between these:
    PHP Code:
    echo $results->id// $results is an object, id is a property
    echo $results['id']; // $results is an array, id is a string key
    Echo $results[0]['id']; // $results is a 2d array, [0] is the first dimension, id is in the second dimension 
    Originally Posted by zxcvbnm
    I have learned how it works but please also please explain this to clear things:
    PHP Code:
    foreach($results as $val =>$id)
     {
             
     }
    or
    while(
    $results $object->fetchArray())
      {
         echo 
    $results->id;

    Those do two different things. They are not interchangeable.
    First one: $results is an array and for each key/value pair in it, $val will be a key and $id will be a value.
    Second one: $object is some object, fetchArray() returns some kind of object (which I can tell by looking at how $results is used), and the id is a member of that object.
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171
    Originally Posted by requinix
    .
    Thank you, all clear and make sense. 1 thing I forgot to ask in example above. What would be the best way to access the array's element. For example id 81 or photo 81.jpg? I create it like this in the model:
    PHP Code:
    $results_and_rows = array('results'=>array($results), 'number_of_rows'=>$rows); 
    Thank you
    Last edited by zxcvbnm; November 6th, 2012 at 03:27 AM.
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,294
    Rep Power
    9400
    There's only ever one option: either it's an object and you use ->id or it's an array and you use ["id"].

    Start at the outermost "level" of the print_r() output and work your way in:
    Code:
    Array <-- (1) array
    ( 
        [0] => Array <-- (2) array
            ( 
                [0] => stdClass Object  <-- (3) object
                    ( 
                        [id] => 81 <-- (4) value you're trying to get
                        [photo] => 81.jpg 
                    ) 
    ...
    If the variable you dumped out was $x then that ID would be
    PHP Code:
    $x /* 1 */ [0/* 2 */ [0/* 3 */ ["id"/* 4 */
    $x[0][0]["id"
    [edit] The above should be ->id >_> [/edit]

    It looks like the third dimension ($x[0][...]) has multiple items so you'll probably want a foreach over that. Can't tell from the dump whether that's true for the first or second dimensions.
    Last edited by requinix; November 6th, 2012 at 12:08 PM.
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171
    Originally Posted by requinix
    It looks like the third dimension ($x[0][...]) has multiple items so you'll probably want a foreach over that. Can't tell from the dump whether that's true for the first or second dimensions.
    Oh thanks. Still working on it.
    Now that we've gone this deep please have a look at how it all began. Model that gets a list of images from the database:
    PHP Code:
    class Load_photos_model extends CI_Model
        
    {
            function 
    check()
                {
                    
    $query $this->db->query("SELECT * FROM photos ORDER BY id DESC");
                    if(
    $query->num_rows()>0)
                        {
                            
    $results $query->result();
                            
    $rows $query->num_rows();
                            
    $results_and_rows = array('results'=>array($results), 'number_of_rows'=>$rows);
                            return 
    $results_and_rows;
    /*If you have a better way of sending data back to controller please advice */
                        
    }
                    else
                        {
                            return 
    false;
                        }    
                }
        } 
    And the controller that calls this model:
    PHP Code:
    public function load_photos()
            {
                
    $this->load->model('load_photos_model');
                
    $photos $this->load_photos_model->check();
                if(
    $photos)
                    { 
                                      
                                           
    print_r($photos['results']);
                                           
    //Need to somehow put this in $data and send to view
                        
    exit(); //As I'm testing    
                        
    $data['photos_message'] = "There are ".$photos_num_rows." photos uploaded so far";
                        
    $this->load->vars($data);
                        return 
    true;
                    }
                else
                    {
                        
    $data['photos_message'] = "There are no photos uploaded yet!";
                        
    $this->load->vars($data);
                    }
            } 
    Which prints something like this:
    Code:
    Array
    (
        [0] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => 100
                        [photo] => 100.jpg
                    )
    
                [1] => stdClass Object
                    (
                        [id] => 99
                        [photo] => 99.jpg
                    )
    
                [2] => stdClass Object
                    (
                        [id] => 98
                        [photo] => 98.jpg
                    )
    
                [3] => stdClass Object
                    (
                        [id] => 97
                        [photo] => 97.jpg
                    )
    edit:

    I got the answer like this:
    PHP Code:
    if($photos)
                    {
                        
                        foreach(
    $photos['results'] as $val)
                            {
                                foreach(
    $val as $row)
                                    {
                                        echo 
    $row->photo;
                                    }
                            }
                        
                        exit(); 
    But I am not sure if foreach in foreach is a good idea. Is it?
    Last edited by zxcvbnm; November 6th, 2012 at 05:03 AM.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    Originally Posted by zxcvbnm
    But I am not sure if foreach in foreach is a good idea. Is it?
    What's the point of wrapping the array of the result rows in another array? I'm talking about this line:

    PHP Code:
    $results_and_rows = array('results'=>array($results), 'number_of_rows'=>$rows); 
    Why not simply write 'results' => $results?

    In any case, the nested foreach loop is completely unnecessary. Since it's a one-element array, the outer loop could simply be replaced with $val = $photos['results'][0].
  12. #7
  13. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171
    Originally Posted by Jacques1
    Hi,



    What's the point of wrapping the array of the result rows in another array? I'm talking about this line:

    PHP Code:
    $results_and_rows = array('results'=>array($results), 'number_of_rows'=>$rows); 
    Why not simply write 'results' => $results?
    Well as you can see this is happening in my model and it has to send back the results to the controller. Beside these values (photos), I also am returning another value from the array ('number_of_rows'=>$rows). So I thought putting them in a mulch-dimentional array is the best way. Is there any other ways to return multiple values from a function in a model?
    Thanks
  14. #8
  15. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4124
    There's two options


    1:
    PHP Code:
    //
    $return = array(
        
    'results'=>$results,            //notice how i took out the extra array construct?
        
    'number_of_rows'=>$rows
    ); 
    2:
    PHP Code:
    //
    return $results;    //why use the $rows when you can count? (okay, so sometimes counting si more expensive; it depends how you want to loop: for/foreach/while - only for needs the row count) 

    Comments on this post

    • English Breakfast Tea agrees : Thanks dude, got it. Who put that extra array there?!!
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  16. #9
  17. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by zxcvbnm
    So I thought putting them in a mulch-dimentional array is the best way. Is there any other ways to return multiple values from a function in a model?
    Thanks
    The outer array is OK. I meant this:

    PHP Code:
    'results'=>array($results
    Wrapping $results into an array makes no sense. The array serves absolutely no purpose, because you only have a single value (namely $results).
  18. #10
  19. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171
    Originally Posted by Jacques1
    The outer array is OK. I meant this:

    PHP Code:
    'results'=>array($results
    Wrapping $results into an array makes no sense. The array serves absolutely no purpose, because you only have a single value (namely $results).
    Oh oh oh, absolutely. Thanks, got it.
  20. #11
  21. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,294
    Rep Power
    9400
    And allow me to correct a blatant mistake I made:
    PHP Code:
    $x /* 1 */ [0/* 2 */ [0/* 3 */ ->id /* 4 */
    $x[0][0]->id 

    Comments on this post

    • ManiacDan agrees : Good catch

IMN logo majestic logo threadwatch logo seochat tools logo