如何通过价值观和分裂关联数组过滤成两个变量? [英] How to filter associative array by values and split into two variables?
问题描述
我想筛选关联数组,这样一个特定的键中的某些值将进入一个特定的变量。为了使这个意义上说,这里是什么,我试图做一个例子:
输入(从DB):
阵列
(
[0] =>排列
(
[ID] => '12',
[现状] => '0'
)
[1] =>排列
(
[ID] => '13',
[现状] => '1'
)
[2] =>排列
(
[ID] => '14',
[现状] => '1'
)
)
输出(在PHP):
$ status_one =
排列
(
[0] =>排列
(
[ID] => '13',
[现状] => '1'
)
[1] =>排列
(
[ID] => '14',
[现状] => '1'
)
);$ status_zero =
排列
(
[0] =>排列
(
[ID] => '12',
[现状] => '0'
)
)
我知道,数组是没有语法正确,但是这是想法。我想基于在一个特定的键的值阵列之一变量分成两个独立的变量
下面是我有权。我知道这是部分错误。我试着array_filter的东西为好。
的foreach($作为重点=&GT $地位; $行){
如果($行['状态'] =='1')
{
$ status_one [] = $键[$行]
}
如果($行['状态'] =='2')
{
$ status_two [] = $键[$行]
}
}
您已经接近..
$ status_one =阵列();
$ status_zero =阵列();的foreach($作为重点=&GT $地位; $行){
如果($行['状态'] =='1')$ status_one [$关键] = $行;
否则status_zero $ [$关键] = $行;
}后续代码var_dump($ status_one,$ status_zero);
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);
这篇关于如何通过价值观和分裂关联数组过滤成两个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!