好吧,伙计们我现在已经挣扎了大约5个小时了,我意识到我的大脑已经被炒了以为我最好让一些人知道他们在做什么:p现在我只需要正确解释问题。
我希望显示所有目标得分手,只需知道每个目标得分者的名字和目标时间: Carlton Dickson(23,53)Danniel Tasker(1)。
我能想到的唯一方法是用一个新的数组替换数组,其中包含元素中的所有目标。因此,为名称创建一行,并为其中的所有目标创建一行。也许我不是以正确的方式看待这个?
干杯。
array(8) {
[0]=> array(3) {
["fname"]=>
string(7) "Carlton"
["sname"]=>
string(7) "Dickson"
["time"]=>
string(2) "23"
}
[1]=> array(3) {
["fname"]=>
string(7) "Carlton"
["sname"]=>
string(7) "Dickson"
["time"]=>
string(2) "53"
}
[2]=> array(3) {
["fname"]=>
string(6) "Daniel"
["sname"]=>
string(6) "Tasker"
["time"]=>
string(1) "1"
}
答案 0 :(得分:0)
您可以这样重建阵列:
$newArray = array();
foreach($array as $player) {
$name = $player['fname'] . " " . $player["sname"];
if(!array_key_exists($name, $newArray)) {
$newArray[$name] = array();
}
$newArray[$name][] = $player['time'];
}
结果数组将是:
Array
(
[Carlton Dickson] => Array
(
[0] => 23
[1] => 53
)
[Daniel Tasker] => Array
(
[0] => 1
)
)
即。键是名称,值是目标的数组。当然,您也可以将目标作为字符串连接起来,这取决于您想要对数据做什么。
然后打印只是遍历数组:
foreach($newArray as $player => $goals) {
echo $player . " (" . implode(',', $goals) . ") <br />"; // or PHP_EOL
}
答案 1 :(得分:0)
$scorers = array();
foreach ($goals as $goal) {
$name = "{$goal['fname']} {$goal['sname']}";
$scorers[$name] = isset($scorers[$name]) ? "{$scorers[$name]}, {$goal['time']}" : $goal['time'];
}
foreach ($scorers as $scorer => $goals) {
echo "$scorer ($goals)";
}
假设你有一个$goals
数组:
$goals = array(
array('fname' => 'Carlton', 'sname' => 'Dickson', 'time' => '23'),
array('fname' => 'Carlton', 'sname' => 'Dickson', 'time' => '53'),
array('fname' => 'Daniel', 'sname' => 'Tasker', 'time' => '1')
);