1

i have array

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

how to split by is_break = 1, so i have question_id (13,14)(15,16)(17) so i have 3 array that array [0] consist of question_id 13 and 14 array [1] consist of question_id 15 and 16 and array [2] consist of question_id 17

3
  • Please clear the question. You mean split by value of is_break? If so then your final result is wrong. Commented Dec 18, 2013 at 6:47
  • so i have 3 array that array [0] consist of question_id 13 and 14 array [1] consist of question_id 15 and 16 and array [2] consist of question_id 17 Commented Dec 18, 2013 at 6:49
  • In my answer you will get an array of n elements whose is_break=1. array[0] contain first element, array[1] second element and so on. Commented Dec 18, 2013 at 7:06

3 Answers 3

2

Try this...

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

See Codepad.

Sign up to request clarification or add additional context in comments.

Comments

1

Do something like this.. [FYI : This works on PHP >= 5.5 ]

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

Comments

1
$questions = array();

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

Now $questions array has the question ids.

2 Comments

This will only return question_id having is_break = 1.
In question i see the question owner wants to get ids where is_break = 1

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.