TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   array elements into variables and values (http://www.talkphp.com/absolute-beginners/2971-array-elements-into-variables-values.html)

Dave 06-17-2008 03:56 AM

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

sketchMedia 06-17-2008 10:35 AM

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.

Dave 06-17-2008 11:24 AM

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

sketchMedia 06-17-2008 11:31 AM

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.

Dave 06-17-2008 12:56 PM

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

sketchMedia 06-17-2008 01:10 PM

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.

Evulness 06-20-2008 01:37 PM

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.

sketchMedia 06-20-2008 01:56 PM

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