Home > Backend Development > PHP Tutorial > php数组问题

php数组问题

WBOY
Release: 2016-06-06 20:52:12
Original
979 people have browsed it

有数组1和数组2,我想把数组1的值作为key,数组2的值作为value,存在数组3中,有什么方法吗?

回复内容:

有数组1和数组2,我想把数组1的值作为key,数组2的值作为value,存在数组3中,有什么方法吗?

<?php $a1=array("a","b","c","d");
$a2=array("Cat","Dog","Horse","Cow");
print_r(array_combine($a1,$a2));
?>
Copy after login

如果两个数组的个数一致且数组1和数组2的值相对应的话,直接用循环就可以搞定了吧。
下面是我测试用的代码,测试过没有问题的。

<code><?php $a = array('a', 'b', 'c', 'd');
$b = array('1', '2', '3', '4');
$c = array();
for($i=0,$l=count($b);$i<$l;$i++) {
    $c[$a[$i]] = $b[$i];
}
print_r($c);
?>
</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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template