在php中过滤数组,同时具有值和键相关条件

在php中过滤数组,同时具有值和键相关条件,php,array-filter,Php,Array Filter,我试图过滤一个数组,其中的filter函数应该检查多个条件。例如,如果元素x以大写字母开头,则filter函数应返回true。除此之外,如果元素x之前的元素满足某些其他条件,那么元素x应该不留在数组中,因此过滤函数应该返回false 问题是数组_filter中的回调函数只传递元素的值,而不传递其键。。。使用array_search执行一些神奇的操作可能会奏效,但我只是想知道,对于这个特定问题,我是否找错了地方 $newArray=array(); foreach($oldArray as $ke

我试图过滤一个数组,其中的filter函数应该检查多个条件。例如,如果元素x以大写字母开头,则filter函数应返回true。除此之外,如果元素x之前的元素满足某些其他条件,那么元素x应该留在数组中,因此过滤函数应该返回false

问题是数组_filter中的回调函数只传递元素的值,而不传递其键。。。使用array_search执行一些神奇的操作可能会奏效,但我只是想知道,对于这个特定问题,我是否找错了地方

$newArray=array();
foreach($oldArray as $key=>$value){
   if(stuff){
      $newArray[$key]=$value;
   }
}


听起来像是一个好的旧foreach循环的例子:

foreach ($arr as $k => $v) {
  // filter
  if (!$valid)
    unset($arr[$k]);
}

你用过简单的foreach吗

$prev;
$first = true;
$result = array();
foreach ($array as $key => $value)
{
    if ($first)
    {
        $first = false;

        // Check first letter. If successful, add it to $result

        $prev = $value;
        continue; // with this we are ignoring the code below and starting next loop.
    }

    // check $prev's first letter. if successful, use continue; to start next loop.
    // the below code will be ignored.

    // check first letter... if successful, add it to $result
}

只要试着在
中遍历数组(对于
:)我想:-)除了我希望有一种更优雅或“功能性”的方法之外。。。但我想这样就可以了;-)谢谢
$prev;
$first = true;
$result = array();
foreach ($array as $key => $value)
{
    if ($first)
    {
        $first = false;

        // Check first letter. If successful, add it to $result

        $prev = $value;
        continue; // with this we are ignoring the code below and starting next loop.
    }

    // check $prev's first letter. if successful, use continue; to start next loop.
    // the below code will be ignored.

    // check first letter... if successful, add it to $result
}