从基于值的multidimensional array中删除元素

我想基于一个值从multidimensional array中删除元素。 在这种情况下,如果一个子数组的键“year”的值为2011,那么我想要这个子数组。

只是为了logging:我正在运行PHP 5.2。

我的数组看起来像这样:

Array ( [0] => Array ( [filmId] => 61359 [url] => http://www.moviemeter.nl/film/61359 [title] => Unstoppable [alternative_title] => [year] => 2011 [thumbnail] => http://www.moviemeter.nlhttp://img.dovov.comcovers/thumbs/61000/61359.jpg [average] => 0 [votes_count] => 0 [similarity] => 100.00 [directors_text] => geregisseerd door Richard Harrison [actors_text] => met Chen Shilony, Ruben Crow en David Powell [genres_text] => Drama / Komedie [duration] => 90 ) [1] => Array ( [filmId] => 87923 [url] => http://www.moviemeter.nl/film/87923 [title] => Unstoppable [alternative_title] => [year] => 2011 [thumbnail] => http://www.moviemeter.nlhttp://img.dovov.comcovers/thumbs/87000/87923.jpg [average] => 0 [votes_count] => 0 [similarity] => 100.00 [directors_text] => geregisseerd door Example Director [actors_text] => met Actor 1, Actor 2 en Actor 3 [genres_text] => Drama / Komedie [duration] => 90 ) [2] => Array ( [filmId] => 68593 [url] => http://www.moviemeter.nl/film/68593 [title] => Unstoppable [alternative_title] => [year] => 2010 [thumbnail] => http://www.moviemeter.nlhttp://img.dovov.comcovers/thumbs/68000/68593.jpg [average] => 3.3 [votes_count] => 191 [similarity] => 100.00 [directors_text] => geregisseerd door Tony Scott [actors_text] => met Denzel Washington, Chris Pine en Rosario Dawson [genres_text] => Actie / Thriller [duration] => 98 ) [3] => Array ( [filmId] => 17931 [url] => http://www.moviemeter.nl/film/17931 [title] => Unstoppable [alternative_title] => Nine Lives [year] => 2004 [thumbnail] => http://www.moviemeter.nlhttp://img.dovov.comcovers/thumbs/17000/17931.jpg [average] => 2.64 [votes_count] => 237 [similarity] => 100.00 [directors_text] => geregisseerd door David Carson [actors_text] => met Wesley Snipes, Jacqueline Obradors en Mark Sheppard [genres_text] => Actie / Thriller [duration] => 96 ) ) 

尝试这个:

 function removeElementWithValue($array, $key, $value){ foreach($array as $subKey => $subArray){ if($subArray[$key] == $value){ unset($array[$subKey]); } } return $array; } 

那么你会这样称呼它:

 $array = removeElementWithValue($array, "year", 2011); 

尝试这个:

 function remove_element_by_value($arr, $val) { $return = array(); foreach($arr as $k => $v) { if(is_array($v)) { $return[$k] = remove_element_by_value($v, $val); //recursion continue; } if($v == $val) continue; $return[$k] = $v; } return $return; } 
 $array[] = array('year' => 2010, "genres_text" => "Drama / Komedie"); $array[] = array('year' => 2011, "genres_text" => "Actie / Thriller"); $array[] = array('year' => "2010", "genres_text" => "Drama / Komedie"); $array[] = array('year' => 2011, "genres_text" => "Romance"); print_r(remove_elm($array, "year", 2010)); // removes the first sub-array only print_r(remove_elm($array, "year", 201)); // will not remove anything print_r(remove_elm($array, "genres_text", "drama", TRUE)); // removes all Drama print_r(remove_elm($array, "year", 2011, TRUE)); // removes all 2011 function remove_elm($arr, $key, $val, $within = FALSE) { foreach ($arr as $i => $array) if ($within && stripos($array[$key], $val) !== FALSE && (gettype($val) === gettype($array[$key]))) unset($arr[$i]); elseif ($array[$key] === $val) unset($arr[$i]); return array_values($arr); } 

对于一个单一的已知值,在迭代开始时通过multidimensional array:

 foreach ( $array as $subarray ) { //beginning of the loop where you do things with your array if ( $subarray->$key == '$valueToRemoveArrayBy' ) continue; //iterate your stuff } 

如果您的条件是真的,只需跳过整个迭代。

或者你也可以做相反的事情。 根据口味可能会更容易阅读:

 foreach ( $array as $subarray ) { if ( $subarray->$key != $valueToRemoveArrayBy ) { //do stuff } } 

我不知道。 也许这对一些看起来很不好。 不过,我喜欢它。 简短,快速和简单。

看起来在这种情况下过滤的目的是打印出一些内容,并跳过一些,根据一定的标准。 如果在循环之前执行过滤,则必须循环整个事件两次 – 一次过滤,一次打印内容。

如果你这样做,在循环内部,这不是必需的。 除了循环内部外,你也不会改变你的数组,如果你不总是想特别按照这些标准进行过滤,这可能会有所帮助。

你应该试试这个方法,
$ mainArray是你当前的数据
$ subArray是你想要删除的数据

  foreach ($mainArray as $key => $mainData){ foreach ($subArray as $subData){ if($mainData['dataId'] == $subData['dataId']){ unset($mainArray[$key]); break; } } } var_dump(array_values($mainArray)); 

这会给你输出你想要的数组的新索引。