在php中使用数组函数通过比较标志划分数组

在php中使用数组函数通过比较标志划分数组,php,arrays,Php,Arrays,php中有一个数组 $sampleArray = array('first' => 'first', 'second' => 'second', 'third' => 'third'); 我需要根据值$check拆分数组,如果$check为“秒”,则需要像这样拆分数组 $requiredArray = array('third'=>'third') $nonReqArray = array('first'=>'first','second'=>'second

php中有一个数组

$sampleArray = array('first' => 'first', 'second' => 'second', 'third' => 'third');
我需要根据值
$check
拆分数组,如果
$check
为“秒”,则需要像这样拆分数组

$requiredArray = array('third'=>'third')
$nonReqArray = array('first'=>'first','second'=>'second')
如果
$check
为“第三个”,则
$requiredArray
将为空。请提出解决办法? 试过这个吗

$afterResult = array_filter($sampleArray, function($key) use (&$requiredArray, &$nonReqArray, &$check) { 
     
        if($key > $check){
            $requiredArray[$key] = $key;
        }else{

            $nonReqArray[$key] = $key;

        }
        return true;
    }, 
    ARRAY_FILTER_USE_KEY);

对于循环,通常使用
进行求解。但是我们可以通过
array\u filter
array函数来解决。主要问题是索引不是数字的,所以比较不容易

    $cmp = false;
    $afterResult = array_filter($sampleArray, function($key) use (&$requiredArray, &$nonReqArray, &$check, &$cmp) { 
             
                if($cmp){
                    $requiredArray[$key] = $key;
                }else{

                    $nonReqArray[$key] = $key;

                }
               if($key == $check){
                  $cmp =true;
                }
                return true;
            }, 
            ARRAY_FILTER_USE_KEY);

这对我很有用。

到目前为止,你想出了什么主意?
$i=array\u search($check,$sample)$r=数组×切片($sample,0,$i)$n=数组×切片($sample,$i)
实现,对于
循环,需要使用数组函数解决方案-@Ro Achterberggyou在PHP7.4中标记,但您甚至没有使用箭头函数抱歉,不知道箭头函数。你能更新一下答案吗?:-@DharmanNo,我觉得你的答案没那么有用,但我也不认为它没用。我既不投反对票,也不投赞成票,但我也认为试图改变它毫无意义。我们已经有了这样的答案,它们作为一个副本联系在一起。如果您想了解箭头函数,可以查看PHP手册。