Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy increment an array's index

    I have an
    $arr = explode("|", $str);
    from wich i need to display all of it's values. ($arr['0'] ; $arr['1'] ...)
    I cant know the number of $arr's parts for shure. Thats why I tryed Repeating()

    function Repeating($count)
    {
    for ($i=0;$i<=$count;$i++)
    {
    $r="$arr[' ".$i." ']";
    echo $r;
    }
    }

    Unfortenetly this doesn't work becouse "echo $r;" gets the value of $r and not further (to the value of the variable formed in $r)

    Thx for any idias

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Loop through the array - take a look at foreach:
    http://us3.php.net/foreach

    If you need to get the number of elements in the array look at count():
    http://us3.php.net/count

    Good luck;

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You're function requires that it be given an array, and that $arr is available for the function. You can do this by either globalizing the $arr or by passing it in as an argument (recommended).
    PHP Code:
    function Repeater($arr)
    {
        
    $count count($arr);
        for (
    $i 0$i $count$i++)
        {
            
    $r $arr[$i];
            echo 
    $r "\n";
        }
    }
    // Or
    function Repeater()
    {
        global 
    $sameArrayName;
        
    $count count($sameArrayName);
        for (
    $i 0$i $count$i++)
        {
            
    $r $sameArrayName[$i];
            echo 
    $r "\n";
        }

    Since the name has to be the same, this is one of the reasons why passing it as an argument is recommended. Another option is a foreach loop, which allows you to separate the key / value pairs, and works on arrays with associated string keys:
    PHP Code:
    foreach ($arr AS $key => $val// Or foreach ($arr AS $val)
    {
        
    printf("The value at index %s is %s\n"$key$val);

    For loop does not work (as shown above) on string indexed values. This doesn't mean you cannot make it work, but a foreach really simplifies the process. The only requirement is that you should ensure that $arr is an array with is_array($arr) before performing the foreach, otherwise it will trigger a notice.

    Hope that helps!

    Edit:
    Bah, beaten by Brandoe :P
    Back to ASP with you
    Last edited by Fou-Lu; 08-12-2008 at 08:28 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    Gabarieko (08-12-2008)

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    lol your reply is much more descriptive though!

  • #5
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up

    It realy helped a lot!
    Thanks for the great post. I now saw my mistake

    (It works great)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •