Number In An Array

Discussion in 'PHP' started by !Unreal, Jan 29, 2009.

  1. #1
    I have this...

    
    <?php
    $links = explode("\n", $_POST['links']); 
    
    foreach ($links as $links)
    {
      echo '[PART="NUM"]'.$links.'[/PART] <br />';
    }
    
    ?>
    
    PHP:
    Where it says NUM I want the number of its position in the array.

    So if I fed this to this script:
    
    http://www.google.com
    http://www.yahoo.com
    http://www.ask.com
    
    Code (markup):
    The script would then output:

    
    [PART="1"]http://www.google.com[/PART]
    [PART="2"]http://www.yahoo.com[/PART]
    [PART="3"]http://www.ask.com[/PART]
    
    Code (markup):
    How would I go about doing this?
     
    !Unreal, Jan 29, 2009 IP
  2. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    
    <?php
    $links = explode("\n", $_POST['links']);
    $num=0;
    foreach ($links as $links)
    {
    $num++;
    echo '[PART="'.$num.'"]'.$links.'[/PART] <br />';
    }
    ?>
    
    PHP:
     
    rohan_shenoy, Jan 29, 2009 IP
  3. InputProductions

    InputProductions Banned

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $links= “http://www.google.com;http://www.yahoo.com;http://www.ask.com";
    
    $websites= explode(”;”, $links); 
    
    $websites= Array ( [0] => http://www.google.com [1] => http://www.yahoo.com [2] => http://www.ask.com ) 
    
    echo $websites[0];
    echo $websites[1];
    echo $websites[2];
    PHP:
    You may be running into a slight issue with the http:// part as this may be seen as a PHP comment. You can try leaving that part out and doing something like this:

    echo "http://$websites[0]";
    echo "http://$websites[1]";
    echo "http://$websites[2]";
    PHP:
     
    InputProductions, Jan 29, 2009 IP
  4. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #4
    ^
    Do you understand what a rubbish reply that was?
    Seriously man, if you don't know how to code in PHP, stay away instead of offering useless solutions.
     
    rohan_shenoy, Jan 29, 2009 IP
  5. InputProductions

    InputProductions Banned

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ^ Why? Whats wrong with it? There are a ton of ways to end up at the same place in the end. That's one solution.
     
    InputProductions, Jan 29, 2009 IP
  6. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #6
    <?php
    
    $links = <<< links
    http://www.google.com
    http://www.yahoo.com
    http://www.ask.com
    links;
    
    
    /* Gen */
    $i = 0 or $links = explode("\n", str_replace("\r", '', $links));
    foreach ( $links as $link )
        echo '[PART=' . ++$i . ']' . $link . '[/PART]' . PHP_EOL;
    PHP:
     
    Danltn, Jan 29, 2009 IP
  7. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I'm gonna go with the variable that can already be passed through a foreach() loop instead of creating my own. Really, though, InputProductions, your reply was just not very helpful. lol

    <?php
    $links = explode("\n", $_POST['links']);
    
    foreach ($links as $id=>$links)
    {
      echo '[PART="'.(++$id).'"]'.$links.'[/PART] <br />';
    }
    
    ?>
    PHP:
     
    zerxer, Jan 29, 2009 IP
  8. !Unreal

    !Unreal Well-Known Member

    Messages:
    1,671
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    165
    #8
    Thanks for all your replies.

    I found this was the best solution:

    
    $count = count($links);
    for($i=0;$i<$count;$i++)
    {
     echo '[PART="'.($i+1).'"]'.$links[$i].'[/PART] <br />';
     }
    
    PHP:
     
    !Unreal, Jan 29, 2009 IP