Home >Backend Development >PHP Tutorial >将数组的值作为键,然后填充一个值组成新的数组,怎么做?以这个为例如何得到$a3?

将数组的值作为键,然后填充一个值组成新的数组,怎么做?以这个为例如何得到$a3?

WBOY
WBOYOriginal
2016-06-06 20:09:06856browse

<code>$a1 = array(1,2,3,4);
$a2 = 'b';
$a3 = array(1=>'b',2=>'b',3=>'b',4=>'b');</code>

回复内容:

<code>$a1 = array(1,2,3,4);
$a2 = 'b';
$a3 = array(1=>'b',2=>'b',3=>'b',4=>'b');</code>

<code>$a1 = array(1,2,3,4);
$a2 = 'b';
$a3 = array_fill_keys($a1, $a2);
var_dump($a3);</code>

array_fill_keys

<code class="php">// 用array_fill_keys更简单
$a2=array_map(function($v){
     return 'b';
},$a1);
$a3=array_combine($a1,$a2);</code>
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