回到给定索引的值

WBOY
Release: 2016-06-13 12:18:06
Original
944 people have browsed it

返回给定索引的值
请问下,PHP是否存在这样的数组函数

已知数组
$a['a']=>'apple',$a['b']=>'banana,$a['p']=>'pear'

已知某个数组
$key = array('a','b','p');

获得对应数组$a的值,并得到数组如下
$val = array('apple','banana','pear');
------解决思路----------------------

$a = array('a' => 'apple', 'b' => 'banana', 'p' => 'pear');<br />$key = array('a','b','p');<br />foreach($key as $k) $val[] = $a[$k];<br /><br />print_r($val);
Copy after login
Array<br />(<br />    [0] => apple<br />    [1] => banana<br />    [2] => pear<br />)<br /><br />
Copy after login

------解决思路----------------------
<br />$a = array(<br />  'a'=>'apple',<br />  'b'=>'banana',<br />  'p'=>'pear'<br />);<br /><br />$key = array('a','b','p');<br /><br />$val = array();<br /><br />foreach($key as $v){<br />    array_push($val, $a[$v]);<br />}<br /><br />print_r($val);<br />
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