0

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.

7
  • Do you know what that array_values() in print_r() does? Commented Aug 28, 2016 at 11:50
  • which is line 21? Commented Aug 28, 2016 at 11:50
  • Sorry, line 21 is the second echo "echo $talentline[2][0];" Commented Aug 28, 2016 at 11:54
  • edit your question to state that. Also state what is the value of $_GET['talente']? Commented Aug 28, 2016 at 11:57
  • Your current in_array searches $i (which is index as you use USE_KEY) in a $talente (which is a string I suppose). So how do you think - what will happen when you will try to search 0 in a string? Commented Aug 28, 2016 at 11:59

1 Answer 1

1

Array values start counting at 0

So zero is first, and 1 is second.

Your issue of "undefined Offset" is caused because you are only "keeping" the array values that are found in the $_GET['talente'] input array. If you change this value to 2 then you will get undefined offset notices for 1 and 3, etc.

Your First issue is that arrays start at [0] so if you have two values in the new talentline array then the first is key [0] and the second value is key [1]. etc.

Your second issue is related in that because you're comparing the key values in the anonymous function, you are looking for the first value (array key [0]) by asking for value number 1 as given by the $_GET array. This is why it's giving you the uexpected (but correct) results.

Solutions:

For the second issue (which is a coding bug):

You need to adjust the key value by either increasing the key values by +1 or to decrease the input reference values by -1, to make the input align with the given CSV values:

return in_array($i, $talente--); //reduces the input comparison value by 1, 
         //so that the first value ("1") is treated as key "0".

Second, To fix the notice you get from referencing an unset array reference later in the script:

print isset($talentline[2][0]) ? $talentline[2][0]; : ""; 
// shorthand PHP stating that if value is set then print it, 
// else don't print anything.

Code I used to reach this answer (PHP 5.6.2). Obviously, adjusted as I don't have your CSV file to import, etc.

<?php
$talente = array(0=>1,1=>3);

$csv[] = array(0=>"Schild", 1=>1, 2=>"Licht", 3=>"1w10", 4=>"-");
$csv[] = array(0=>"Schutz", 1=>1, 2=>"Licht", 3=>"1w10", 4=>"-");
$csv[] = array(0=>"Licht", 1=>4, 2=>"Licht", 3=>"1w10", 4=>"-");
$csv[] = array(0=>"Genesung",1=>1,2=>"Licht",3=>"-",4=>"-");
$csv[] = array(0=>"Aufopfern",1=>1,2=>"Licht",3=>"-",4=>"-");

        $talentline = array_filter($csv, function($i) use ($talente) {
            return in_array($i, $talente--);
        }, ARRAY_FILTER_USE_KEY);
        $talentline = array_filter($talentline);

print_r(array_values($talentline));

print isset($talentline[1][0]) ? $talentline[1][0]; : ""; 
print isset($talentline[2][0]) ? $talentline[2][0]; : ""; 
print isset($talentline[3][0]) ? $talentline[3][0]; : ""; 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you a lot for your help and your explanations! But I think I couldn't explain what I try to do here. If the user would set ID 4 and 5 ($talente = array(0=>4,1=>5);) I'd get nothing printed since Genesung and Aufopfern were ignored. I need $talentline[1][0] filled with the first fitting line of the CSV. I hope it's understandable what I mean.
Okay I got it. I noticed that "array_values" is doing exactly that and yeah, just had to give it to $talentline. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.