php 如何仅在数组中保留特定的数组键/值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23158393/
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 16:32:32  来源:igfitidea点击:

How to keep only specific array keys/values in array?

phpmultidimensional-array

提问by Benn

I have a multidimensional array that I am searching trough for specific values. If those values are found I need to extract the indexes with those values ( make new array ) and remove all others.

我有一个多维数组,我正在搜索特定值的槽。如果找到这些值,我需要使用这些值提取索引(创建新数组)并删除所有其他值。

array_intersect worked fine on php 5.3 , now on 5.4 it complains Notice: Array to string conversion.

array_intersect 在 php 5.3 上工作正常,现在在 5.4 上它抱怨注意:数组到字符串的转换。

I found that array_intersect has an issue with multidimensional array on 5.4. https://bugs.php.net/bug.php?id=60198

我发现 array_intersect 在 5.4 上有一个多维数组的问题。 https://bugs.php.net/bug.php?id=60198

This is $options array I am searching trough

这是我正在搜索的 $options 数组

Array (

    [index1] => html
    [index2] => html
    [index3] => slide
    [index4] => tab
    [index5] => Array
        (
            [0] => 123
        )

)

code that works on php 5.3.x

适用于 php 5.3.x 的代码

$lookfor   = array('slide', 'tab');
$found     = array_intersect($options, $lookfor);


print_r($found);


Array
(
    [index3] => slide
    [index4] => tab
)

but in 5.4.x this trows the error mentioned above.

但在 5.4.x 中,这会引发上述错误。

What would be another way to do this without a loop please. and without suppressing the error.

请问没有循环的另一种方法是什么。并且没有抑制错误。

Thank you!

谢谢!

采纳答案by Amal Murali

array_intersect()isn't recursive. The function assumes the array is just one level deep and expects all the array elements to be scalars. When it finds a non-scalar value, i.e. a sub-array, it throws a Notice.

array_intersect()不是递归的。该函数假设数组只有一层深,并期望所有数组元素都是标量。当它找到一个非标量值,即一个子数组时,它会抛出一个通知。

This is vaguely mentioned in the documentation for array_intersect():

文档中array_intersect()含糊地提到了这一点:

Note: Two elements are considered equal if and only if: (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

注意:两个元素被认为相等当且仅当: (string) $elem1 === (string) $elem2。换句话说:当字符串表示相同时。

One solution I could think of is to use array_filter():

我能想到的一种解决方案是使用array_filter()

$lookfor = array('html', 'slide');
$found   = array_filter($options, function($item) use ($lookfor) {
    return in_array($item, $lookfor);
});

Note:This still performs a looping and isn't any better than a simple foreach. In fact, it might be slower than a foreachif the array is large. I have no idea why you're trying to avoid loops — I personally think it'd be more cleaner if you just use a loop.

注意:这仍然执行循环,并不比简单的foreach. 事实上,foreach如果数组很大,它可能比 a 慢。我不知道你为什么要避免循环——我个人认为如果你只使用循环会更干净。

Demo

演示

Another solution I could think of is to remove the sub-arrays before using array_intersect():

我能想到的另一个解决方案是在使用之前删除子数组array_intersect()

<?php

$options = array(
    'index1' => 'html',
    'index2' => 'html',
    'index3' => 'slide',
    'index4' => 'tab',
    'index5' => array(123),
);

$lookfor = array('html', 'slide');
$scalars = array_filter($options,function ($item) { return !is_array($item); });
$found = array_intersect ($scalars, $lookfor);

print_r($found);

Demo

演示

回答by zwacky

you could use array_filter()

你可以使用 array_filter()

$arr = array(
  'index1' => 'html',
  'index2' => 'html',
  'index3' => 'slide',
  'index4' => 'tab',
  'index5' => array(0 => 123),
);

$with = array('html', 'slide');
$res = array_filter($arr, function($val) use ($with) {
    return in_array($val, $with);
});

this will rerturn index1, index2 and index3.

这将返回 index1、index2 和 index3。

edit: just read your comment that your array will hold lots of entries. array_filterwill of course loop with a condition over them and create a new array.

编辑:只需阅读您的评论,即您的数组将包含大量条目。array_filter当然会在它们上面循环一个条件并创建一个新数组。