我对PHP数组/关键内容并不是很好,我在过去3小时内一直在努力解决这个问题,所以我正在寻求帮助。我需要对这件事情有新面貌。我做了一些研究,但我发现只有2把钥匙,所以我感到困惑。
我有这个数组:
阵列( [0] =>排列 ( [season] => 1 [开始] => 2013 [end] => 2014 [锦标赛] =>排列 ( [0] =>排列 ( [name] =>法甲1 [result] => 3 )
) ) [1] => Array ( [season] => 3 [start] => 2012 [end] => 2013 [championship] => Array ( [0] => Array ( [name] => Ligue 1 [result] => 1 ) ) ) [2] => Array ( [season] => 3 [start] => 2012 [end] => 2013 [championship] => Array ( [0] => Array ( [name] => Coupe de France [result] => 1 ) ) ) [3] => Array ( [season] => 4 [start] => 2011 [end] => 2012 [championship] => Array ( [0] => Array ( [name] => Ligue 1 [result] => 1 ) ) )
)
如您所见,有些值是相同的,有些则不是。 我想要做的是重新组合同一赛季ID的“锦标赛”关键值。 所以我的输出将如下所示:
阵列( [0] =>排列 ( [season] => 1 [开始] => 2013 [end] => 2014 [锦标赛] =>排列 ( [0] =>排列 ( [name] =>法甲1 [result] => 3 )
) ) [1] => Array ( [season] => 3 [start] => 2012 [end] => 2013 [championship] => Array ( [0] => Array ( [name] => Ligue 1 [result] => 1 ) [1] => Array ( [name] => Coupe de France [result] => 1 ) ) ) [2] => Array ( [season] => 4 [start] => 2011 [end] => 2012 [championship] => Array ( [0] => Array ( [name] => Ligue 1 [result] => 1 ) ) )
)
任何帮助都将不胜感激。
提前致谢。
#编辑编辑编辑
#取得一些空气,最后回来了解决方案
function sortList($arrayToSort) {
$array = $arrayToSort;
for ($i = 0; $i < count($array); $i++) {
for ($j = 0; $j < count($array); $j++) {
if ($array[$i]['season'] == $array[$j]['season'] && $i != $j) {
$array[$i]['championship'] = array_merge($array[$i]['championship'], $array[$j]['championship']);
unset($array[$j]);
}
}
}
return array_values($array);
}
这似乎有效。但我不确定这是否安全,这意味着:如果不需要,不会丢失或删除任何结果。当我们在数组中使用unset()时,似乎PHP没有重新索引...但是如果有人能够确认这是正确的做法,那将有所帮助。 我在这里关注的是我们在迭代时修改数组,所以这个解决方案听起来不像是一个“好习惯”。