Reputation: 45
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
Upvotes: 1
Views: 101
Reputation: 4858
Try this...
$count = 0;
$result = array();
foreach($array as $a) {
$result[$count][] = $a['question_id'];
if($a['is_break']) {
$count++;
}
}
See Codepad.
Upvotes: 2
Reputation: 3647
$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.
Upvotes: 1
Reputation: 68476
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);
Upvotes: 1