PHP基于值的拆分数组

编程入门 行业动态 更新时间:2024-10-07 05:30:28
本文介绍了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基于值的拆分数组

本文发布于:2023-11-30 15:21:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650491.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!