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
)
|