本文介绍了PHP基于值的拆分数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复: 如何基于特定值拆分数组?/a>
Possible Duplicate: How to split an array based on a certain value?
这是我要拆分的示例数组:
Here is an example array I want to split:
(0,1,2,3,0,4,5)
(0, 1 ,2 ,3, 0, 4, 5)
如何将其分成2个数组?
How do I split it in 2 array like this?
(0,1,2,3) (0,4,5)
(0, 1 ,2 ,3) (0, 4, 5)
基本上,我想检查元素的值是否为零,然后创建所有元素的数组,直到找到另一个零为止.我似乎无法提出解决方案.
Basically I want to check if the value of an element is zero and then create an array of all the elements until I find another zero. I can't seem to come up with a solution to this.
推荐答案$array = array(0, 1 ,2 ,3, 0, 4, 5); $result = array(); $index = -1; foreach ($array as $number) { if ($number == 0) { $index++; } $result[$index][] = $number; } echo print_r($result, true);
您将以以下内容结束.
You'll end with the following.
array( 0 => array(0, 1, 2, 3), 1 => array(0, 4, 5) )更多推荐
PHP基于值的拆分数组
发布评论