PHP array_slice方法只输出所需的数组

时间:2014-08-19 06:01:25

标签: php arrays slice

我有一个阵列说:

$input = Array
(
    [2] => Array
        (
            [message] => 1 file(s) do not conform to the xxx naming standards.
            [type] => warning
            [headers] => Array
                (
                    [0] => Array
                        (
                            [name] => Package
                            [property] => zip
                        )

                    [1] => Array
                        (
                            [name] => Name
                            [property] => name
                        )

                )

            [rows] => Array
                (
                    [0] => Array
                        (
                            [zip] => abcd.zip
                        )

                )

        )

    [111] => Array
        (
            [message] => Invalid it is
            [type] => error
            [headers] => Array
                (
                    [0] => Array
                        (
                            [name] => ID
                            [property] => id
                        )

                    [1] => Array
                        (
                            [name] => Title
                            [property] => title
                        )

                    [2] => Array
                        (
                            [name] => Zip
                            [property] => zip
                        )

                )

            [rows] => Array
                (
                    [0] => Array
                        (
                            [id] => abcd
                            [title] => title
                            [zip] => abcd
                        )

                )

        )

)

然后我计算长度如下:  $ length = count($ this-> view-> feedback);  $ i = 0;

现在我正在运行一个foreach循环

foreach ($input as $operation)
{
    if($operation['type'] == 'error')
    {
        $output = array_slice($operation, -$i, $length);
    }
    $i++;
}

上面的array_slice()方法输出如下:

$output = 
 Array
(
    [rows] => Array
        (
            [0] => Array
                (
                    [id] => 214501
                    [title] => SRM- 3860(Res-2)
                    [zip] => SRM-3860(Test_2).zip
                )

        )

)

我希望/需要的输出是:

$output = 
Array
(
    [111] => Array
        (
            [message] => Invalid it is
            [type] => error
            [headers] => Array
                (
                    [0] => Array
                        (
                            [name] => ID
                            [property] => id
                        )

                    [1] => Array
                        (
                            [name] => Title
                            [property] => title
                        )

                    [2] => Array
                        (
                            [name] => Zip
                            [property] => zip
                        )

                )

            [rows] => Array
                (
                    [0] => Array
                        (
                            [id] => abcd
                            [title] => title
                            [zip] => abcd
                        )

                )

        )
)

我怎样才能做到这一点? 我的array_slice方法有问题吗?

1 个答案:

答案 0 :(得分:1)

这个怎么样:

$my_desired_output = [];
foreach ($input as $key => $operation)
{
    if($operation['type'] == 'error')
    {
        $my_desired_output[$key] = $operation;
    }
}