php PHP根据值将数组拆分为两个数组

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

PHP splitting array into two arrays based on value

phparrayssortingsplit

提问by Austin

I have a PHP array that I am trying to split into 2 different arrays. I am trying to pull out any values that contain the word "hidden". So one array would contain all the values that do not contain the word "hidden". The other array would contain all the values that do contain the word "hidden". I just can't figure out how to do it though.

我有一个 PHP 数组,我试图将其拆分为 2 个不同的数组。我正在尝试提取包含“隐藏”一词的任何值。因此,一个数组将包含所有不包含“隐藏”一词的值。另一个数组将包含所有包含“隐藏”一词的值。我就是不知道该怎么做。

The original array is coming from a form post that contains keys and values from a bunch of check boxes and hidden inputs. so the actual post value looks something like this:

原始数组来自一个表单帖子,其中包含来自一堆复选框和隐藏输入的键和值。所以实际的帖子值看起来像这样:

Group1 => Array([0] => item1,[1] => item2hidden,[2] => item3,[3] => item4,[4] => item5hidden)

so to simplify it:

所以为了简化它:

$myArray = Array(item1, item2hidden, item3, item4, item5hidden)

final output

最终输出

$arr1 = (item1, item3, item4)
$arr2 = (item2hidden, item5hidden)

Anyone know how to do something like this?

有谁知道如何做这样的事情?

采纳答案by Vlad Preda

This should do the trick:

这应该可以解决问题:

$myArray = array('item1', 'item2hidden', 'item3', 'item4', 'item5hidden');
$secondaryArray = array();

foreach ($myArray as $key => $value) {
    if (strpos($value, "hidden") !== false) {
        $secondaryArray[] = $value;
        unset($myArray[$key]);
    }
}

It moves all the entries that contain "hidden" from the $myArrayto $secondaryArray.

它将所有包含“隐藏”的条目从 移动$myArray$secondaryArray

Note:It's case sensitive

注意:区分大小写

回答by Glavi?

You can use array_filter()function:

您可以使用array_filter()功能:

$myArray = array('item1', 'item2hidden', 'item3', 'item4', 'item5hidden');

$arr1 = array_filter($myArray, function($v) { return strpos($v, 'hidden') === false; });
$arr2 = array_diff($myArray, $arr1);

Demo

演示

回答by Viacheslav Kondratiuk

$myArray = Array('item1', 'item2hidden', 'item3', 'item4', 'item5hidden');
$arr1 = array();
$arr2 = array();    
foreach ($myArray as $item) {
    if (strpos($item, "hidden") !== false) {
        $arr1[] = $item;
    } else {
        $arr2[] = $item;
    }
}

This solution checks if 'hidden' present at current item, if no, move to $arr1else to $arr2

此解决方案检查当前项目中是否存在“隐藏”,如果不存在,则移至$arr1其他$arr2

回答by Nikolay Krasnov

You can use array_filter:

您可以使用 array_filter:

function filtreHiddens($e) {
    if (isset($e['hidden']) && $e['hidden']) return true;
    else return false;
}

function filtreNotHiddens($e) {
    if (isset($e['hidden']) && !$e['hidden']) return true;
    else return false;
}

$arrayToFiltre = array(
    array('hidden' => true, 'someKey' => 'someVal'),
    array('hidden' => false, 'someKey1' => 'someVal1'),
    array('hidden' => true, 'someKey2' => 'someVal3'),
);

$hidden = array_filter($arrayToFiltre, 'filtreHiddens');
$notHidden = array_filter($arrayToFiltre, 'filtreNotHiddens');

print_r($hidden);
print_r($notHidden);

回答by fresbeeplayer

Maybe it's just me, but I would go for the clarity of regular expressions...

也许这只是我,但我会为了正则表达式的清晰度而去......

foreach($myArray as $item) {
    if (preg_match("/hidden$/i", $item)) {
        array_push($arr2, $item);
    } else {
        array_push($arr1, $item);
    }
}