How to remove an array from occurrence found within a sub array!

Asked

Viewed 60 times

0

Well, I have the following situation, I would need to check if the term exists (name of the fruits) if to delete the array that is matching the values (name of the fruit and code)

Example: When searching for the term pineapple do this

[0] => Array
    (
        [0] => maça
        [1] => 1256
    )

[1] => Array
    (
        [0] => abacaxi
        [1] => 1234
    )

[2] => Array
    (
        [0] => pera
        [1] => 235
    )

turn this around

   [0] => Array
        (
            [0] => maça
            [1] => 1256
        )

    [1] => Array
        (
            [0] => pera
            [1] => 235
        )

I couldn’t make it work with multidimensional array! Thank you all.

1 answer

5


Supposing $frutas is the target array of the query followed by exclusion:

$frutas = [
  ['maça', 1256],
  ['abacaxi', 1234],
  ['pera', 235],
  ['banana', 1235],
  ['laranja', 2135],
  ['limão', 2315],
  ['morango', 2351]
];

One possibility is to filter the array with array_filter() and check each item with in_array() to know if one of its values is the cut string.

$frutas = array_filter($frutas, function ($item){
  return !in_array('abacaxi', $item);
});

Another possibility is to iterate with foreach for each element of the array and destroy with unset() the element has the cut string:

foreach($frutas as $key => &$value){
  if (in_array('pera', $value)) unset($frutas[$key]);
}

Test both examples in Repl.it: https://repl.it/repls/WhitesmokePunctualDebugmonitor

  • Thank you my expensive worked super well!

  • Hello, implementing in my project, I could notice that the first solution does not work with variables containing the search value, just by typing the string directly, you know the reason?

  • @Caiolourençon, you can put an example of the problem you mentioned so I can analyze it. It can be a link to a text file or an online sandbox. Because both examples use the same code as a base in_array().

  • 1

    edited, please check!

  • 1

    @Caiolourençon a variable $val_busca not declared within the scope of callback. Simply declare as global within callback global $val_busca; See example working: https://repl.it/repls/LuxuriousNeatExpertise

  • I’m going to reverse the question, because the issue of the question has turned my answer into something meaningless.

  • 1

    congratulations, working perfectly as needed.

  • If you want to add this implementation in your solution comment, you’d also like it, because I think people will use variables to search.

  • @Caiolourençon I appreciate the suggestion, but I will decline because it leaves the original focus of the question and after answered the format of the site does not allow the change of scope of the question, so I reversed it, and also does not allow issues divergent from the original question. In the case I presented the solution out of courtesy, but correct guidance would be to ask a new question and lynch this as context.

  • 1

    goes there my dear I return the courtesy, https://answall.com/questions/448152/fu%C3%A7%C3%a3o-para-encontra-e-remover-termo-no-array-n%C3%a3o-funciona-com-variav%C3%a9l

Show 5 more comments

Browser other questions tagged

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