php - How do I put this array into csv or excel? -
i can't figure out way , bad working arrays hope can me.
i have array $stuck contains names,
$stuck = array('daniel','alex','alfredo','dina');
i using sort put names in alphabetical order, this
sort($stuck);
now thing want put array csv or excel file first letter of name title , names letter under title.
this example of trying accomplish
here try this. added comments between code.
$csvarray = array(); $namearray = array('daniel','alex','alfredo','dina'); //create array can used creating csv foreach($namearray $name) { $firstletter = strtoupper(substr($name, 0,1)); $csvarray[$firstletter][] = $name; } //determine subarray have must rules $maxrulecount = 0; foreach($csvarray $firstletter => $namearray) { if(count($namearray) > $maxrulecount) { $maxrulecount = count($namearray); } } //create first rule (letters) $csvcontent = implode(';', array_keys($csvarray)); $csvcontent .= "\r\n"; //loop through csv array , create rules of names for($i = 0 ; $i < $maxrulecount ; $i++) { foreach($csvarray $firstletter => $namearray) { $csvcontent .= @$csvarray[$firstletter][$i].';'; } $csvcontent .= "\r\n"; } //the hole csv content in $csvcontent variable //if want print need add text/plain header because of \r\n new line characters header("content-type:text/plain"); echo $csvcontent;
ouput is:
d;a daniel;alex; dina;alfredo;
Comments
Post a Comment