PHP根据值将数组分为两个数组 [英] PHP splitting array into two arrays based on value

查看:728
本文介绍了PHP根据值将数组分为两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP数组,试图将其拆分为2个不同的数组.我正在尝试提取包含"hidden"一词的所有值.因此,一个数组将包含所有不包含单词"hidden"的值.另一个数组将包含确实包含单词"hidden"的所有值.我只是不知道该怎么做.

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.

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

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)

为简化起见:

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

最终输出

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

有人知道该怎么做吗?

推荐答案

这应该可以解决问题:

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

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

它将所有包含隐藏"的条目从$myArray移动到$secondaryArray.

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

注意:区分大小写

这篇关于PHP根据值将数组分为两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆