本文介绍了PHP - 从查询拆分阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP和我有查询阵列

I am using PHP and I have array from query

Array
(
    [0] => stdClass Object
        (
            [question_id] => 13
            [is_break] => 0
        )

    [1] => stdClass Object
        (
            [question_id] => 14
            [is_break] => 1
        )

    [2] => stdClass Object
        (
            [question_id] => 15
            [is_break] => 0
        )
    [3] => stdClass Object
        (
            [question_id] => 16
            [is_break] => 1
        )
    [4] => stdClass Object
        (
            [question_id] => 17
            [is_break] => 1
        )
)

如何通过is_break分割(分组)= 1,所以我有question_id(13,14)(15,16)(17)

How to split (grouping) by is_break = 1 so i have question_id (13,14)(15,16)(17)

推荐答案

下面的解决方案

$data   = {your data};

$result = array();
$i      = 0;
foreach($data as $k => $item) {

    if ($item->is_break) {
        $result[] = array_slice($data, $i, $k);
    }
    $i = $k;
}

print_r($result);

这篇关于PHP - 从查询拆分阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 20:39