我在php中有这样的数组:
Array
(
[Color] => Array
(
[0] => Pink
[1] => Pink
[2] => Pink
[3] => Pink
)
[Size] => Array
(
[0] => S
[1] => S
[2] => S
[3] => M
)
[Brand] => Array
(
[0] => Raymond
[1] => Levis
[2] => Lee
[3] => Raymond
)
[price] => Array
(
[0] => 1500
[1] => 1500
[2] => 1500
[3] => 1600
)
[quantity] => Array
(
[0] => 2
[1] => 2
[2] => 2
[3] => 3
)
)
我想从上面的数组中创建新数组,如下所示:
Array(
[0] => Array
(
[Color] => Pink
[Size] => S
[Brand] => Raymond
[price] => 1500
[quantity] => 2
)
[1] => Array
(
[Color] => Pink
[Size] => S
[Brand] => Levis
[price] => 1500
[quantity] => 2
)
)
等等。任何帮助将不胜感激。提前谢谢。
答案 0 :(得分:2)
foreach ($old as $key => $values) {
foreach ($values as $k => $val) {
$new[$k][$key] = $val;
}
}
答案 1 :(得分:1)
只需对输入数组进行两次循环:
<?php
$input = array (
'Color' => array (
'Pink',
'Pink',
'Pink',
'Pink'
),
'Size' => array (
'S',
'S',
'S',
'M'
),
'Brand' => array (
'Raymond',
'Levis',
'Lee',
'Raymond'
),
'price' => array (
1500,
1550,
1500,
1600
)
);
$output = array();
foreach ($input as $key => $values) {
foreach ($values as $k => $val) {
$output[$k][$key] = $val;
}
}
echo '<pre>';
print_r($output);
/*
OUTPUT:
Array
(
[0] => Array
(
[Color] => Pink
[Size] => S
[Brand] => Raymond
[price] => 1500
)
[1] => Array
(
[Color] => Pink
[Size] => S
[Brand] => Levis
[price] => 1550
)
[2] => Array
(
[Color] => Pink
[Size] => S
[Brand] => Lee
[price] => 1500
)
[3] => Array
(
[Color] => Pink
[Size] => M
[Brand] => Raymond
[price] => 1600
)
)
*/
?>