Converting Strings to Arrays


 $a = explode(',', $csvdata); 


Sometimes, arrays are not used to store information; instead, a string is used. The single values are all within the string, but are separated by a special character. One example for this is the comma separated values (CSV) format.

Turning a String into an Array (explode.php)
 <?php   $csvdata = 'Sams Publishing,800 East 96th Street,Indianapolis,Indiana,46240';   $a = explode(',', $csvdata);   $info = print_r($a, true);   echo "<pre>$info</pre>"; ?> 

The PHP function explode() creates an array out of these values; you just have to provide the character(s) at which the string needs to be split. The browser then shows this output:

 Array (     [0] => Sams Publishing     [1] => 800 East 96th Street     [2] => Indianapolis     [3] => Indiana     [4] => 46240 ) 




PHP Phrasebook
PHP Phrasebook
ISBN: 0672328178
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net