PHP在特定元素之后将数组分为两部分

时间:2019-02-24 20:53:15

标签: php arrays

我想通过数组中的特定元素将php数组分为两部分,因此我使用此功能:

function split_by_contents($ary, $pattern){
  if (!is_array($ary)) return FALSE; // brief error checking

  // keep track of the last position we had a match, and the current
  // position we're searching
  $last = -1; $c = 0;

  // iterate over the array
  foreach ($ary as $v){
  // check for a pattern match
  if (preg_match($pattern, $v)){
  // we found a match, record it
  $last = $c;
  }
  // increment place holder
   $c++;
  }


 return array(array_slice($ary, 0, $last), array_slice($ary, $last + 1));

 }

然后我这样称呼它:

$pattern = 'steps';
list($array1 , $array2) =  split_by_contents($ary, $pattern) ; 

但是它不起作用,两个数组($ array1和array2)包含相同的值。 有什么想法吗

0 个答案:

没有答案