SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Neatness and efficiency with arrays and loops

    Hello,

    I've made this loop using several arrays:
    PHP Code:
    $email = array(
    'email1',
    'email2',
    'email3',
    'email4',
    'email5',
    'email6'
    );

    $name = array(
    'name1',
    'name2',
    'name3',
    'name4',
    'name5',
    'name6'
    );


    $description = array(
    'The Chairman is...',
    'The Treasurer is...',
    'The Secretary is...',
    'The Stores & Catering person is...',
    'The Social Sec is...',
    'The Webmaster is...',
    );

    $position = array(
    'name1 is in charge of...',
    'name2 is in charge of...',
    'name3 is in charge of...',
    'name4 is in charge of...',
    'name5 is in charge of...',
    'name6 is in charge of...'
    );

    foreach(
    $position as $key => $value) {
      echo 
    "Position: $value<br>\n";
      echo 
    "Name: $name[$key]<br>\n";
      echo 
    "Email: $email[$key]<br>\n";
      echo 
    "Description: $description[$key]<br>\n";
      } 
    I'd like to know if this is the best way to go about it, in terms of good practice, speed or anything else. Basically, I'm asking "is this what you gurus would do?"

  2. #2
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd probably throw into one multi dimensional array, but it's all a matter of style. I don't think one is any faster than the other.

    PHP Code:
    $person[0]['position'] = 'name1 is in charge of';
    $person[0]['description'] = 'The chairman is..';
    ... 

  3. #3
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With that number of array elements I'd do whatever is easiest. Don't think you'll take much of a performance hit either way.
    Search and compre web hosts
    Website hosting directory

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the feedback. I think I'll just stick to what I've got, seems to be the clearest way of doing it anyway, because elements might be added to the arrays if new positions are created in future years.

  5. #5
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's nothing wrong with the example you've posted, although I imagine I would have structured an object somewhere along the line.
    I think a for loop in place of the foreach would be clearer, but that's a matter of opinion:
    PHP Code:
    for($i 0$i count($name); $i++) {
        echo 
    "Position: $position[$i]<br>\n";
        echo 
    "Name: $name[$i]<br>\n";
        echo 
    "Email: $email[$i]<br>\n";
        echo 
    "Description: $description[$i]<br>\n";


  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ah, I thought about using for(), but I didn't know count() existed. You're right, that is a bit clearer. I've got used to working with arrays using foreach() so I've only used for() when the amount of elements in the array is known.

    Still, now I know about count(), which is a useful to thing to have picked up.

  7. #7
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trib4lmaniac
    There's nothing wrong with the example you've posted, although I imagine I would have structured an object somewhere along the line.
    I think a for loop in place of the foreach would be clearer, but that's a matter of opinion:
    PHP Code:
    for($i 0$i count($name); $i++) {
        echo 
    "Position: $position[$i]<br>\n";
        echo 
    "Name: $name[$i]<br>\n";
        echo 
    "Email: $email[$i]<br>\n";
        echo 
    "Description: $description[$i]<br>\n";

    Just a matter of opinion

    I find a huge speed increase when using count() in a for loop such as:
    PHP Code:
    $count count($name);
    for(
    $i 0$i $count$i++) {
        echo 
    "Position: $position[$i]<br>\n";
        echo 
    "Name: $name[$i]<br>\n";
        echo 
    "Email: $email[$i]<br>\n";
        echo 
    "Description: $description[$i]<br>\n";

    That way, the function doesn't have to run/count the same array for each loop

  8. #8
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by psalzmann
    the function doesn't have to run/count the same array for each loop
    You are indeed correct. I'm just lazy

  9. #9
    SitePoint Member phpandme's Avatar
    Join Date
    Apr 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    for($i = 0; $i < count($name); $i++) {
    echo "Position: $position[$i]<br>\n";
    echo "Name: $name[$i]<br>\n";
    echo "Email: $email[$i]<br>\n";
    echo "Description: $description[$i]<br>\n";
    }

    How would you get the above array item (like just: $position variable ) to insert into mysql db as individual items??? thanks!!

  10. #10
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    mysql_query("INSERT INTO employment_info(name,email,description) VALUES('" . $name[$i] . "','" . $email[$i] . "','" . $description[$i] . "')");


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
  •