PHP splitting array into two arrays based on value

前端 未结 5 1065
小蘑菇
小蘑菇 2021-02-19 06:43

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 v

5条回答
  •  遥遥无期
    2021-02-19 07:22

    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

提交回复
热议问题