Home >Backend Development >PHP Tutorial >返回给定索引的值

返回给定索引的值

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-23 13:36:381054browse

请问下,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');$key = array('a','b','p');foreach($key as $k) $val[] = $a[$k];print_r($val);
Array(    [0] => apple    [1] => banana    [2] => pear)

$a = array(  'a'=>'apple',  'b'=>'banana',  'p'=>'pear');$key = array('a','b','p');$val = array();foreach($key as $v){    array_push($val, $a[$v]);}print_r($val);

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



谢谢,所以PHP没有自带数组来处理就是了?想说有提供,就可以不用再写代码处理

没有自带,你就自己写

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
Previous article:Linux+nginx使用if判断问题Next article:水印乱码