Convert Array to CSV String

Data visualization is an important tool for web applications to take advantage of. The right chart, graph or scatter plot can show trends, looming disasters, & more. We often use Javascript libraries, such as HighCharts, to display data to our users. The problem is, we need to extract our data from the database, generally as an array or object, and HighCharts needs a CSV (coma seperated value) string. So we created this function to quickly convert arrays (including multi-dimensional) into CSVs. This is specifically designed to be used with CodeIgniter Active Record results arrays, but it could be any array with matching keys.
// Expected input
$myArray = array(
  0 => array(
    'People' => 6,
    'Places' => 2,
    'Things' => 92.42
  ),
  1 => array(
    'People' => 4,
    'Places' => 'milk',
    'Things' => 10003
  ),
  2 => array(
    'People' => 3,
    'Places' => 55,
    'Things' => 32
  )
);


$csv = array2csv($myArray);

/* CSV output 
array (
  'People' => '6,4,3',
  'Places' => '2,milk,55',
  'Things' => '92.42,10003,32'
);
*/
The function
function array2csv($array) {
    $csv = array();

    if (is_array($array))
    {
      foreach ($array as $k => $v)
      {
        if (is_object($v))
          $v = (array) $v;

        if (is_array($v))
        {
          foreach ($v as $subk => $subv)
          {
            $csv[$subk] .= $subv .',';
          }
        }
        else
        {
          $csv[$k] .= $v .',';
        }
      }

      foreach ($csv as &$string)
      {
        $string = substr($string, 0, -1);
      }
    }
    return $csv;
  }
An example using it with CodeIgniter Active Record.
/*
 * 'dogModel' model
 */
class dogModel extends CI_Model {
  function get_my_data() {
    return $this->db
      ->select('people, places, things')
      ->get('myTable')
      ->result();
  }
  function array2csv($array) {
    $csv = array();

    if (is_array($array))
    {
      foreach ($array as $k => $v)
      {
        if (is_object($v))
          $v = (array) $v;

        if (is_array($v))
        {
          foreach ($v as $subk => $subv)
          {
            $csv[$subk] .= $subv .',';
          }
        }
        else
        {
          $csv[$k] .= $v .',';
        }
      }

      foreach ($csv as &$string)
      {
        $string = substr($string, 0, -1);
      }
    }
    return $csv;
  }
}


/*
 * 'dogController' controller
 */
class dogController extends CI_Controller {
  function index() {
    $this->load->model('dogModel');

    $sql_result = $this->dogModel->get_my_data();

    $view_data = array();
    $view_data['csv'] = $this->dogModel->array2csv($sql_result);

    $this->load->view('dogView', $view_data);
    unset($view_data);
  }
}

/*
 * 'dogView' view
 */

echo "My places string is ". $csv['places'] ."
"; echo "My people string is ". $csv['people'] ."
"; echo "My things string is ". $csv['string'] ."
";
Happy coding.
Small dog web development studio
View All Our Posts

Web application developers in western Washington, creating user centric web solutions for businesses of all shapes and sizes.

Our projects include ecommerce, event organization, user driven web applications and more.


Small Dog Studios, LLC. Small Dog Studios News