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 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2002
    Location
    New York, NY
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Multidimen Array ... printing elements

    So, I have a multidimensional array called $cats (see below for the actual array). I am trying to loop through the array, and print certain elements (of each "group" or "subarray"). I tried the code below, but the php keeps "stopping"...

    PHP Code:
    <?php
    // This shows the full array (see below)
    ?><!-- Catsarray full: <?php print_r($cats); ?>--><?php

    // This shows the elements in the 1st array
    ?><!-- CatsArray: <?php print_r($cats[0]); ?>--><?php

    // This SHOULD look through and show each 3 
    // of the elements (in each of the subarrays). 
    // But it doesn't. It just prints one "ID:" and stops the loop.

    for( $i=$i count($cats); $i++ ) {
            echo 
    'ID: ';
            echo 
    $cats[$i][9];
            echo 
    'Name: ';
            echo 
    $cats[$i][1];
            echo 
    'Slug: ';
            echo 
    $cats[$i][2];
            echo 
    '<br /><br />';
    }
    echo 
    "<br><br>";
    ?>
    And here is the array (as printed out in the first and second print_r above, before the FOR)...

    Code:
    <!-- Catsarray full: Array
    (
        [0] => stdClass Object
            (
                [term_id] => 319
                [name] => Artists &amp; Decor
                [slug] => artists
                [term_group] => 0
                [term_taxonomy_id] => 322
                [taxonomy] => category
                [description] => 
                [parent] => 299
                [count] => 332
                [cat_ID] => 319
                [category_count] => 332
                [category_description] => 
                [cat_name] => Artists &amp; Decor
                [category_nicename] => artists
                [category_parent] => 299
            )
    
        [1] => stdClass Object
            (
                [term_id] => 335
                [name] => Kitchen/Bath Design
                [slug] => kitchenbath-design
                [term_group] => 0
                [term_taxonomy_id] => 338
                [taxonomy] => category
                [description] => 
                [parent] => 319
                [count] => 332
                [cat_ID] => 335
                [category_count] => 332
                [category_description] => 
                [cat_name] => Kitchen/Bath Design
                [category_nicename] => kitchenbath-design
                [category_parent] => 319
            )
    
        [2] => stdClass Object
            (
                [term_id] => 336
                [name] => Photography
                [slug] => photography-artists
                [term_group] => 0
                [term_taxonomy_id] => 339
                [taxonomy] => category
                [description] => 
                [parent] => 319
                [count] => 332
                [cat_ID] => 336
                [category_count] => 332
                [category_description] => 
                [cat_name] => Photography
                [category_nicename] => photography-artists
                [category_parent] => 319
            )
    
    )
    --><!-- CatsArray: stdClass Object
    (
        [term_id] => 319
        [name] => Artists &amp; Decor
        [slug] => artists
        [term_group] => 0
        [term_taxonomy_id] => 322
        [taxonomy] => category
        [description] => 
        [parent] => 299
        [count] => 332
        [cat_ID] => 319
        [category_count] => 332
        [category_description] => 
        [cat_name] => Artists &amp; Decor
        [category_nicename] => artists
        [category_parent] => 299
    )
    -->
    Please help. Thanks.


    Note: I also tried...

    PHP Code:
    for( $i=$i count($cats); $i++ ) {
            echo 
    'ID: ';
            echo 
    $cats[$i]['cat_ID'];
            echo 
    'Name: ';
            echo 
    $cats[$i]['name'];
            echo 
    'Slug: ';
            echo 
    $cats[$i]['slug'];
            echo 
    '<br /><br />';

    But that didn't work either...
    Last edited by Jabbamonkey; 01-27-2014 at 10:52 PM.

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    63
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Try this.

    Code:
    foreach( $cats as $cat ) {
            echo 'ID: ';
            echo $cat['cat_ID'];
            echo 'Name: ';
            echo $cat['name'];
            echo 'Slug: ';
            echo $cat['slug'];
            echo '<br /><br />';
    }

  • #3
    New Coder
    Join Date
    Aug 2002
    Location
    New York, NY
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Still not working..

    Nope. Same thing is happening. I can't figure it out.

    So, I ran this....

    PHP Code:
    <?php 
    foreach( $cats as $cat ) {
            echo 
    'ID: ';
            
    ?><!-- CatsArray: <?php print_r($cat); ?>--><?php
            
    echo $cat['cat_ID'];
            echo 
    'Name: ';
            echo 
    $cat['name'];
            echo 
    'Slug: ';
            echo 
    $cat['slug'];
            echo 
    '<br /><br />';
    }
    ?>
    And it printed out this (in the source code):
    [Note: There was html above this, and NOTHING below this code. So something is making the code stop loading the rest of the content]

    Code:
    ID: <!-- CatsArray: stdClass Object
    (
        [term_id] => 319
        [name] => Artists &amp; Decor
        [slug] => artists
        [term_group] => 0
        [term_taxonomy_id] => 322
        [taxonomy] => category
        [description] => 
        [parent] => 299
        [count] => 332
        [cat_ID] => 319
        [category_count] => 332
        [category_description] => 
        [cat_name] => Artists &amp; Decor
        [category_nicename] => artists
        [category_parent] => 299
    )
    -->

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    These are objects, not arrays.
    PHP Code:
    $cat->cat_ID;
    $cat->name;
    $cat->slug
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New Coder
    Join Date
    Aug 2002
    Location
    New York, NY
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Awesome. That did it. Thanks.


    So, for layman like me... in this case, do not use:

    PHP Code:
    echo $cat['cat_ID']; 
    Use this instead...

    PHP Code:
    $cat->cat_ID

  • #6
    New Coder
    Join Date
    Sep 2011
    Posts
    63
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by Fou-Lu View Post
    These are objects, not arrays.
    PHP Code:
    $cat->cat_ID;
    $cat->name;
    $cat->slug
    Just noticed that myself!

  • #7
    New Coder
    Join Date
    Nov 2011
    Location
    Ratio, Logic
    Posts
    55
    Thanks
    3
    Thanked 6 Times in 6 Posts
    Little more clarification: when for debugging purposes is printed out or even better var dumped stdClass Object
    it is clear sign that you have an object, not an array so you have to approach it's property not array's element.
    Also it can be smart to use variable handling functions is_array($array) and/or is_object($object) to check what we have got there.


  •  

    Tags for this Thread

    Posting Permissions

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