19

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?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Austin
  • 1,619
  • 7
  • 25
  • 51

5 Answers5

30

You can use array_filter() function:

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

$arr1 = array_filter($myArray, function($v) { return strpos($v, 'hidden') === false; });
$arr2 = array_diff($myArray, $arr1);
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Glavić
  • 42,781
  • 13
  • 77
  • 107
  • Need to be `=== false;` instead of `== false;` – Narek Nov 20 '13 at 14:24
  • Doesn't work if you have array values inside your initial array. For instance, $myArray = [['id' => 1], ['id'] => 2]; – andnik Jul 22 '19 at 16:15
  • @andnik: just replace `strpos($v, ...`to `strpos($v['id'], ...` – Glavić Jul 24 '19 at 14:47
  • @Glavić, thanks, but I'm referring to second part. array_diff doesn't work when you are using multidimensional array. This link seems to contain answer I was looking for: https://stackoverflow.com/questions/11821680/array-diff-with-multidimensional-arrays – andnik Jul 24 '19 at 15:30
10

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

Vlad Preda
  • 9,780
  • 7
  • 36
  • 63
0
$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 $arr1 else to $arr2

Viacheslav Kondratiuk
  • 8,493
  • 9
  • 49
  • 81
0

You can use 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);
Nikolay Krasnov
  • 363
  • 1
  • 14
0

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);
    }
}
fresbeeplayer
  • 93
  • 1
  • 5