我$categories在php中有一个数组,如下所示.
Array
(
[item_name] => Array
(
[0] => I-Phone
[1] => samsung
[2] => nokia
[3] => htc
)
[item_price] => Array
(
[0] => 30.00
[1] => 20
[2] => 10
[3] => 15
)
)
Run Code Online (Sandbox Code Playgroud)
我想把它的元素转换成,
Array
(
[0] => Array
(
[item_name] => I-Phone
[item_price] => 30.00
)
[1] => Array
(
[item_name] => samsung
[item_price] => 20
)
[2] => Array
(
[item_name] => nokia
[item_price] => 10
)
[2] => Array
(
[item_name] => htc
[item_price] => 15
)
)
Run Code Online (Sandbox Code Playgroud)
我尝试过使用foreach循环但没有工作.
$count=0;
foreach ($categories as $key=> $category)
{
$categories[$count] = $category[$key];
$categories[$count] = $category[$key];
$count++;
}
Run Code Online (Sandbox Code Playgroud)
这是一种方法:
$categories = array(
'item_name' => array('I-Phone', 'samsung', 'nokia','htc'),
'item_price' => array('30.00', '20', '10', '15')
);
$out = array();
foreach($categories as $key => $a){
foreach($a as $k => $v){
$out[$k][$key] = $v;
}
}
echo '<pre>';
print_r($out);
echo '</pre>';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
697 次 |
| 最近记录: |