0

I have an array which is structured like this

  [cars] => Array
    (
        [0] => 43063
        [1] => 17306
        [2] => 116078
    )

  [houses] => Array
    (
        [0] => 13300
        [1] => 32243
    )

[garage] => Array
    (
        [0] => 13094
        [1] => 30649
    )

 [hotels] => Array
    (
        [0] => 10025
        [1] => 59468
        [2] => 29734
    )

I would like to create a csv file out of that array. My csv file should be structured this way cars,43063,17306,116078 houses,13300,32243,1000 garage,13094,30649,1000 hotels,10025,59468,29734

I have tried several codes, but i am not getting what i want, how can i acheive it in php ?

Here is the var dump of my array

array(4) {
["23"]=>
array(1) {
["cars"]=>
int(43063)
}
[23]=>
array(4) {
["cars"]=>
int(17306)
["houses"]=>
int(13300)
["garage"]=>
int(13094)
["hotels"]=>
int(10025)
}
[22]=>
array(4) {
["cars"]=>
int(116078)
["houses"]=>
int(32243)
["garage"]=>
int(30649)
["hotels"]=>
int(59468)
}
[21]=>
array(1) {
["hotels"]=>
int(29734)
}
}

I would like to have a csv that contains 4 lines, one line for each key (hotels,car,garage,etc..

cars,43063,17306,116078
houses,13300,32243,1000
garage,13094,30649,1000
hotels,10025,59468,29734
user3172841
  • 75
  • 1
  • 7
  • 1
    What did you try and what results did you get? – Seb Jan 09 '14 at 09:18
  • 1
    look here http://stackoverflow.com/questions/3933668/convert-array-into-csv, http://stackoverflow.com/questions/13108157/php-array-to-csv – sergio Jan 09 '14 at 09:18

2 Answers2

1

You could try something like this

function arrayToValues(array $data)
{
    $output = array();

    foreach ($data as $key => $item) {
        if (is_array($item)) {
            $output = array_merge($output, array($key), arrayToValues($item));
        } else {
            $output[] = $item;
        }
    }

    return $output;
}

$csvData = implode(',', arrayToValues($yourArrayData));

If you want to write the output you could use fputcsv in combination with the function instead.

Bearwulf
  • 353
  • 1
  • 8
  • This is the only function for converting a multidimensional array to CSV output that I have found that actually works on my array. However, it doesn't put a new line at the end of each item. Still trying to figure that out. – Yvonne Aburrow Jan 22 '16 at 10:24
0

Quick and dirty:

foreach ($arr as $key => $vals) {
    echo $key . implode(",", $vals) . "\n";
}

UPDATE:

$transformed = array();
foreach ($arr as $item) {
    foreach ($item as $key => $val) {
        $transformed[$key][] = $val;
    }
}
foreach ($transformed as $key => $vals) {
    echo $key . implode(",", $vals) . "\n";
}
jgroenen
  • 1,332
  • 1
  • 8
  • 13
  • i have tried your code but i am not getting the desired results here is what i get 43063 17306 116078 13300 32243 13094 30649 10025 59468 29734 – user3172841 Jan 09 '14 at 09:33
  • Do a var_dump on your Array and post that in your question. – jgroenen Jan 09 '14 at 09:39
  • 1
    Do not use this echo ",..." construct. implode(',', $vals) is a much better way to achieve this. – Seb Jan 09 '14 at 09:52
  • Your Array does not match the described data structure. – jgroenen Jan 09 '14 at 09:54
  • @jgroenen, i am sorry but i didn't add the downvote, it was added automatically by the site and i am not able to change it. Your code helped me solve the problem i was facing. Thanks very much – user3172841 Jan 09 '14 at 10:14
  • @jgroenen, i need 15 reputation in order to "voteUp". I will do it as soon as i have the required reputation – user3172841 Jan 09 '14 at 10:16