I am trying to filter an associative array so that certain values within a specific key will go into a specific variable. To make sense of this, here is an example of what I'm trying to do:
Input (from DB):
Array
(
[0] => Array
(
[id] => '12',
[status] => '0'
)
[1] => Array
(
[id] => '13',
[status] => '1'
)
[2] => Array
(
[id] => '14',
[status] => '1'
)
)
Output (in PHP):
$status_one =
Array
(
[0] => Array
(
[id] => '13',
[status] => '1'
)
[1] => Array
(
[id] => '14',
[status] => '1'
)
);
$status_zero =
Array
(
[0] => Array
(
[id] => '12',
[status] => '0'
)
)
I know that the arrays are not syntactically correct, but that's the idea. I want to split one variable of arrays into two separate variables based on the value in a specific key.
Here's what I have right. I know it's partly wrong. I've tried something with array_filter as well.
foreach ($status as $key => $row) {
if($row['status'] == '1')
{
$status_one[] = $key[$row];
}
if($row['status'] == '2')
{
$status_two[] = $key[$row];
}
}
You're close..
$status_one = array();
$status_zero = array();
foreach ($status as $key => $row) {
if($row['status'] == '1') $status_one[$key] = $row;
else $status_zero[$key] = $row;
}
var_dump($status_one, $status_zero);