Home > Backend Development > PHP Tutorial > 关于数组重组。该怎么处理

关于数组重组。该怎么处理

WBOY
Release: 2016-06-13 13:00:24
Original
1062 people have browsed it

关于数组重组。

Array<br />
(<br />
    [0] => 123<br />
    [1] => 456<br />
    [2] => 789<br />
)<br />
$url = array('123','456','789');<br />
能将上面这个数组,组装成下面的这个吗?<br />
<br />
$urls = array(array('url'=>'123'),array('url'=>'456'),array('url'=>'789'));<br />
<br />
<br />
<br />
<br />
<br />
<br />
Array<br />
(<br />
    [0] => Array<br />
        (<br />
            [url] => 123<br />
        )<br />
<br />
    [1] => Array<br />
        (<br />
            [url] => 456<br />
        )<br />
<br />
    [2] => Array<br />
        (<br />
            [url] => 789<br />
        )<br />
<br />
)
Copy after login

------解决方案--------------------
<br />
foreach ($url as $url_value) {<br />
$urls[] = array('url'=>$url_value);<br />
}<br />
Copy after login

------解决方案--------------------
另类的写法
$ar = array(123, 456, 789);<br />
$t = array_map('array_combine',array_chunk(array_fill(0,count($ar), 'url'), 1), array_chunk($ar, 1));<br />
print_r($t);
Copy after login
Array
(
    [0] => Array
        (
            [url] => 123
        )

    [1] => Array
        (
            [url] => 456
        )

    [2] => Array
        (
            [url] => 789
        )

)

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template