tags:

views:

66

answers:

7

I don't understand the each() and the list() function that well. Can anyone please give me a little more detail and explain to me how can it be useful?

Edit:

<?php
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo);
print_r($bar);
?>

Array
(
    [1] => bob
    [value] => bob
    [0] => 0
    [key] => 0
)

So does this mean in array[1] there's the value bob, but it's clearly in array[0]?

+2  A: 

The each() function returns the current element key and value, and moves the internal pointer forward. each()

The list function — Assigns variables as if they were an array list()

Example usage is for iteration through arrays

  while(list($key,$val) = each($array))
    {
      echo "The Key is:$key \n";
      echo "The Value is:$val \n";

    }
streetparade
Need more details please.
Doug
+2  A: 

list is not a function per se, as it is used quite differently.

Say you have an array

$arr = array('Hello', 'World');

With list, you can quickly assign those different array members to variables

list($item1, $item2) = $arr; //$item1 equals 'Hello' and $item2 equals 'World'
Tim Cooper
How might list be useful?
Doug
@Doug — Functions which return multiple values, for one. Say you have a function which, when passed a filename, will return both its extension and MIME type as a two-element array. You could then capture both values as e.g. `list($ext, $mime) = getTypes($filename);`.
Ben Blank
Simpler, less verbose array-value extraction into the local scope.Note that, sadly, it only works on numerically indexed arrays.
Tchalvak
@Doug: Very simple example: `list($firstname, $surname) = explode(' ', "Tim Cooper");` Generally, it helps when you have any sort of array, but want to assign its values to uniquely named variables. Why you might want to do *that* depends on the situation. :)
deceze
+1  A: 

list isn't a function, it is a language construct. It is used to assign multiple values to different variables.

list($a, $b, $c) = array(1, 2, 3);

Now $a is equal to 1, and so on.

Every array has an internal pointer that points to an element in its array. By default, it points to the beginning.

each returns the current key and value from the specified array, and then advances the pointer to the next value. So, put them together:

list($key, $val) = each($array);

The RHS is returning an array, which is assigned to $key and $val. The internal pointer in `$array' is moved to the next element.

Often you'll see that in a loop:

while(list($key, $val) = each($array)):

It's basically the same thing as:

foreach($array as $key => $val): 
konforce
A: 

Using them together is, basically, an early way to iterate over associative arrays, especially if you didn't know the names of the array's keys.

Nowadays there's really no reason I know of not to just use foreach instead.

list() can be used separately from each() in order to assign an array's elements to more easily-readable variables.

Brock Batsell
What? Except list doesn't work for associative keys, only numeric keys.
Tchalvak
Right. It has to be used in conjunction with each(). each() uses the array's internal pointer, pulls the current array element, and creates an array that has the key indexed to 0 and the value indexed to 1.
Brock Batsell
+1  A: 

To answer the question in your first edit:

Basically, PHP is creating a hybrid array with the key/value pair from the current element in the source array.

So, you can get the key by using $bar[0] and the value by using $bar[1]. OR, you can get the key by using $bar['key'] and the value using $bar['value']. It's always a single key/value pair from the source array, it's just giving you two different avenues of accessing the actual key and actual value.

Brock Batsell
+1  A: 

Say you have a multi-dimensional array:

+---+------+-------+
|ID | Name | Job   |
| 1 | Al   | Cop   |
| 2 | Bob  | Cook  |
+---+------+-------+

You might do something like:

<?php
while(list($id,$name,$job) = each($array)) {
    echo "<a href=\"profile.php?id=".$id."\">".$name."</a> is a ".$job;
}
?>
aslum
+2  A: 

A very common example for list is when thinking about CSV files. Imagine you have a simple database stored as CSV with the columns id, title and text, such a file could look like this:

1|Foo|Lorem ipsum dolor|
2|Bar|sit amet|
...

Now when you parse this file you could do it like this, using the list function:

$lines = file( 'myFile.csv' );
for ( $i = 0; $i < count( $lines ); $i++ )
{
    list( $id, $title, $text, $null ) = explode( '|', $lines[$i], 4 );
    echo "Id: $id, Title: $title\n$text\n\n";
}

The other function, each, is basically just an old way to walk through arrays, using internal pointers. A more common way to do that is by using foreach now.

poke