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

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

WBOY
Release: 2016-06-06 20:09:06
Original
860 people have browsed it

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

回复内容:

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

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

array_fill_keys

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