Advanced Search

Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: array problem

  1. #1
    Join Date
    Oct 2008
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default array problem

    plz check this:

    PHP Code:
    function info(){
        
    $a = array(
                
    'Name'=>'Othman',
                
    'Nickname'=>'vividona',
                
    'Age'=>'35',
                
    'Nationalit'=>'Sudan');
        foreach(
    $a as $k=>$v){
            while(list(
    $k$v) = each($a)){
                echo 
    "$k=>$v<br />";
            }
        }

    }
    info(); 
    this code print all array except array[0] why???

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    To answer your question, when each() is called, it moves the array pointer forward. Thus when your loop runs for the first time, the pointer has already been moved to the second index and outputs 'Nickname' => 'Vividona' .

    With that being said, I don't completely understand what is going on inside your function.

    If someone could put this into english for me that would be great: list($k, $v) = each($a)

    I know what list() and each() do separately, but I can't get my head around what list($k, $v) = each($a) is doing.

    Also, like I answered above, it seems each is pushing the array pointer forward, and spitting out starting with the the second index. I guess this is happening because each() is called prior to the echo. Am I right?

  3. #3
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    From what I see, this code looks a little redundant. This should be a better solution:
    PHP Code:
    <?php
    function info(){
        
    $a = array(
                
    'Name'=>'Othman',
                
    'Nickname'=>'vividona',
                
    'Age'=>'35',
                
    'Nationalit'=>'Sudan');
        foreach(
    $a as $k => $v){
            echo 
    $k '=>' $v '<br />';
        }

    }
    As for why it was skipping the first item; it did that because when it got to the foreach statement, it advanced the array pointer one ahead.

    EDIT:
    Jason beat me to it.

  4. #4
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Quote Originally Posted by techietim View Post
    EDIT:
    Jason beat me to it.
    Yeah, but that code confused Jason too. So I raised some questions of my own. It does seem like a crazy way to go about doing things.

    Tim, can you explain what exactly is happening with this: list($k, $v) = each($a)

  5. #5
    Join Date
    Jan 2008
    Posts
    4,158
    Thanks
    28
    Thanked 623 Times in 619 Posts
    Blog Entries
    1

    Default

    List goes through arrays, and each gets the "current key and value pair from an array and advance the array cursor."

    http://us2.php.net/each
    http://us2.php.net/list
    Jeremy | jfein.net

  6. #6
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    list is a language construct which is used to assign array items to variables.

    Example:
    PHP Code:
    list($name$age) = array('Tim'105); 
    each returns an array with the key and value of the current array element, then advances the array pointer.

    Example:
    PHP Code:
    $array = array('name' => 'Tim''age' => 105);
    var_dump(each($array)); //0 => name, 1 => Tim
    var_dump(each($array)); //0 => age, 1 => 105
    var_dump(each($array)); //false 
    When it reaches the end, it returns false. So when used in a while loop, it loops through the array (starting at where the pointer is) and stops when it gets to the end.

  7. #7
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Yeah, I more or less understand what each do separately, but when used like this:
    PHP Code:
    list($k$v) = each($a
    is kinda difficult for me to get my head around. each() returns an array with 4 elements. Both numeric keys and associative keys.

    So list($k, $v) is assigning the value of the first two elements [0] and [1] of the array in each(), to $k and $v respectively?

    I think this is what is happening. But I could be wrong. Since there are only two variables in list($k, $v), the remaining two values that are returned by each() are just ignored?

    Also, I am thinking that there is no practical use for this. If there is one, I'd love to see an example. I really can't see what the point of Vividona's original function is. But again, I am totally capable of missing the point. So if there is one, please fill me in.

  8. #8
    Join Date
    Oct 2008
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thank you guys for your nices replies.

    but how can I include the array[0] in foreach?

  9. #9
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    PHP Code:
    function info($a){

        foreach(
    $a as $k=>$v){
            
                echo 
    "$k=>$v<br />";
        
        }

    }

    $a = array(
            
    'Name'=>'Othman',
            
    'Nickname'=>'vividona',
            
    'Age'=>'35',
            
    'Nationalit'=>'Sudan');
            
    info($a); 
    Please tell me what you were trying to do with this: while(list($k, $v) = each($a))

  10. #10
    Join Date
    Mar 2009
    Posts
    65
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default

    Quote Originally Posted by JasonDFR View Post
    Please tell me what you were trying to do with this: while(list($k, $v) = each($a))
    I have no idea either.. It's not really needed.

    the code will basically take an the key and value from array $a and assign it to $k and $v. But since the foreach has already move the iterator forward, when you reach the while(list...) part, the iterator is already pointing to the second element.

    Either use the foreach, or the while(list($k, $v), but not both...

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
  •