使用特定数组元素处理数组值

时间:2014-08-01 04:51:57

标签: php arrays sorting arraylist multidimensional-array

我有这样的数组值。我尝试过array_search,没用。 我想要的只是过滤具有状态值的数组值。

输出

    Array
    (
    [1] => Array
    (
    [author] => Author1
    [book] => Book1
    [status] => 1
    )

    [2] => Array
    (
    [author] => Author2
    [book] => Book2
    )

    [3] => Array
    (
    [author] => Author3
    [book] => Book3
    [status] => 1
    )
    )

预期输出

Array
    (
    [1] => Array
    (
    [author] => Author1
    [book] => Book1
    [status] => 1
    )

    [3] => Array
    (
    [author] => Author3
    [book] => Book3
    [status] => 1
    )
    )

如果预期输出具有正确的数字序列,我会更高兴。在上面的例子中,有两个数组的数组为[1]和[3]。如果可能,我需要将其设为[1]和[2]。

任何帮助都非常有用。

谢谢, Kimz

2 个答案:

答案 0 :(得分:1)

你可以这样做

foreach($my_array as $arr) {
    if(isset($arr['status']) && $arr['status'] != '') {
        $temp_array[] = $arr;
    }
}
print_r($temp_array);

答案 1 :(得分:1)

您可以使用array_filter

$array = array(['status' => 1], [], ['status' => 1]);

$result = array_filter($array, function($item)
    {
    return !empty($item['status']);
    });

var_dump($result);