从mysql拆分数组php

时间:2013-12-18 06:45:08

标签: php arrays split

我有阵列

Array
(
    [0] => Array
        (
            [question_id] => 13
            [is_break] => 0
        )
    [1] => Array
        (
            [question_id] => 14
            [is_break] => 1
        )
    [2] => Array
        (
            [question_id] => 15
            [is_break] => 0
        )
    [3] => Array
        (
            [question_id] => 16
            [is_break] => 1
        )
    [4] => Array
        (
            [question_id] => 17
            [is_break] => 1
        )
)

如何按is_break = 1拆分,所以我有question_id (13,14)(15,16)(17) 所以我有3个数组,数组[0]包含question_id 13和14数组[1]包含question_id 15和16,数组[2]包含question_id 17

3 个答案:

答案 0 :(得分:2)

试试这个......

$count = 0;
$result = array();
foreach($array as $a) {
    $result[$count][] = $a['question_id'];
    if($a['is_break']) {
        $count++;
    }
}

请参阅 Codepad

答案 1 :(得分:1)

做这样的事情.. [ FYI 这适用于PHP> = 5.5 ]

$new_array = array_chunk(array_column($your_array,'question_id'),2);
var_dump($new_array);

答案 2 :(得分:1)

$questions = array();

foreach ($main_array as $subarray) {
   if($subarray['is_break'] == 1) {
       array_push($questions, $subarray['question_id']);
   }
}

现在$questions数组有问题ID。