TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-17-2008, 03:56 AM   #1 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default 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
Dave is offline  
Reply With Quote
Old 06-17-2008, 10:35 AM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

PHP Code:
$arr = array(
    
'green',
    
'blue',
    
'white',
    
'sky',
    
'cloud'
);

${
$arr[3]} = $arr[1];
echo 
$sky
PHP: Variable variables - Manual

probably a better way of doing it, but i havnt put much time into it.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Dave (06-17-2008)
Old 06-17-2008, 11:24 AM   #3 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default 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!

Dave
Dave is offline  
Reply With Quote
Old 06-17-2008, 11:31 AM   #4 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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($arrayEXTR_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.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 06-17-2008 at 01:11 PM. Reason: punctuation
sketchMedia is offline  
Reply With Quote
The Following 3 Users Say Thank You to sketchMedia For This Useful Post:
Dave (06-17-2008), Evulness (06-20-2008), Matt (06-18-2008)
Old 06-17-2008, 12:56 PM   #5 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

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
Dave is offline  
Reply With Quote
Old 06-17-2008, 01:10 PM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
seems written for folks who already know PHP
I agree, its sometimes very unclear about things especially if your not a PHP professional.

Glad you found them useful.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 06-20-2008, 01:37 PM   #7 (permalink)
The Contributor
 
Evulness's Avatar
 
Join Date: Apr 2008
Location: Tampa, FL
Posts: 65
Thanks: 6
Evulness is on a distinguished road
Default

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.
__________________
"Knowledge is power. Abuse it."~Evulness
My portfolio: www.evularts.com
Send a message via AIM to Evulness
Evulness is offline  
Reply With Quote
Old 06-20-2008, 01:56 PM   #8 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Not a problem m8
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:04 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design