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

555

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:

$con=mysqli_connect("localhost","db_user","db_pass","table_name");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset($con,"utf8");
$sql = $con->prepare("SELECT message FROM online_survey");
$sql->execute();
$array = [];
foreach ($sql->get_result() as $row){
    $array[] = json_decode($row['message'], true);
}
if (!$sql) {
    die(mysqli_error($con));
}

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

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

fclose($fp);

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:

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
)

Instead of this:

Array
(
[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!

Thanks!

606

Answer

Solution:

You just need to implode the arrays:

Code: (Demo)

$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'
    ]
];

foreach($array as $k=>$v){
    if(is_array($v)){
        $v=implode(', ',$v);
    }
    $result[$k]=$v;
}
var_export($result);

Output:

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

$v=json_decode($row['message'],true);
$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

Source

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.