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
.