Split flat array into two arrays based on value
arrays, filtering, php
Solution
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 `$myArray` to `$secondaryArray`.
Note: It's case sensitive
Problem
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) ``` 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?