0

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
        )
)

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

1
  • I don't understand the downvote. It's a perfectly valid programming question and conditional grouping of array values doesn't always seem straight-forward. Commented Dec 18, 2013 at 5:56

3 Answers 3

1

A simple and naive solution might look something like this:

var original = /* your original array, the one you posted */;
var result = [];
var tmp = [];
$.each(original, function(idx, obj) {
    tmp.push(obj.question_id);
    if(obj.is_break == 1) {
        result.push(tmp);
        tmp = [];
    }
});
console.log(result); // returns array of array

HTH

EDIT: In PHP, it may look something like this (I am not too well-versed in PHP):

var $original = /* your original array, the one you posted */;
var $result = [];
var $tmp = [];
foreach($original as $obj) {
    $tmp.push($obj.question_id); /* or could be $obj['question_id'] */
    if($obj.is_break == 1) { /* or could be $obj['is_break'] */
        $result.push($tmp);
        $tmp = [];
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

You must then specify the language. Anyways, the logic that I presented applies to arrays in any language including PHP.
0

Here's solution

$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);

Comments

0

Try this:

<?php

$inputArr = Array
(
Array("question_id" => 13, "is_break" => 0),
 Array("question_id" => 14, "is_break" => 1),
 Array("question_id" => 15, "is_break" => 0),
 Array("question_id" => 16, "is_break" => 1),
 Array("question_id" => 17, "is_break" => 1)
);

$result = array();
$tmp = array();

foreach ($inputArr as $obj) {
    //$tmp . push($obj . question_id); /* or could be $obj['question_id'] */

    array_push($tmp, $obj['question_id']);

    if ($obj['is_break'] == 1) { /* or could be $obj['is_break'] */
        //$result . push($tmp);
        array_push($result, $tmp);
        $tmp = array();
    }
}

var_dump($result);
?>
  • Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.