How to filter associative array by values and split into two variables?

我只是一个虾纸丫 提交于 2019-12-11 10:34:22

问题


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];
    }
}

回答1:


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



回答2:


If you do not like the if-else statements and prefer a more generic way of sorting by status, e.g if status is suddenly 2

$input = array(
    array("id" => 1, "status" => 1),
    array("id" => 2, "status" => 1),
    array("id" => 3, "status" => 0),
    array("id" => 4, "status" => 2),
    array("id" => 5, "status" => 0)
);

$arr = array();
foreach($input as $key => $item){
   $arr[$item['status']][] = $item;
}

ksort($arr, SORT_NUMERIC);

$arr would now be a multidimensional array sorted by status.

<?php list($status_zero, $status_one, $status_two) = $arr ?>

To assign the individual arrays to their own variable.




回答3:


hope this helps...i think simple way is always the best...

function array_group(Array $array, $groupBy){
    $grouped_array = [];
    for ($i=0; $i<count($array); $i++){
        $key_name = $groupBy.'_'.$array[$i][$groupBy];
        if (!array_key_exists($key_name, $grouped_array)){
            $grouped_array[$key_name] = [];
        }
        $grouped_array[$key_name][] = $array[$i];
    }
    return $grouped_array;
}

test :

$test_array = [["id" => 12, "status" => 0], ["id" => 13, "status" => 1], ["id" => 14, "status" => 1]];

print_r(array_group($test_array, 'status'));

output:

Array
(
    [status_0] => Array
        (
            [0] => Array
                (
                    [id] => 12
                    [status] => 0
                )

        )

    [status_1] => Array
        (
            [0] => Array
                (
                    [id] => 13
                    [status] => 1
                )

            [1] => Array
                (
                    [id] => 14
                    [status] => 1
                )

        )

)


来源:https://stackoverflow.com/questions/33904365/how-to-filter-associative-array-by-values-and-split-into-two-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!