【发布时间】:2020-11-03 23:00:31
【问题描述】:
我正在读取包含以下文本的文件:
Vélez Sarsfield|Zárate, Mauro|8|0|0|1|9
Estudiantes|Carrillo, Guido|5|1|0|2|8
Boca Juniors|Gigliotti, Emanuel|3|2|0|2|7
River Plate|Carbonero, Carlos Mario|4|2|0|0|6
Arsenal|Echeverría, Mariano|6|0|0|0|6
Olimpo|Valencia, José Adolfo|6|0|0|0|6
River Plate|Cavenaghi, Fernando Ezequiel|4|0|0|2|6
Boca Juniors|Riquelme, Juan Román|1|0|1|3|5
这是我的代码:
<?php
$handle = fopen("tp7-datos-goleadores.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
//hago el asociativo
$porciones = explode("|", $line );
$arrAsociativo[$porciones[0]] = $porciones[6];
}
fclose($handle);
} else {
echo "error al leer el archivo";
}
foreach($arrAsociativo as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
但它会在 html 上返回这个:
Key=V�lez Sarsfield, Value=9
Key=Estudiantes, Value=8
Key=Boca Juniors, Value=5
Key=River Plate, Value=6
Key=Arsenal, Value=6
Key=Olimpo, Value=6
如您所见,我的原始文本中有 2 个 Boca Juniors 和 2 个 River Plate,但是当我遍历数组时,我只能从中得到一个。这是为什么呢?
【问题讨论】:
-
您不能有两个具有相同键值的条目。
-
@Nick 所以,我不能把它们都展示出来吗?
-
你可能想要一个多维数组,例如
$arrAsociativo[$porciones[0]][] = $porciones[6];但随后$x_value将是一个数组,而不是一个数字
标签: php arrays associative