如何在数组中获取特定值

时间:2010-09-08 17:32:06

标签: php arrays multidimensional-array

我会清楚解释

$array  = array("1" => array(0 =>"aa",1 =>"bb", 2 => "cc",3=>"dd"),
                "2" => array(0 =>"aa1",1 =>"bb1", 2 => "cc1",3=>"dd1"));

这个二维是

$array2[$a][$b];

我知道$ a值而$ b未知

如果我使用$a =1,我想过滤array(0 =>"aa",1 =>"bb", 2 => "cc",3=>"dd")此数组

但我需要得到第二个数组元素。任何功能都可以做到吗?

2 个答案:

答案 0 :(得分:4)

您就是这样做的:

$a = array(1=>'a', 2=>'b', 3=>'c');

//display the value with key 2:
echo $a[2];

//remove the value with key 2 (throw-out / bring-out in your language)
unset($a[2]);

//now display whole array to show that value with key 2 is gone
print_r($a);

输出:

  

B'/ P>

然后输出:

  

数组([1] => a [3] => c)

答案 1 :(得分:0)

制作新数组,以便为​​密钥

指定特定元素
$newarr = array(2,5,8);
foreach($arr1 as v1){
foreach($arr2 as $k => $v2 ){
if (in_array($k,$newarr))
{
// process ... 
}
}
}