Change Column Headers in Array with Unset: PHP -
i'm trying change text gets written first row, column header, of csv. i'm trying use unset
, it's not working. referencing wrong array? appreciated. code below:
for ($x=0; $x<$count; $x++) { $currentrecord = $response['data'][$x]; $jsondatainarray[] = array ( "b98336b40c8420dc2f1401d6451b1b47198eee6d" => $response['data'][$x]['b98336b40c8420dc2f1401d6451b1b47198eee6d'], "17a14a9da9815451ff5ffc669d407e8b0376b06b" => $response['data'][$x]['17a14a9da9815451ff5ffc669d407e8b0376b06b'], "3eedd4fd08f44a72d911dc4934a6916f3b31911b" => $response['data'][$x]['3eedd4fd08f44a72d911dc4934a6916f3b31911b'], "52ede287f6c55eb6b12821ca24f74098779abdce" => $response['data'][$x]['52ede287f6c55eb6b12821ca24f74098779abdce'], "13916ba291ab595f27aefbff8b6c43a3fb467b72" => $response['data'][$x]['13916ba291ab595f27aefbff8b6c43a3fb467b72'] ) ) //change key names. $jsondatainarray["col1"] = $jsondatainarray["b98336b40c8420dc2f1401d6451b1b47198eee6d"]; unset($jsondatainarray["b98336b40c8420dc2f1401d6451b1b47198eee6d"]); //first row of data $test_array = $jsondatainarray[0]; //open file write towards if($startpos == 0){ $fp = fopen('output.csv', 'w'); fputcsv($fp, array_keys($response['data'][0])); }else{ $fp = fopen('output.csv', 'a'); } //write data foreach ($jsondatainarray $fields) { fputcsv($fp, $fields); }
my current output is:
b98336b40c8420dc2f1401d6451b1b47198eee6d|17a14a9da9815451ff5ffc669d407e8b0376b06b|3eedd4fd08f44a72d911dc4934a6916f3b31911b|52ede287f6c55eb6b12821ca24f74098779abdce 4616|||stuff
my desired output be
col1|17a14a9da9815451ff5ffc669d407e8b0376b06b|3eedd4fd08f44a72d911dc4934a6916f3b31911b|52ede287f6c55eb6b12821ca24f74098779abdce 4616|||stuff
your $jsondatainarray
has numerical indexes, each of contains array.
replace
$jsondatainarray["col1"] = $jsondatainarray["b98336b40c8420dc2f1401d6451b1b47198eee6d"]; unset($jsondatainarray["b98336b40c8420dc2f1401d6451b1b47198eee6d"]);
with
$jsondatainarray[0]["col1"] = $jsondatainarray[0]["b98336b40c8420dc2f1401d6451b1b47198eee6d"]; unset($jsondatainarray[0]["b98336b40c8420dc2f1401d6451b1b47198eee6d"]);
to have work in test, you'll want replace loop have change each entry.
Comments
Post a Comment