| Home > Computer & I'net > Exploding and Imploding... |
|
|
|||||||||||||||
|
|
|||||||||||||||
|
||||||||||||||||
Exploding and Imploding | |
Using the explode command will create an array from a string. Here is a sample string to start things off : <?php
$pantry = "tomatoes,oranges,bananas,potatoes,bread,apples"; ?> Now you have to figure out a common seperator element. In this case it is a comma seperating each word. In a normal sentence, it could be specifed as a space. <?php The array called $pantry_food now contains 6 elements. The array keys range from 0 to 5 and contains the values of tomatoes to apples. Doing a quick array print out : <?php $count_total = count($pantry_food); for ($counter=0; $counter<$count_total; $counter++){ $line = each ($pantry_food); echo "$line[key] $line[value] <br />"; ?> results as : 0 tomatoes
1 oranges 2 bananas 3 potatoes 4 bread 5 apples So exploding breaks the data apart, imploding brings the data together. The implode command will take the data from an array and assemble it into a single string. <?php Imploding uses a separator element as well. In this case, it adds the separator between each of the array elements when it creates the new string. The above example is using a space this time. <?php Has the value of : tomatoes oranges bananas potatoes bread apples
----------------- Article by David Stanley. Visit his site http://www.htmlite.com. Reprinted with permission. | |
| Articles |
•Auto & Trucks•Business•Computer & I'net·General·Apache·CSS·Database·Hardware·HTML·Javascript&DHTML·Linux·MySQL·Operating System·Perl / CGIPHP·Programming·Publishing·Search Engines·Software Problems·SSI·Tips & Tricks·Utilities·Web Design•Family•Food & Drink•Gardening•Health•Other•Pets•Psychology•Spiritual•Travel•Women |
| Calculators |
|