Home > Articles > Data > MySQL

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Converting Strings to Arrays

$a = explode(',', $csvdata);
<?php
  $csvdata = 'Pearson Education,800 East 96th Street,Indianapolis,Indiana,46240';
  $a = explode(',', $csvdata);
  $info = print_r($a, true);
  echo "<pre>$info</pre>";
?>

Turning a String into an Array (explode.php)

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.

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

Array
(
    [0] => Pearson Education
    [1] => 800 East 96th Street
    [2] => Indianapolis
    [3] => Indiana
    [4] => 46240
)
  • + Share This
  • 🔖 Save To Your Account