Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

prepend row to two dimensional array 1

Status
Not open for further replies.

PeDa

Technical User
Oct 10, 2002
227
NL
I have a two dimensional array $data:
[r1c1][r1c2][r1c3]
[r2c1][r2c2][r2c3]
[r3c1][r3c2][r3c3]
[r4c1][r4c2][r4c3]
How can I prepend a row $heads?:
[r0c1][r0c2][r0c3]

P.S. The reason I am doing this is to submit the resultant array to SimpleXLSXGen
 
Hi

PHP:
[gray]// assuming this is how your data looks[/gray]
[navy]$data[/navy] [teal]= [[[/teal][i][green]'r1c1'[/green][/i][teal],[/teal] [i][green]'r1c2'[/green][/i][teal],[/teal] [i][green]'r1c3'[/green][/i][teal]], [[/teal][i][green]'r2c1'[/green][/i][teal],[/teal] [i][green]'r2c2'[/green][/i][teal],[/teal] [i][green]'r2c3'[/green][/i][teal]], [[/teal][i][green]'r3c1'[/green][/i][teal],[/teal] [i][green]'r3c2'[/green][/i][teal],[/teal] [i][green]'r3c3'[/green][/i][teal]], [[/teal][i][green]'r4c1'[/green][/i][teal],[/teal] [i][green]'r4c2'[/green][/i][teal],[/teal] [i][green]'r4c3'[/green][/i][teal]]];[/teal]
[navy]$heads[/navy] [teal]= [[/teal][i][green]'r0c1'[/green][/i][teal],[/teal] [i][green]'r0c2'[/green][/i][teal],[/teal] [i][green]'r0c3'[/green][/i][teal]];[/teal]

[gray]// this should do it[/gray]
[COLOR=orange]array_splice[/color][teal]([/teal][navy]$data[/navy][teal],[/teal] [purple]0[/purple][teal],[/teal] [b]null[/b][teal], [[/teal][navy]$heads[/navy][teal]]);[/teal]


Feherke.
feherke.github.io
 
Thank you; however I only see the $data array. Perhaps my description was misleiding, my actual script (version 8.1.5) is:

Code:
require_once('../includes/SimpleXLSXGen.php');

try {$db = new PDO("mysql:host=".HOST.";dbname=".DBNAME, USER, PASS);}
catch(PDOException $e) {exit("Geen verbinding met database");}
$koppen=array('Lidnr','Achternaam','Titel','vl.','tsv','Adres','Postcode','Woonplaats','Telefoon','Email','Rekening','t.n.v.','Incasso?','Geboortejaar','Lid vanaf','Lid tot','Nieuwsbrief?','Relatie','Tellid');
$result="(SELECT lidnr,achternaam,titel,voorletter,tussenvoegsel,adres,postcode,woonplaats,telefoon,email,reknr,reknaam,incasso,geboortejaar,datumin,IF(datumuit='0000-00-00','',datumuit),nieuwsbrief,relatie,tellid 
FROM ledenadmin ORDER BY relatie, achternaam, voorletter)";
$q=$db->prepare($result);
$klaar=$q->execute();
if (!$klaar){echo "Fout ".$q->errorCode()."; admin/ledenbestand.php; 1; gegevens niet ingelezen!<br>";die();}
$result = $q->fetchAll(PDO::FETCH_ASSOC);
$tabel=array_splice($result, 0, null, [$koppen]); 
$xlsx = Shuchkin\SimpleXLSXGen::fromArray( $tabel )->downloadAs('ledenbestand.xlsx');

If I replace the last line by var_dump($tabel) I only see the data from $result.
 
Hi

Oops. Sorry. My default PHP version is still 7.3. Your must be 8.something. The [tt]$length[/tt] parameter's meaning changed abit from 8.0. Use [purple]0[/purple] instead of null and $result will contain the unified array :
PHP:
[COLOR=orange]array_splice[/color][teal]([/teal][navy]$result[/navy][teal],[/teal] [purple]0[/purple][teal],[/teal] [purple]0[/purple][teal], [[/teal][navy]$koppen[/navy][teal]]);[/teal]


Feherke.
feherke.github.io
 
Hello Feherke,
Yes, that did the trick! Thank you very much.
-Peter D.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top