Insert inner arrays in JSON-encoded array to csv-file with php


I have values from form that stored in DB withjson_encode() and I need to insert then these values into.csv file. Here's my code:

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
$sql = $con->prepare("SELECT message FROM online_survey");
$array = [];
foreach ($sql->get_result() as $row){
    $array[] = json_decode($row['message'], true);
if (!$sql) {

$fp = fopen('/path/to/file.csv', 'w');

foreach($array as $key) {
    fputcsv($fp, $key);


And in this file I am getting all values from this form except Arrays. For example:

Cell 1 Cell2 Cell3 Cell4 Cell5 Cell6 Cell7 OrgName Country City Position Scope Array

What I need is to get this:

[organization_name] => orgName
[organisation_country] => Country
[organisation_city] => City
[organization_position] => position
[other_1_4_text] => 
[organisation_scope] => Local
[activity_areas] => Public finance management, Democratic participation and civil society, Urban development and management, Anti-corruption organisations and institutions, Children and youth

Instead of this:

[organization_name] => orgName
[organisation_country] => Country
[organisation_city] => City
[organization_position] => position
[other_1_4_text] => 
[organisation_scope] => Local
[activity_areas] => Array
        [0] => Public finance management
        [1] => Democratic participation and civil society
        [2] => Urban development and management
        [3] => Anti-corruption organisations and institutions
        [4] => Children and youth

If you need some more information please let me know!





You just need to implode the arrays:

Code: (Demo)

        'Public finance management',
        'Democratic participation and civil society',
        'Urban development and management',
        'Anti-corruption organisations and institutions',
        'Children and youth'

foreach($array as $k=>$v){
        $v=implode(', ',$v);


array (
  'organization_name' => 'orgName',
  'organisation_country' => 'Country',
  'organisation_city' => 'City',
  'organization_position' => 'position',
  'other_1_4_text' => '',
  'organisation_scope' => 'Local',
  'activity_areas' => 'Public finance management,Democratic participation and civil society,Urban development and management,Anti-corruption organisations and institutions,Children and youth',

Edit: It would be better to make this modification when you are looping through the db resultset...

$array[]=is_array($v)?implode(', ',$v):$v;

People are also looking for solutions to the problem: php - Updating to mysqli and cant get database to read


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.