SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Sep 2001
    Location
    UK
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    multidimensional array

    Hi there,

    I am attempting to populate a 2 dimensional array through a loop but I'm struggling somewhat. I'd be grateful if you could help. Loop 1 should be adding each 'row' and loop 2 should output it all at the end. The code is fairly self explanatory.

    PHP Code:
    // loop 1
    for ($a 0$a 10$a++) {
        
    $sample = array($a,"jack".$a"ted".$a,"jim".$a);
    }

    //loop 2
    for ($a 0$a 10$a++) {
        echo 
    $sample[$a][0] . " | " $sample[$a][1] . " | " $sample[$a][2] . "<br>";

    I was hoping for it to output:
    jack0 | ted0 | jim0
    jack1 | ted1 | jim1
    jack2 | ted2 | jim2
    jack3 | ted3 | jim3
    jack4 | ted4 | jim4
    jack5 | ted5 | jim5
    jack6 | ted6 | jim6
    jack7 | ted7 | jim7
    jack8 | ted8 | jim8
    jack9 | ted9 | jim9
    but it's actually outputting:
    | |
    j | a | c
    t | e | d
    j | i | m
    | |
    | |
    | |
    | |
    | |
    | |
    Thanks in advance.

  2. #2
    SitePoint Addict -Ice-php's Avatar
    Join Date
    May 2002
    Location
    UK
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I have modified your code so that it outputs as you expected:

    PHP Code:
    // loop 1
    for ($a 0$a 10$a++) {
        
    $sample[] = array("jack".$a"ted".$a,"jim".$a);
    }

    //loop 2
    for ($a 0$a 10$a++) {
        echo 
    $sample[$a][0] . " | " $sample[$a][1] . " | " $sample[$a][2] . "<br>";

    -Ice

  3. #3
    SitePoint Addict
    Join Date
    Sep 2001
    Location
    UK
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So simple! School boy error.

    Big thanks -Ice-php.

  4. #4
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, Ice-php is right. Also don't forget to instantiate the $sample array.

    PHP Code:
    $sample=array();
    // loop 1
    for ($a 0$a 10$a++) {
        
    $sample[] = array("jack".$a"ted".$a,"jim".$a);
    }

    //loop 2
    for ($a 0$a 10$a++) {
        echo 
    $sample[$a][0] . " | " $sample[$a][1] . " | " $sample[$a][2] . "<br>";


  5. #5
    SitePoint Addict
    Join Date
    Sep 2001
    Location
    UK
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by REMIYA View Post
    Also don't forget to instantiate the $sample array.
    Thanks Remiya. My code works as -Ice posted it, is it necessary to instantiate the $sample array?

  6. #6
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Azaar View Post
    Thanks Remiya. My code works as -Ice posted it, is it necessary to instantiate the $sample array?
    If it works it is OK. However if you port the code on a different server, it is better to be on the safe side, and instantiate it.

    And it is also easier to read it later, if you modify it.

  7. #7
    SitePoint Addict
    Join Date
    Sep 2001
    Location
    UK
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Will do, thanks a lot.


Bookmarks

Posting Permissions

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