SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict Shalin's Avatar
    Join Date
    Sep 2003
    Location
    __beyond
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using arrays smartly, need help!

    Hi guys,

    I have a simple problem statement but i need some ideas to achieve what I need effectively.

    I have an array which looks like this:

    Code:
    users[0]=Ted
    users[1]=23
    users[2]=Reed
    users[3]=32
    users[4]=John
    users[5]=43
    users[6]=Ray
    users[7]=35
    Now this array essentially contains a User's name and his age. I know I can write this array in a much better form but since I am extracting this information from another source this is the only form I can put it into.

    What I need from here on is: I want to form an associative array which looks like this
    Code:
    user["reed"]= 32
    user["John"]=43
    and essentially show this information in a tabular form. Now the tricky part here is I have a business rule that says:

    1. I have to begin taking users when it starts with REED (assuming the next array element is his age).
    2. I have to stop forming an array when I reach user RAY.
    3. So I have to print all the users with their age that follow in this range.

    My plan here is, I would find the array key for REED and RAY and then run a loop that would get me all the values. Since I am fairly new to PHP i don't know the exact syntax that can help me pull out this information.

    So if you have any idea how I can go about this, then please advice. I hope my question is clear enough. Thanks in advance.
    Help Desk Software - Helpdesk Pilot
    Flash Screensaver - Screenswift for Windows
    Online Polls - Tezaa.com
    Address Book Software - Addza for Windows

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe this will help:

    PHP Code:
    <?php

      $names 
    "Jeffery, Reed, John, Ted, Fred, Guido, jeff, Ray, Tina, Tess";
      
    $ages  "21, 12, 45, 32, 78, 14, 55, 64, 5, 98";
      
      
    $nameArray explode(", "$names);
      
    $ageArray  explode(", "$ages);
      
      
    $newArray = array();
      
      
    $trigger "don't add";
      
      foreach(
    $nameArray as $index => $value)
      {
        echo 
    $value.'<br />';
        if(
    $value == "Reed")
          
    $trigger "add one";
        if(
    $value == "Ray")
          
    $trigger "one more";
        if(
    $trigger == "add one")
          
    $newArray[$value] = $ageArray[$index];
          
        echo 
    $trigger.'<br />';
        
        if(
    $trigger == "one more")
        {
          
    $newArray[$value] = $ageArray[$index];
          break;
        }
      }
      
    print_r($newArray);
      
    ?>
    John

  3. #3
    SitePoint Addict Shalin's Avatar
    Join Date
    Sep 2003
    Location
    __beyond
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the quick reply!

    I took the idea and was able to determine and the start_key and end_key for my raw array. I added more tweaks to it that I wanted and I think I got what I wanted. Thanks indeed for all the help.
    Help Desk Software - Helpdesk Pilot
    Flash Screensaver - Screenswift for Windows
    Online Polls - Tezaa.com
    Address Book Software - Addza for Windows

  4. #4
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem.
    John

  5. #5
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could use array_flip() as well to get your keys more easily

  6. #6
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mr_Money
    you could use array_flip() as well to get your keys more easily
    array_flip() would work IF all your values are unique. If not, then you would lose elements in your array. However, there are other ways.

    www.php.net/array_flip
    John


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
  •