Home > database >  How to insert multidimensional array values into a single row in CSV file using PHP
How to insert multidimensional array values into a single row in CSV file using PHP

Time:04-22

I have this problem with my code converting my multidimensional array to csv.

This is the structure of my array

Array (
        [vbas31] => 
                Array ( [enabled] => Array ( [0] => NO ) 
                        [registered] => Array ( [0] => NO ) ) 
        [bnmsa1] => 
                Array ( [enabled] => Array ( [0] => YES ) 
                        [registered] => Array ( [0] => NO )
                        [compromised] => Array ( [0] => NO ) )
      )

and I want to save it to a csv file for reporting purposes, like this:

vbas31, enabled, no, registered, no
bnmsa1, enabled, yes, registered, no, compromised, no

What I did in my code is something like this:

$file = fopen('testfile.csv','w');
$keysarr = array_keys($jsonArr);

for($i = 0; $i < count($jsonArr); $i  ) {
    foreach($jsonArr[$keysarr[$i]] as $key => $value) {
        echo $key . " : " . $value[0] . "<br>";
    
    }
    $new_line = [$keysarr[$i], $key, $value[0]];


    fputcsv($file, $new_line);
}

fclose($file);

But the output is not what I want, and here's the output that is generated by my code:

vbas31, registered, no
bnmsa1, compromised, no

It only gets the last data from the array. Can I ask what's the problem with my code and what I'm doing wrong?

CodePudding user response:

I'm not a fan of nested loops, as well as provided answers but look at this:

Your array:

$yourArray = [
    'bnas31' => [
            'enabled' => [0 => 'NO'],
            'registered' => [0 => 'NO']
    ],
    'bnmsa1' => [
        'enabled' => [0 => 'YES'],
        'registered' => [0 => 'NO'],
        'compromised' => [0 => 'NO']
    ]
];

Code:

foreach($yourArray as $key1 => $value1) {
    $row = $key1;
    $line = [$key1];
    foreach ($value1 as $key2 => $value2) {
        $row .= ','.$key2.','.$value2[0];
        $line = array_merge($line, [$key2, $value2[0]]);
    }
    echo $row.'<br>';
}

You can use another foreach inside your other foreach. :) You can just add the key first to the new line, and then iterate on rest of the elements and add those as well. The $row variable is just for checking the result.

The code is so simple you should be able to analyze it by yourself.

The result of the above code is:

bnas31,enabled,NO,registered,NO
bnmsa1,enabled,YES,registered,NO,compromised,NO

Greetings.

CodePudding user response:

what's the problem with my code and what I'm doing wrong?

Your code is only saving the last element of each nested array because your fputcsv() is only happening for the last element of that nested array - it happens outside that last, inner loop. Move it inside, where your echo is, and it will work.

for($i = 0; $i < count($jsonArr); $i  ) {
    foreach($jsonArr[$keysarr[$i]] as $key => $value) {
        // Inside inner loop - this works
        // echo $keysarr[$i] . ": " . $key . ", " . $value[0] . "\n";
    }
    // Outside inner loop - only shows last element because we only
    // get here after the inner loop over the nested array has 
    // finished.  At that time the variables are set to whatever 
    // they were at the end of the last iteration - the last element.
    echo $keysarr[$i] . ": " . $key . ", " . $value[0] . "\n";
}

Having said that, your looping is a bit more complicated than it needs to be, here's a simpler version, and a working example:

$file = fopen('testfile.csv','w');

foreach ($jsonArr as $index => $data) {
    foreach ($data as $key => $array) {

        // Some debug output
        echo $index . ": " . $key . ", " . $array[0] . "\n";

        // And save your line
        $line = [$index, $key, $array[0]];
        fputcsv($file, $line);
    }
}

fclose($file);
  • Related