How do you delete the values foreach on php?



  • There is a multi-dimensional mass, emitted through foreach

     foreach ($arr as $value) {
         echo "<td>Значение: $value</td>\n";
     }
    

    As you understand, the result is presented in a table:

    Bulka Lozki Perez Ucrop
    Bulka Vilka Perez Ucrop
    Chléb Noj Perez Ucroup
    Pyroz Bulka Stoper Perez Ucroop
    

    Couldn't we show the masses foreach that have a meaning, like the Chleb?



  • Well, I'll offer you my answer because it's not very convenient to check if the whole block is full. foreach It's not very beautiful, because after that, there can be any other actions.

    $unexcepted = array(
       'Хлеб'
    );
    

    foreach($arr as $_value) {
    if(in_array($_value,$unexpected)) {
    continue; // если нашли повторяем цикл
    }
    echo "<td>Значение: $_value</td>\n";
    // тут ещё какая то логика представления
    }

    It is also possible to use array_map before foreach to clean the mass from unnecessary values

    $arr = array_map(function($element) use $unexpected {
    if(in_array($element,$unexpected) {
    unset($element);
    }
    },$arr);


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2