A quick explanation of the above methods you listed:
list
Using the list language construct, you can assign a list of variables with the values from an array in one statement, consider this example:
PHP Code:
$array = array(
'Sam',
'21',
'Manchester'
);
list($name, $age, $location) = $array;
This example will give us three variables:
$name,
$age and
$location.
$name is assigned the first element within the array ($array[0]) which happens to be my name, next $age is assigned the second value within the array ($array[1]) and the process continues to the next variable and so on.
this is useful because it allows us to assign a series of variables from an array in one statement, thus is an alternative to this:
PHP Code:
$array = array(
'Sam',
'21',
'Manchester'
);
$name = $array[0];
$age = $array[1];
$location = $array[2];
ugly huh?
Explode
This function within php's arsenal will allow you to take a string and explode on a given specifier into an array of parts, take this example:
PHP Code:
$string = 'this is a string i want to convert into an array';
$array = explode(' ', $string);
Ok, here we have a string that we want made into an array of words, to do this we use explode to drop a bomb on the string and take the parts and put them into an array. Unlike conventional explosives; explode allows us to accuratly 'pin-point' a place to concentrate the explosion in many places from the same explosion, in this case the delimiter is a blank space.
This will result in these results:
Code:
array(
0 => string 'this' (length=4)
1 => string 'is' (length=2)
2 => string 'a' (length=1)
3 => string 'string' (length=6)
4 => string 'i' (length=1)
5 => string 'want' (length=4)
6 => string 'to' (length=2)
7 => string 'convert' (length=7)
8 => string 'into' (length=4)
9 => string 'an' (length=2)
10 => string 'array' (length=5)
)
Simple really and very useful.
Implode
Implode is the direct opposite of explode, with this function we can make a string out of an array and like explode we can specify where we want it connected. The php manual refers to this as 'glue' and i think that is a good analogy of the process, basically we take each element within the array and use the glue to glue them together, consider this example:
PHP Code:
$array = array(
'this',
'is',
'an',
'array' ,
'i' ,
'want' ,
'to' ,
'convert' ,
'into' ,
'a',
'string'
);
echo implode(' ', $array);
This will result in:
this is an array i want to convert into a string, as you can see each element has been glued together with a blank space.
Extract
Extract extracts the values from an associative array and creates variables from them, using the key from the array as the variable name and the value from the array as the new variable value, consider this example:
PHP Code:
$array = array(
'name' => 'Sam',
'age' => '21',
'location' => 'Manchester'
);
extract($array);
echo $name, ' ',$age, ' ',$location;
As you can see, here extract extracts three variables from the array
$name,
$age and
$location, this function however allows us to check for conflicts, in other words if we already have a variable $name set and we run an extract that will (like the above example) extract a variable called $name then we can tell php what we want it to do by using the extract type parameter:
PHP Code:
$name = 'Ben';
$array = array(
'name' => 'Sam',
'age' => '21',
'location' => 'Manchester'
);
extract($array, EXTR_SKIP);
echo $name, ' ',$age, ' ',$location;
This will output: Ben 21 Manchester, because we gave the function the 'EXTR_SKIP' flag therefore it will skip any collisions if finds and will leave the existing variable alone, as you can see from the example instead of overwriting
$name from 'Ben' as it was initially assigned, to 'Sam' via the extract method, it has left it alone and therefore
$name still contains the string 'Ben'.
There are many different flags for this function, I suggest you read the manual for a better explanation of each.
PHP: extract - Manual
PHP: list - Manual
PHP: implode - Manual
PHP: explode - Manual
Dunno if you needed them explaining but it should show you how these methods wouldnt do what you needed them to do. Anyway it should serve someone else a purpose hopefully.