对于从关联数组中获取键和值的每个键和值,如果它们共享相同的键名,则不会 return 所有键和值 - PHP
For each to get keys and values from associative array doesn't return all keys and values if they share the same key name - PHP
我正在读取包含以下文本的文件:
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>";
}
?>
但是 returns 这个 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,但是当我遍历数组时,我只能从每个中得到一个。这是为什么?
您可以通过循环回显索引。或者,只需创建一个带有对联的新数组。
<?php
$data =<<<DATA
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
DATA;
$lines = preg_split('/\R/', $data);
foreach($lines as $line) {
$rows[] = str_getcsv($line, '|');
}
foreach($rows as $row) {
$name_values[] = [$row[0], $row[6]];
}
foreach($name_values as list($name, $value)) {
echo 'Name: ', $name, ', Value: ', $value, "\n";
}
输出:
Name: Vélez Sarsfield, Value: 9
Name: Estudiantes, Value: 8
Name: Boca Juniors, Value: 7
Name: River Plate, Value: 6
Name: Arsenal, Value: 6
Name: Olimpo, Value: 6
Name: River Plate, Value: 6
Name: Boca Juniors, Value: 5
和$name_values的数据结构:
var_export($name_values);
输出:
array (
0 =>
array (
0 => 'Vélez Sarsfield',
1 => '9',
),
1 =>
array (
0 => 'Estudiantes',
1 => '8',
),
2 =>
array (
0 => 'Boca Juniors',
1 => '7',
),
3 =>
array (
0 => 'River Plate',
1 => '6',
),
4 =>
array (
0 => 'Arsenal',
1 => '6',
),
5 =>
array (
0 => 'Olimpo',
1 => '6',
),
6 =>
array (
0 => 'River Plate',
1 => '6',
),
7 =>
array (
0 => 'Boca Juniors',
1 => '5',
),
)
我正在读取包含以下文本的文件:
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>";
}
?>
但是 returns 这个 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,但是当我遍历数组时,我只能从每个中得到一个。这是为什么?
您可以通过循环回显索引。或者,只需创建一个带有对联的新数组。
<?php
$data =<<<DATA
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
DATA;
$lines = preg_split('/\R/', $data);
foreach($lines as $line) {
$rows[] = str_getcsv($line, '|');
}
foreach($rows as $row) {
$name_values[] = [$row[0], $row[6]];
}
foreach($name_values as list($name, $value)) {
echo 'Name: ', $name, ', Value: ', $value, "\n";
}
输出:
Name: Vélez Sarsfield, Value: 9
Name: Estudiantes, Value: 8
Name: Boca Juniors, Value: 7
Name: River Plate, Value: 6
Name: Arsenal, Value: 6
Name: Olimpo, Value: 6
Name: River Plate, Value: 6
Name: Boca Juniors, Value: 5
和$name_values的数据结构:
var_export($name_values);
输出:
array (
0 =>
array (
0 => 'Vélez Sarsfield',
1 => '9',
),
1 =>
array (
0 => 'Estudiantes',
1 => '8',
),
2 =>
array (
0 => 'Boca Juniors',
1 => '7',
),
3 =>
array (
0 => 'River Plate',
1 => '6',
),
4 =>
array (
0 => 'Arsenal',
1 => '6',
),
5 =>
array (
0 => 'Olimpo',
1 => '6',
),
6 =>
array (
0 => 'River Plate',
1 => '6',
),
7 =>
array (
0 => 'Boca Juniors',
1 => '5',
),
)