问题描述
有了另一个用户的很多帮助,我来到了点,我得到我想要的二维数组。我得到的每个ID($ talente)从CSV保存一行。像数组的charm一样工作。
这是我使用的CSV:
c。Schild,1,Licht,1w10, -Schutz,1,Licht,1w10, -
Licht,4,Licht,1w10, -
Genesung,1,Licht,
Aufopfern,1,Licht, - , -
脚本:
<?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));给我以下输出id的1& 3.
[0] => Array(
[0] => Schutz
[1] => 1
[2] => Licht
[3] => 1w10
[4] => -
)
[1] => Array(
[0] => Genesung
[1] => 1
[2] => Licht
[3] => -
[4] => -
)
:
有两个问题,我无法解决。第一个是,打印的线条是我期望的一行之后。因此,我希望Schild,而不是Schutz。
更大的问题是,脚本将行保存在array-position等于ID。这不是我需要的,因为它也保存空数组元素。我希望的结果是当发送ID 1和3时,数组[0]处的Schild和数组[1]处的Licht。
我希望我能够解释得很好。
数组值开始计数 0
所以零是第一个,1是秒。
您的未定义偏移问题是由于您只是保留在 $ _ GET ['talente ']
输入数组。如果您将此值更改为 2
,那么您将会得到1和3等的未定义偏移通知。
第一个问题是数组从 [0]
开始,所以如果在 talentline
数组中有两个值,是键 [0]
,第二个值是键 [1]
。等等。
您的第二个问题与此相关,因为您要比较匿名函数中的键值,您正在查找第一个值(数组键<$ c $ $ _ GET 给出的值
1
c>数组。
解决方案:
对于第二个问题(这是一个编码错误):
您需要通过将键值增加+1或将输入参考值减少-1来调整键值,以使输入与给定的CSV值一致:
return in_array($ i,$ talente--); //将输入比较值减小1,
//,以便将第一个值(1)视为键0。
其次,
要解决通过引用未设置的数组引用脚本:
打印isset($ talentline [2] [0])? $ talentline [2] [0]; :;
//简写PHP说明如果值设置然后打印它,
//否则不打印任何东西。
代码我用来达到这个答案.2)。显然,由于我没有要导入的CSV文件等而进行调整。
<?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]; :;
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:
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.
解决方案 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]; : "";
这篇关于PHP二维数组与空项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!