php 如何获取数组中的特定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3670391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:38:37  来源:igfitidea点击:

How can I get specific value in array

phparraysmultidimensional-array

提问by Gowri

I will explain clearly

我会解释清楚

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

In this two dimension are

在这两个维度中

$array2[$a][$b];

I know $a value and $b is unknown

我知道 $a 值而 $b 未知

If I using $a =1, I want to filter array(0 =>"aa",1 =>"bb", 2 => "cc",3=>"dd")this array

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

But I need to get the second array element. Any function is available to do that?

但我需要获取第二个数组元素。任何功能都可以做到这一点?

回答by shamittomar

This is how you do it:

这是你如何做到的:

$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);

This outputs:

这输出:

b

And then it outputs:

然后它输出:

Array ( [1] => a [3] => c )

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

回答by stalin wesley

make new array you want to sperate specific elements that key

制作新的数组,以分散关键的特定元素

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