Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过内部值获取数组元素_Php - Fatal编程技术网

Php 通过内部值获取数组元素

Php 通过内部值获取数组元素,php,Php,我有这样一个数组: $a = [ 0 => ['a' => ['id' => 10, 'value' => 111]], 1 => ['a' => ['id' => 20, 'value' => 222]], 2 => ['a' => ['id' => 30, 'value' => 333]] ]; 我想在array\u filter()中使用此数组来过滤$id比较值: $ids = [10, 20

我有这样一个数组:

$a = [
    0 => ['a' => ['id' => 10, 'value' => 111]],
    1 => ['a' => ['id' => 20, 'value' => 222]],
    2 => ['a' => ['id' => 30, 'value' => 333]]
];
我想在
array\u filter()
中使用此数组来过滤
$id
比较

$ids = [10, 20, 30];
$filtered = array_filter($ids, function($id) use($a) {
    return $a[$id][$value] == 222; //this is wrong, just to show what I'm trying
});

我该怎么做?谢谢

问题是,
$a
没有10、20、30键,只有0、1、2键。在闭包中使用
$a[$id]
时,它会对
$a[10]
$a[20]
$a[30]
(不存在)进行迭代


如果要筛选
$a
,请将
$a
作为数组过滤器的第一个参数。您使用
$ids
作为第一个参数,并且它不会对
$a

进行迭代。您的方法是错误的。你想过滤什么

$id
中的
$a
中具有
值的所有元素

然后这样做:

$ids = array();
foreach ($a as $item) {
    if ($item['a']['value'] == 222)
        $ids[] = $item['a']['id'];
}
更新
array\u filter
返回数组的完整项,这意味着它不仅仅返回
id

$ids = array_filter($a, function($v) { return $v['a']['value'] == 222; } );

实际上我不明白为什么你需要$ids数组。伙计们说对了,你应该走$a数组,代码就会

    $filtered = array_filter($a, function($item) {
        return $item['a']['value'] == 222; 
    });

从回调中的
var\u dump($id)
开始,查看它的结构。是的,这就是我想做的,但我想使用
array\u过滤器
。。。但我认为使用
array\u filter
无法做到这一点。更新后的
array\u filter
将完成部分工作。