Delete some array results

Asked

Viewed 36 times

0

I need to leave in the array below only the values Yes: 1 and 2 Any suggestions? I tried unset() but it didn’t work.

array (size=3)
  0 => 
    array (size=4)
      0 => string 'Adair' (length=26)
      1 => string '498' (length=3)
      2 => string 'Nao' (length=3)
      3 => string '' (length=0)
  1 => 
    array (size=4)
      0 => string 'Pedro' (length=31)
      1 => string '189' (length=3)
      2 => string 'Sim' (length=3)
      3 => string '' (length=0)
  2 => 
    array (size=4)
      0 => string 'Celso' (length=11)
      1 => string '651' (length=3)
      2 => string 'Sim' (length=3)
      3 => string 'E-mail: sdsd' (length=12)
  • I got it with this post. http://stackoverflow.com/questions/38965541/filter-multidimensional-array-by-value-in-arra Thank you all.

1 answer

0

Use the function array_filter with an anonymous function to return items containing the desired value.

Examples

If the value "Yes" is always in position 2 of the sub-line:

$novoArray = array_filter($seuArray, function($arr) {
    return $arr[2] == 'Sim';
})

If you are in any position:

$novoArray = array_filter($seuArray, function($arr) {
    return in_array('Sim', $arr);
});
  • Thank you for your reply, but it did not happen. $new_values = array_filter($values, Function($value) { Return $value == 'Yes'; }); $new_values became empty..

  • Just take care that $valor, in this case, it is a array and not a string. It is necessary to compare a value of this array, then the right thing is to do return $valor[2] == 'Sim'.

  • Fixed and included another alternative. Working.

Browser other questions tagged

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