With a lot of help of another user I came to the point that I get the two-dimensional array I desired. Every ID i get ($talente) saves a line from the CSV. Works like a charm for the array. But the output confuses me.
This is the CSV I use:
Schild,1,Licht,1w10,-
Schutz,1,Licht,1w10,-
Licht,4,Licht,1w10,-
Genesung,1,Licht,-,-
Aufopfern,1,Licht,-,-
The script:
<?php
$talente = $_GET['talente'];
$myFile = fopen("talente.csv", "r");
$csv = [];
while ($data = fgetcsv($myFile, 1000, ",")) {
$csv[] = $data;
}
fclose($myFile);
$talentline = array_filter($csv, function($i) use ($talente) {
return in_array($i, $talente);
}, ARRAY_FILTER_USE_KEY);
print_r(array_values($talentline));
echo $talentline[1][0];
echo $talentline[2][0]; //line 21.
echo $talentline[3][0];
?>
print_r(array_values($talentline)); gives me the following output for the id's 1 & 3.
[0] => Array (
[0] => Schutz
[1] => 1
[2] => Licht
[3] => 1w10
[4] => -
)
[1] => Array (
[0] => Genesung
[1] => 1
[2] => Licht
[3] => -
[4] => -
)
The three echos at the end give me this:
Schutz Notice: Undefined offset: 2 in C:\xampp\htdocs\DvC Generator\php\test.php on line 21 Genesung
There are two issues I can't work out. The first one is, that the printed lines are one line after the one I'd expect. So instead of "Schutz" I'd expect "Schild".
The bigger issue I have is, that the script saves the line at the array-position equal to the ID. That's not what I need, because it saves empty array elements as well. My desired outcome would be Schild at array[0] and Licht at array[1] when the IDs 1 and 3 were send.
I hope I could explain it well enough.
$_GET['talente']?in_arraysearches$i(which is index as you useUSE_KEY) in a$talente(which is a string I suppose). So how do you think - what will happen when you will try to search0in astring?