1

I've taken a look at this question, but I can't seem to get the code to work for my purposes.

I have an array ($array) structured like so, full of data from a database:

array(369) {
    array(3) {
        ["id"] => string(5) "12345",
        ["title"] => string(11) "Hello World",
        ["description"] => string(n) "..."
    }
    array(3) {
        ["id"] => string(5) "12346",
        ["title"] => string(13) "Goodbye World",
        ["description"] => string(n) "..."
    }
    ...
}

However, this array data will be creating a CSV, and I need to insert empty columns as well. Therefore I need the array to end up looking like this:

array(369) {
    array(5) {
        ["id"] => string(5) "12345",
        ["title"] => string(11) "Hello World",
        ["title2"] => string(0) "",
        ["description"] => string(n) "...",
        ["description2"] => string(0) ""
    }
    array(5) {
        ["id"] => string(5) "12346",
        ["title"] => string(13) "Goodbye World",
        ["title2"] => string(0) "",
        ["description"] => string(n) "...",
        ["description2"] => string(0) ""
    }
    ...
}

I've tried using array_splice() to enter blank values at the relevant points:

array_splice($array, 2, 0, "");
array_splice($array, 4, 0, "");

But this ends up just breaking the array, and later code utlising fputcsv() doesn't even recognise it as an array. How can I enter these blank values?

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

Please note: What the array key is labelled as does not matter. It can be as suggested above, blank, numeric, zero... all that's important is that I need a blank value.

Community
  • 1
  • 1
mpdc
  • 3,440
  • 4
  • 20
  • 44
  • 1
    Have you tried a simple loop over each array item, appending the blank elements that way? Alternatively, you could have these columns included with the MySQL select query, even if they don't exist in the original table. Something like `SELECT *, "" as description1 FROM table_name WHERE condition_stuff` – Jake Bathman Sep 11 '15 at 13:59
  • 1
    A neat trick, pulling non-existent columns via a query! – mpdc Sep 11 '15 at 14:41

1 Answers1

3
$array = array_map(function (array $row) {
    static $default = [
        'id' => null,
        'title' => null,
        'title2' => null,
        'description' => null,
        'description2' => null
    ];
    return array_merge($default, $row);
}, $array);

array_merge keeps the structure (keys and order) of the first array, but replaces any values whose keys match with the second array.

deceze
  • 471,072
  • 76
  • 664
  • 811
  • 3
    5.3+ for the anonymous callback, 5.4+ for the short array syntax `[]`. In other words: any non-ancient PHP version. – deceze Sep 11 '15 at 14:04
  • @SeanBright No, they should update their PHP version! – Rizier123 Sep 11 '15 at 14:08
  • For anyone still needing to write `array()`: support for the last version which didn't support `[]` ran out a year ago, support for the version that introduced `[]` is ending just now... ;P – deceze Sep 11 '15 at 14:09
  • Thankfully you don't have to upgrade if you aren't able to, you can just change the `[...]` notation to `array(...)` and you are all set. – Sean Bright Sep 11 '15 at 14:15
  • Yes, excellent idea. Worked like a charm. – mpdc Sep 11 '15 at 14:40