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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    'echo'ing a value from a 2D array?

    Is there any way to echo a value from a 2D array? When I try:
    PHP Code:
    echo "$array_2d[0][0]"
    I get:

    Array[0]

    But if I pull it to a variable first then echo the variable, it displays correctly. Is there any way to directly echo the array value or does it have to be this way? It isn't a problem, but I'd like to eliminate any extra, unnecessary variables from my code.

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    The quotes are unneeded and increases the load time of your script:

    PHP Code:
    echo $array_2d[0][0]; 

  • #3
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    if it's 2d you don't need to define the vertical dimension you can just do

    echo $array_2d[0];
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #4
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Mwnciau: I tried your suggestion and nothing displays.

    hinch: I tried your suggestion and it just returns "Array".

  • #5
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    <?php
    $darr = array('one','two','three','four');
    echo $darr[0];
    ?>

    works just fine take the number out of your variable atm you have array_2d change it to $arraytwod and it should work fine

    appears it doesn't like numbers in array var names don't ask me why i dont know
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #6
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    Quote Originally Posted by bryceowen View Post
    Mwnciau: I tried your suggestion and nothing displays.

    hinch: I tried your suggestion and it just returns "Array".
    Hinch, you are talking about a 1d array not 2d.

    Bryce, are you sure there is anything in 0, 0? This:

    PHP Code:
    <?php

    $array_2d 
    = array ( array ( '0, 0''0, 1''0, 2' ), '1', array ( '2, 0''2, 1' ) );

    echo 
    $array_2d[0][0] . '<br />';
    echo 
    $array_2d[2][1];
    outputs this:

    Code:
    0, 0
    2, 1


  •  

    Posting Permissions

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