Skip to main content
replaced http://de2.php.net with https://www.php.net
Source Link

It's because array_search searches your array for the string 'Partners'. But as your array only contains 3 arrays, no entry is found. array_search will result false which evaluates to 0. That's why the first key is removed.

You need to search inside each array (foreach ($myarray as $array) { ... }). And you also must check the return value:

$value = array_search(...); if ($value !== false) { // do unset } 

Solution with array_filterarray_filter:

// Remove all arrays having 'title' => 'Partners' $arr = array_filter($arr, function($e) { return $e['title'] != 'Partners'; }); // Remove all arrays containing a value 'Partners' $arr = array_filter($arr, function($e) { return array_search('Partners', $e) === false; }); 

It's because array_search searches your array for the string 'Partners'. But as your array only contains 3 arrays, no entry is found. array_search will result false which evaluates to 0. That's why the first key is removed.

You need to search inside each array (foreach ($myarray as $array) { ... }). And you also must check the return value:

$value = array_search(...); if ($value !== false) { // do unset } 

Solution with array_filter:

// Remove all arrays having 'title' => 'Partners' $arr = array_filter($arr, function($e) { return $e['title'] != 'Partners'; }); // Remove all arrays containing a value 'Partners' $arr = array_filter($arr, function($e) { return array_search('Partners', $e) === false; }); 

It's because array_search searches your array for the string 'Partners'. But as your array only contains 3 arrays, no entry is found. array_search will result false which evaluates to 0. That's why the first key is removed.

You need to search inside each array (foreach ($myarray as $array) { ... }). And you also must check the return value:

$value = array_search(...); if ($value !== false) { // do unset } 

Solution with array_filter:

// Remove all arrays having 'title' => 'Partners' $arr = array_filter($arr, function($e) { return $e['title'] != 'Partners'; }); // Remove all arrays containing a value 'Partners' $arr = array_filter($arr, function($e) { return array_search('Partners', $e) === false; }); 
added 75 characters in body
Source Link
steffen
  • 17.4k
  • 4
  • 47
  • 87

It's because array_search searches your array for the string 'Partners'. But as your array only contains 3 arrays, no entry is found. array_search will result false which evaluates to 0. That's why the first key is removed.

You need to search inside each array (foreach ($myarray as $array) { ... }). And you also must check the return value:

$value = array_search(...); if ($value !== false) { // do unset } 

Maybe also interesting:Solution with array_filter.:

// Remove all arrays having 'title' => 'Partners' $arr = array_filter($arr, function($e) { return $e['title'] != 'Partners'; }); // Remove all arrays containing a value 'Partners' $arr = array_filter($arr, function($e) { return array_search('Partners', $e) === false; }); 

It's because array_search searches your array for the string 'Partners'. But as your array only contains 3 arrays, no entry is found. array_search will result false which evaluates to 0. That's why the first key is removed.

You need to search inside each array (foreach ($myarray as $array) { ... }). And you also must check the return value:

$value = array_search(...); if ($value !== false) { // do unset } 

Maybe also interesting: array_filter.

It's because array_search searches your array for the string 'Partners'. But as your array only contains 3 arrays, no entry is found. array_search will result false which evaluates to 0. That's why the first key is removed.

You need to search inside each array (foreach ($myarray as $array) { ... }). And you also must check the return value:

$value = array_search(...); if ($value !== false) { // do unset } 

Solution with array_filter:

// Remove all arrays having 'title' => 'Partners' $arr = array_filter($arr, function($e) { return $e['title'] != 'Partners'; }); // Remove all arrays containing a value 'Partners' $arr = array_filter($arr, function($e) { return array_search('Partners', $e) === false; }); 
Source Link
steffen
  • 17.4k
  • 4
  • 47
  • 87

It's because array_search searches your array for the string 'Partners'. But as your array only contains 3 arrays, no entry is found. array_search will result false which evaluates to 0. That's why the first key is removed.

You need to search inside each array (foreach ($myarray as $array) { ... }). And you also must check the return value:

$value = array_search(...); if ($value !== false) { // do unset } 

Maybe also interesting: array_filter.