PHP-按值从多维数组中删除

PHP-按值从多维数组中删除,php,arrays,Php,Arrays,我有一个多维PHP数组,希望删除任何与值不匹配的元素(以行为单位) 例如: $user_types = [ 0 => ['ut_id' => 32, 'type' => 'admin'], 1 => ['ut_id' => 31, 'type' => 'user'], 2 => ['ut_id' => 801, 'type' => 'editor'] ]; 假设我只想要'type'='admin'中的元素。我希望输出

我有一个多维PHP数组,希望删除任何与值不匹配的元素(以行为单位)

例如:

$user_types = [
    0 => ['ut_id' => 32, 'type' => 'admin'],
    1 => ['ut_id' => 31, 'type' => 'user'],
    2 => ['ut_id' => 801, 'type' => 'editor']
];
假设我只想要
'type'='admin'
中的元素。我希望输出为:

$user_types = [
     0 => ['ut_id' => 32, 'type' => 'admin']
]
我还需要确保数组是按顺序键控的。因此,如果我只想要
type==“editor”
,数组键仍然应该是0(而不是2),例如

我已经看过了,但这不涉及多维数组

我还见过一些使用
foreach
循环的解决方案,但这似乎效率很低

有人能告诉我看什么的方向吗?对于多维数组,我找不到任何处理这个问题的示例。我见过,但这似乎效率低下,大约是6年前写的。

您可以在这里使用php函数:

<?php
$user_types = [
    0 => ['ut_id' => 32, 'type' => 'admin'],
    1 => ['ut_id' => 31, 'type' => 'user'],
    2 => ['ut_id' => 801, 'type' => 'editor']
];

$type = 'admin';
print_r(
    array_values(
        array_filter($user_types, function($entry) use ($type){
            return $entry['type'] === $type;
        })
    )
);

$type = 'editor';
print_r(
    array_values(
        array_filter($user_types, function($entry) use ($type){
            return $entry['type'] === $type;
        })
    )
);
您可以在此处使用php函数:

<?php
$user_types = [
    0 => ['ut_id' => 32, 'type' => 'admin'],
    1 => ['ut_id' => 31, 'type' => 'user'],
    2 => ['ut_id' => 801, 'type' => 'editor']
];

$type = 'admin';
print_r(
    array_values(
        array_filter($user_types, function($entry) use ($type){
            return $entry['type'] === $type;
        })
    )
);

$type = 'editor';
print_r(
    array_values(
        array_filter($user_types, function($entry) use ($type){
            return $entry['type'] === $type;
        })
    )
);

我将研究多维数组搜索并从那里开始。我将注意到,数字索引数组总是从
0
开始。不能有键为
2
的单个元素。结合
array\u value
@StuartWagner是的,在php中对数组键没有这样的限制。@jeroen你完全正确。我错了。我会研究多维数组搜索并从那里开始。我将注意到,数字索引数组总是从
0
开始。不能有键为
2
的单个元素。结合
array\u value
@StuartWagner是的,在php中对数组键没有这样的限制。@jeroen你完全正确。我错了。工作完美,效率很高,谢谢。我将在几分钟内接受答案,如果允许的话:)工作完美且非常高效,谢谢。如果允许,我会在几分钟内接受答案:)
Array
(
    [0] => Array
        (
            [ut_id] => 32
            [type] => admin
        )

)
Array
(
    [0] => Array
        (
            [ut_id] => 801
            [type] => editor
        )

)