There is a multidimensional array, the output values ​​through foreach

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

As you already understand, the result is displayed in the form of a table:

 Pie Bread Spoons Pepper Dill
 Pie Bread Fork Pepper Dill
 Patty Bread Knife Pepper Dill
 Pie Bun Corkscrew Pepper Dill 

Perhaps not pokasyvat arrays in foreach, in which there is a value, such as bread?

  • 2
    before the new line we write if (!in_array('Хлеб', $array)) {... and everything ..... php.net/manual/ru/function.in-array.php - Alexey Shimansky
  • @Dvashington $ value is it your array or string? You just have one conclusion, and the code is different. - Firepro

2 answers 2

In general, I will offer my answer, since it is not very convenient to wrap the entire foreach block into the if if it is not very nice, since some other actions can be recorded later.

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

You can also use array_map before foreach to clear an array of unnecessary values.

 $arr = array_map(function($element) use $unexpected { if(in_array($element,$unexpected) { unset($element); } },$arr); 
  • Apparently, I am doing something wrong, but my data still displays everything. - Dvashington
  • one
    Var_dump ($ _ value) make at the beginning of the cycle. - Naumov
  • array (5) {[0] => string (2) "28" [1] => string (1) "t" [2] => string (26) "chown -R user ./foldername" [3] => string (69) "Change of ownership for a folder with recursion" [4] => string (38) "user - username"} My code is $ un = array ('t'); foreach ($ get_csv as $ _value) {echo '<tr>'; if (in_array ($ _ value, $ un)) {continue; } echo '<pre>'; var_dump ($ _ value); echo '</ pre>'; echo "<td> $ _ value [0] </ td> \ n"; echo "<td> $ _ value [1] </ td> \ n"; echo "<td> $ _ value [2] </ td> \ n"; echo "<td> $ _ value [3] </ td> \ n"; echo "<td> $ _ value [4] </ td> </ tr>"; } - Dvashington
  • @Dvashington and what have the "Bread" to compare with what you need - Naumov
  • A simple example was provided, the values ​​were different, the arrays were larger. Actually, I removed the array from the exception and left only the string value - everything turned out. - Dvashington

create an array in which a list of values ​​that should not be displayed and check the function in_array

$ arr - array with exceptions

 foreach ($arr as $value) { if(!in_array($value, $arr)){ echo "<td>Значение: $value</td>\n"; } } 
  • 2
    What is the catch of your condition? - Visman
  • one
    @Visman I think this is still a human typo - Naumov