0

I have an array say :

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

                )

        )

)

Then I calculate length as below : $length = count($this->view->feedback); $i= 0;

Now I am running a foreach loop as

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

The above array_slice() method outputs as below :

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

        )

)

My DESIRED/REQUIRED output is :

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

                )

        )
)

How can I achieve this ? Is something wrong with my array_slice method ?

1 Answer 1

1

What about this :

$my_desired_output = [];
foreach ($input as $key => $operation)
{
    if($operation['type'] == 'error')
    {
        $my_desired_output[$key] = $operation;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.