How to filter a dynamic multidimensional array?

Asked

Viewed 246 times

0

I am a beginner in PHP and I am in need of a help, as I do to filter a dynamic multidimensional array by removing the arrays that do not have a specific value?

Taking for example: In my code, I make a call in the database and create a dynamic array according to this structure.

Array


(  
  [0] => Array
        (
            [0] => Array
                (
                    [nome] => Maria
                    [idade] => 22
                )

            [1] => Array
                (
                    [casas_alugadas] => 'S' 
                     [qtd] => 10
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [nome] => Joao
                    [idade] => 28
                )

            [1] => Array
                (
                    [casas_alugadas] => 'N'
                )

        )

)

I need to filter based on the values, if the array has the element "rented houses == N", it is deleted and is passed to the next one, leaving only the index array 0, as done in the example. How would you do that?

1 answer

0


You can use the function array_filter. Just pass the array that you want to filter and a function to perform the condition. In this function you can return true to the array be returned (a array valid) or false to "jump" the array and check out the next.

Example:

<?php

$arr = [
    [
        [
            "nome"  => "Maria",
            "idade" => 22
        ],
        [
            "casas_alugadas" => "S",
            "qtd" => 10
        ]
    ],
    [
        [
            "nome" => "João",
            "idade" => 28
        ],
        [
            "casas_alugadas" => "N"
        ]
    ]
];

/* Percorre todos os índices */
$arr = array_filter($arr, function($item) {

    /**
     * Verifica se o índice `casas_alugadas` existe no
     * índice 1 e se o valor é diferente de "N"
     */
    return $item[1]["casas_alugadas"] != "N";
});

var_dump($arr);

Demonstration: https://ideone.com/FhXwHO

If you don’t know the index, just use the example below:

<?php

$arr = [
    [
        [
            "nome"  => "Maria",
            "idade" => 22
        ],
        [
            "casas_alugadas" => "S",
            "qtd" => 10
        ]
    ],
    [
        [
            "nome" => "João",
            "idade" => 28
        ],
        [
            "casas_alugadas" => "N"
        ]
    ]
];


/* Filtra os dados do array */
$arr = array_filter($arr, function($item) {

    /* Percorre todos os índices */
    foreach($item as $result) {

        /**
         * Verifica se existe o índice `casas_alugadas` e se o valor dele é "S"
         * Caso a condição seja verdadeira, valida e retorna o array
         */
        if (isset($result['casas_alugadas']) && $result['casas_alugadas'] == "S") {
            return true;
        }
    }
});

var_dump($arr);

Demonstration: https://ideone.com/IyKAkO

  • Cool, that way I didn’t know, but she treats the array and changes it? I need to remove these array, without changing the structure(Remove the array and keep the main array to perform the sets in a foreach)

  • @Vic.py The structure of the array is maintained.

  • @Vic.py managed to solve?

  • I managed without problems thanks so much for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.