简体   繁体   English

PHP在csv中插入多维数组值

[英]PHP Insert multi-dimensional array values in csv

I have the following array. 我有以下数组。 For simplicity, i have only posted one item but it's a big array that contains a huge number of keys and values 为简单起见,我只发布了一项,但这是一个包含大量键和值的大型数组

Array
(
[Maths] => Array
    (
        [00] => 0
        [01] => 0
        [02] => 0
        [03] => 0
        [04] => 0
        [05] => 0
        [06] => 0
        [07] => 0
        [08] => 0
        [09] => 0
        [10] => 0
        [11] => 0
        [12] => 0
        [13] => 0
        [14] => 0
        [15] => 0
        [16] => 0
        [17] => 9
        [18] => 5128
        [19] => 5763
        [20] => 1734
        [21] => 632
        [22] => 299
        [23] => 190
    )

I would like to put the contents of the array into a csv file which will be structured like this. 我想将数组的内容放入一个csv文件中,该文件的结构如下。 One line per outer array and conctenate the values of the inner array to it. 每个外部数组一行,并将内部数组的值压缩到该行。

So the above array will appear like this. 因此,上面的数组将像这样显示。 Please bear in mind that it's not a static array. 请记住,它不是静态数组。 All the information in the array is dynamic 数组中的所有信息都是动态的

Math,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,5128,5763,1734,632,299,190

Please help 请帮忙

$data=Array(...);
$csv="";
foreach ($data as $key => $value) {
    $csv.=$key.",".implode(",", $value)."\n";
}

if your structure is something like: 如果您的结构是这样的:

array ( key1 => array(), key2 => array() ... ) you could do this: array(key1 => array(),key2 => array()...),您可以执行以下操作:

$csvOutput = "";

foreach ($baseAy as $key => $subAy) {

    $csvOutput.= "\n".$key.",";

    foreach ($subAy as $value) {
         $csvOutput.= $value.",";
    }
}

print_r($csvOutput);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM