PHP5 & Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tomoko
    New Member
    • Apr 2006
    • 1

    PHP5 & Array

    Code:
    $data[] = array('id' => 00001,'name'=>asuka);
    $data[] = array('id' => 00002,'name' =>ntt);
    $data[] = array('id' => 00003,'name' =>docomo);
    
    for($i=0; $i<3;$i++)
    {	
    	echo $data['id']."   ".$data['name']."<br>";
    }
    -------------------------------------------------------- :( :confused:
    something's wrong with this code.
    I wanna display the content of array($data).
    but it doesn't work. can somebody please tell me how to write correct code?
    Im stuck!! :eek:
    and also I wanna put those information into table which is 3 by 2.

    thanks :)
    Last edited by Markus; Nov 28 '08, 12:57 PM. Reason: added # tags
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Hi,

    I'm not sure what is wrong with your code. This leads me to what is wrong with your post:

    1. No clear indication of the problem - what is it doing , what is it not doing, what was expected.
    2. Please post code in code tags

    That said, I think the following code will do what you are after.
    Code:
    <?php
    $data=array() ;
    $data['00000'] = "asuka" ;
    $data['00001'] = "ntt" ;
    $data['00002'] = "docomo" ;
    
    for($i=0; $i<count($data);$i++)
    {	
    	$lcKey = str_pad($i, 5, '0', STR_PAD_LEFT) ;
    	echo $lcKey . " " . $data[$lcKey] . "<br />";
    }
    ?>
    If that's not what you want then please post again using code tags and giving a clearer outline of what the code needs to do and how it appears to be failing.

    Cheers
    nathj

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Tomoko,

      For the benefit of our experts and yourself, it is a posting guidelines that all users use [code] tags when posting code. This makes the code easier to read and, in turn, helps our experts answer your questions.

      An easy way to do this is to highlight the code in the textarea and then hit the '#' button at the top of the textarea.

      Please read the Post Guidelines so you're more familiar with how things work.

      Moderator.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by nathj

        That said, I think the following code will do what you are after.
        Code:
        <?php
        $data=array() ;
        $data['00000'] = "asuka" ;
        $data['00001'] = "ntt" ;
        $data['00002'] = "docomo" ;
        
        for($i=0; $i<count($data);$i++)
        {	
        	$lcKey = str_pad($i, 5, '0', STR_PAD_LEFT) ;
        	echo $lcKey . " " . $data[$lcKey] . "<br />";
        }
        ?>
        Pbmods would have your head for this!

        You should do your count($var) before the loop because, when it's inside the loop, count() is called with each iteration.

        But that's off-topic..

        Carry on!

        Comment

        • nathj
          Recognized Expert Contributor
          • May 2007
          • 937

          #5
          Originally posted by Markus
          Pbmods would have your head for this!

          You should do your count($var) before the loop because, when it's inside the loop, count() is called with each iteration.

          But that's off-topic..

          Carry on!
          I completely agree, but I knocked it up in 2 minutes just as lunch was being served and didn't make all the changes I wanted.

          nathj

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by nathj
            I completely agree, but I knocked it up in 2 minutes just as lunch was being served and didn't make all the changes I wanted.

            nathj
            I wasn't doubting you. My reply was more for OPs sake, than to correct you.

            Markus.

            Comment

            • nathj
              Recognized Expert Contributor
              • May 2007
              • 937

              #7
              Originally posted by Markus
              I wasn't doubting you. My reply was more for OPs sake, than to correct you.

              Markus.
              Clearly my over-fragile ego got the better of me (smile)

              Cheers
              nathj

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by nathj
                Clearly my over-fragile ego got the better of me (smile)

                Cheers
                nathj
                That expert title has gone to your head ;)

                Comment

                • itsmeaf
                  New Member
                  • Nov 2008
                  • 2

                  #9
                  Multi-dimensional array

                  Hey, tomoko

                  Two things wrong.

                  1. Defined a string in your array without quotes.

                  2. Trying to output an array instead of the values.

                  PS. if your want to keep the prefixed 0's then they need to be strings to.



                  Try this code below
                  Code:
                  $data[] = array('id' => "00001",'name'=>"asuka");
                  $data[] = array('id' => "00002",'name' =>"ntt");
                  $data[] = array('id' => "00003",'name' =>"docomo");
                   
                  foreach($data as $elem) // NOTE: $elem is also an array
                   {    
                      // now echo the bits of the $elem(ment)
                      echo $elem['id']."   ".$elem['name']."<br>"; 
                   }
                  // hope this sets you on track

                  the above is good to know esp. when connecting MySQL and using the mysql_fetch_ass oc function.

                  Comment

                  Working...