This example will give us three variables: $name, $age and $location.
$name is assigned the first element within the array ($array) which happens to be my name, next $age is assigned the second value within the array ($array) 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:
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:
$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:
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:
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:
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:
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.
Wow! Hey, sketchMedia, many thanks for the clear explanations.
As I mentioned, I had read the PHP manual on these functions, but my criticism of the manual is that it SOMETIMES seems written for folks who already know PHP, but just need to be reminded of how this or that works.
The user notes are sometimes helpful, but most of the time are written as professional-to-professional. As I learn more, the PHP manual becomes incrementally more helpful.
That being said, your summaries are VERY clear and I've added them to my reference file in order to refer back to many times, I'm sure, in the future.
Woah sketch.... you sound like me... explaining things in detail.
You should submit that post as an article. I'm sure there are plenty of people looking for that simple of an explanation.
Though i wasn't looking for that right now.... you just helped solved an issue i was going to fix today. Though you didn't cover exactly what i was going to look up. the way you just explained implode and explode, just gave me an idea for my CMS.
"Knowledge is power. Abuse it."~Evulness
My portfolio: www.evularts.com