Web Analytics Made Easy -
StatCounter Newbie problem with arrays - CodingForum

Announcement

Collapse
No announcement yet.

Newbie problem with arrays

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newbie problem with arrays

    I've got this:

    PHP Code:
    <?php

    $names 
    = array("Abraham""Beth""Christina""David""Ellen""Fergus""Goliath""Hamish")

    ?>

    <html>
    <body>
    <p>I live next to 

    <?php 

    echo $names[0] . " ," $names[1] . " ," $names[2] . " ," $names[3] . " ," $names[4] . " ," $names[5] . " ," $names[6] . " and " $names[7] . ".";

    ?>

    </p>
    </body>
    </html>

    But when I run it, only "I live next to" shows up... Why?


    Regards,
    "DLA"
    Always Use a DOCTYPE!
    Always Use * {margin: 0; border: none; padding: 0;} in the top of your CSS stylesheet.
    Always Use only CSS for layout; never (X)HTML.
    Always Have fun while coding!

  • #2
    You need to loop through the array and echo each value. Use foreach for it

    PHP Code:
    <?php 

    foreach($names as $name)
    {
        echo 
    $name .", ";
    }

    Comment


    • #3
      PHP Code:
      <?php

      $names 
      = array("Abraham""Beth""Christina""David""Ellen""Fergus""Goliath""Hamish")

      $names array_keys($names);
      ?>

      <html>
      <body>
      <p>I live next to 

      <?php 

      echo $names[0] . " ," $names[1] . " ," $names[2] . " ," $names[3] . " ," $names[4] . " ," $names[5] . " ," $names[6] . " and " $names[7] . ".";

      ?>

      </p>
      </body>
      </html>

      Comment


      • #4
        You are missin some concatenators (decimals, periods, whatever you want to call them):
        PHP Code:
        echo $names[0] . " ," $names[1] . " ," $names[2] . " ," $names[3] . " ," $names[4] . " ," $names[5] . " ," $names[6] . " and " $names[7] . "."
        ... as well as a semi-colon after the $names array. error_reporting should have pointed this out.
        ZCE

        Comment


        • #5
          Originally posted by kbluhm View Post
          concatenators
          Thanks I always wondered what they were called. Although i think it's concatenates. Not that I'll probably remember that word
          You can not say you know how to do something, until you can teach it to someone else.

          Comment


          • #6
            The missing concatenates was what was missing. Thanks
            Always Use a DOCTYPE!
            Always Use * {margin: 0; border: none; padding: 0;} in the top of your CSS stylesheet.
            Always Use only CSS for layout; never (X)HTML.
            Always Have fun while coding!

            Comment


            • #7
              This code could easily be simplified to handle any number of names. You're currently assuming a fixed name count.
              PHP Code:
              $names = array( /* array of names */ );

              switch ( 
              count$names ) )
              {
                  case 
              0:
                      
              $names 'no one';
                      break;
                  case 
              1:
                      
              $names reset$names );
                      break;
                  default:
                      
              $last array_pop$names );
                      
              $names implode', ' $names ) . ' and ' $last;
              }

              echo 
              'I live next to '$names'.';
              // I live next to no one.
              // I live next to Abraham.
              // I live next to Abraham, Beth, Christina, David and Ellen. 
              Last edited by kbluhm; Apr 14, 2009, 01:26 PM.
              ZCE

              Comment

              Working...
              X