从数组PHP删除值 [英] php remove value from array
本文介绍了从数组PHP删除值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我需要帮助使用递归函数从数组中删除值
Hi I need help in removing values from an array using a recursive function
$array = [0] => testing,testing1
[1] => testing,testing1,testing2
[2] => testing,testing1,testing2,testing3
[3] => testing,testing1,testing2,testing3,tesing4
[4] => testing,testing1,testing2,testing3,tesing4
[5] => testing,testing1,testing2,testing3,tesing4
[6] => testing,testing1,testing2,testing3,tesing4
[7] => testing,testing1,testing2,testing3,tesing4
我需要检查阵列数,即如果count(数组[0])==计数(数组[1]),然后reutrn阵列
其他未设置(数组[值]);
从上面的数组我不得不删除数组[0],[1],[2]
并返回数组值的其余部分。
From the above array I have to remove array[0],[1],[2]
and return rest of the array values.
我试过下面code
$idx =10;
$separtor =',';
function array_delete($idx, $array,$separtor) {
$finalvalue = array();
for ($i = 0; $i < $idx; $i++) {
$values = explode($separtor, $array[$i]);
$valuesnext = explode($separtor, $array[$i+1]);
if(count($values) != count($valuesnext) )
{
unset($array[$i]);
// reset($array);
// array_delete($idx, $array,$separtor);
if (is_array($array)) $array = array_delete($idx, $array,$separtor);
$finalvalue = $array;
}else
{
}
//echo $i;
}
return $finalvalue;
//(is_array($array)) ? array_values($array) : null;
//array_delete($idx, $array,$separtor);
}
我收到注意:未定义抵消:0试图递归调用,将无限循环时
推荐答案
你要保持子阵列具有大多数项目?你的描述似乎这样说。
Do you want to keep the sub-arrays that have the most items? Your descriptions appear to say this.
如果是这样,像下面这样就足够了。
If so, something like the following would suffice.
// Get maximum number of items in the arrays
$max_count = max(array_map('count', $array));
// Keep only those arrays having $max_count items
$filtered = array_filter($array, function ($a) use ($max_count) {
return count($a) === $max_count;
});
旁白:如果你需要过滤的阵列有它从零开始的键,通话 array_values()
请参阅一个例子在线运行。
这篇关于从数组PHP删除值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文