我正在玩数组函数以更好地理解。
下面我有三个数组:
$cardNumber = array(2, 4, 5, 8, 9);
$playerName = array('Julian', 'Brad', 'Chloe', 'Laura', 'Paul');
$playerWins = array(4, 5, 1, 2, 6);
我正在尝试实现以下数组结构:
array (size=5)
2 =>
array (size=2)
0 => string 'Julian' (length=6)
1 => int 4
4 =>
array (size=2)
0 => string 'Brad' (length=4)
1 => int 5
5 =>
array (size=2)
0 => string 'Chloe' (length=5)
1 => int 1
8 =>
array (size=2)
0 => string 'Laura' (length=5)
1 => int 2
9 =>
array (size=2)
0 => string 'Paul' (length=4)
1 => int 6
我决定将$playerName
和$playerWins
与array_combine()
合并,然后将结果数组与$cardNumber
合并,这不会产生我期望的输出。我是否错误地理解array_combine()
?
答案 0 :(得分:1)
您可以将array_map
与 null 一起用作回调,并使用两个数据数组为您提供所需的数组结构。然后使用array_combine
:
$result = array_combine($cardNumber, array_map(null, $playerName, $playerWins));
例如:
$result = array_map(null, $playerName, $playerWins);
收率:
Array
(
[0] => Array
(
[0] => Julian
[1] => 4
)
[1] => Array
(
[0] => Brad
[1] => 5
)
[2] => Array
(
[0] => Chloe
[1] => 1
)
[3] => Array
(
[0] => Laura
[1] => 2
)
[4] => Array
(
[0] => Paul
[1] => 6
)
)
然后合并得到钥匙:
$result = array_combine($cardNumber, $result);
收率:
Array
(
[2] => Array
(
[0] => Julian
[1] => 4
)
[4] => Array
(
[0] => Brad
[1] => 5
)
[5] => Array
(
[0] => Chloe
[1] => 1
)
[8] => Array
(
[0] => Laura
[1] => 2
)
[9] => Array
(
[0] => Paul
[1] => 6
)
)