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
2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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";
}

6 Comments

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
Do a var_dump on your Array and post that in your question.
Do not use this echo ",..." construct. implode(',', $vals) is a much better way to achieve this.
Your Array does not match the described data structure.
@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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.