首頁 >後端開發 >php教程 >php数组转换的问题

php数组转换的问题

WBOY
WBOY原創
2016-06-06 20:49:491183瀏覽

<code>Array(
[server] => Array(
        [0] => hk
        [1] => jp
    )
[fromdate] => Array(
        [0] => 2013-07-17
        [1] => 2013-06-19
))
</code>

以上数组如果最快最简单的方法转换成为

<code>Array(
[0] => Array(
    [server] => 'hk',
    [fromdate] => '2013-07-17'
)
[1] => Array(
    [server] => 'jp',
    [fromdate] => '2013-07-17'
))
</code>

回复内容:

<code>Array(
[server] => Array(
        [0] => hk
        [1] => jp
    )
[fromdate] => Array(
        [0] => 2013-07-17
        [1] => 2013-06-19
))
</code>

以上数组如果最快最简单的方法转换成为

<code>Array(
[0] => Array(
    [server] => 'hk',
    [fromdate] => '2013-07-17'
)
[1] => Array(
    [server] => 'jp',
    [fromdate] => '2013-07-17'
))
</code>

<code class="lang-php">// 源数组
$source = array(
    'server' => array('hk','jp'),
    'fromdate' => array('2013-07-17','2013-07-08')
);

//目标数组
$result = array();
foreach($source as $key=>$value){
    $j = 0;
    // 当然用这里用foreach也可以实现,但是for循环更能直观了解它的怎么工作的。
    for($i=0;$i<count foreach as echo>';
print_r($result);
echo '</count></code>
';

<code>$source = array(
    'server' => array('hk','jp'),
    'fromdate' => array('2013-07-17','2013-07-08')
);
$new_array = array_map(function($server,$fromdate) {
    return array('server'=>$server,'fromdate'=>$fromdate);
}, $source['server'],$source['fromdate']); //代码比较少
</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn