Home > Backend Development > PHP Tutorial > php数组转换的问题

php数组转换的问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:49:49
Original
1223 people have browsed it

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

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

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

回复内容:

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

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

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

<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>
Copy after login
';

<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>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template