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

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

WBOY
WBOYOriginal
2016-06-13 13:00:241065browse

关于数组重组。

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 />
)

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

------解决方案--------------------
另类的写法
$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);
Array
(
    [0] => Array
        (
            [url] => 123
        )

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

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

)

Statement:
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