![]() |
array elements into variables and values
If I have an array containing several values (e.g., "green","blue", "white", "sky", "cloud"), how could I generate this from the array:
$sky = "blue" IOW, how to get "sky" from the array, make it into a variable, and assign to it the value of another array value, in this case "blue"? I've worked hard on this one, but I just couldn't make it happen. I looked at LIST, IMPLODE, EXPLODE, EXTRACT, etc., but couldn't figure it out. Thanks for any help! Dave |
PHP Code:
probably a better way of doing it, but i havnt put much time into it. |
What? No way....
sketchMedia --
Now wait a minute! That was way too simple!! OK, OK. I'll just chaulk it up to my monster brain which (evidently) is focused on solving the world's great problems...:-/... Thanks so much for your help! 8-) Dave |
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:
$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:
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:
This will result in these results: Code:
array(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:
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:
PHP Code:
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. |
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. Dave |
Quote:
Glad you found them useful. |
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. Thanks bro. |
Not a problem m8
|
| All times are GMT. The time now is 09:15 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0