<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Remove an Item From an Array By Value</title>
	<atom:link href="http://dev-tips.com/featured/remove-an-item-from-an-array-by-value/feed" rel="self" type="application/rss+xml" />
	<link>http://dev-tips.com/featured/remove-an-item-from-an-array-by-value</link>
	<description>Become a Better Developer, One Tip at a Time.</description>
	<lastBuildDate>Wed, 04 Aug 2010 15:39:50 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
	<item>
		<title>By: Gherald</title>
		<link>http://dev-tips.com/featured/remove-an-item-from-an-array-by-value#comment-1691</link>
		<dc:creator>Gherald</dc:creator>
		<pubDate>Wed, 03 Mar 2010 01:37:02 +0000</pubDate>
		<guid isPermaLink="false">http://dev-tips.com/?p=436#comment-1691</guid>
		<description>This also fails if &#039;value&#039; is the zeroeth entry in $array.

I.e.

$array = array(&#039;value&#039;, &#039;someothervalue&#039;);

Will not be filtered to array(&#039;someothervalue&#039;), because zero evaluates as false.</description>
		<content:encoded><![CDATA[<p>This also fails if &#8216;value&#8217; is the zeroeth entry in $array.</p>
<p>I.e.</p>
<p>$array = array(&#8216;value&#8217;, &#8216;someothervalue&#8217;);</p>
<p>Will not be filtered to array(&#8216;someothervalue&#8217;), because zero evaluates as false.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Danish Jamil</title>
		<link>http://dev-tips.com/featured/remove-an-item-from-an-array-by-value#comment-1555</link>
		<dc:creator>Danish Jamil</dc:creator>
		<pubDate>Sun, 17 Jan 2010 12:15:22 +0000</pubDate>
		<guid isPermaLink="false">http://dev-tips.com/?p=436#comment-1555</guid>
		<description>function removeArrayKey($array,$toSearch,$innerSearchArrayKey){
	$arrayCount = count($array);
	if($arrayCount&gt;0) {for($i=0;$i0)?$array:false; }else{ return false; } }</description>
		<content:encoded><![CDATA[<p>function removeArrayKey($array,$toSearch,$innerSearchArrayKey){<br />
	$arrayCount = count($array);<br />
	if($arrayCount&gt;0) {for($i=0;$i0)?$array:false; }else{ return false; } }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Danish Jamil</title>
		<link>http://dev-tips.com/featured/remove-an-item-from-an-array-by-value#comment-1554</link>
		<dc:creator>Danish Jamil</dc:creator>
		<pubDate>Sun, 17 Jan 2010 12:09:28 +0000</pubDate>
		<guid isPermaLink="false">http://dev-tips.com/?p=436#comment-1554</guid>
		<description>&lt;code&gt;
$array = array(0=&gt;array(&#039;avatar_id&#039;=&gt;1,
						&#039;quantity&#039;=&gt;4),
		 	  1=&gt;array(&#039;avatar_id&#039;=&gt;1,
					   &#039;quantity&#039;=&gt;4),
			  2=&gt;array(&#039;avatar_id&#039;=&gt;3,
					   &#039;quantity&#039;=&gt;4));

function removeArrayKey($array,$toSearch,$innerSearchArrayKey){
	$arrayCount = count($array);
	if($arrayCount&gt;0){
		for($i=0;$i0)?$array:false;

	}else{
		return false;
	}
}&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p><code><br />
$array = array(0=&gt;array('avatar_id'=&gt;1,<br />
						'quantity'=&gt;4),<br />
		 	  1=&gt;array('avatar_id'=&gt;1,<br />
					   'quantity'=&gt;4),<br />
			  2=&gt;array('avatar_id'=&gt;3,<br />
					   'quantity'=&gt;4));</p>
<p>function removeArrayKey($array,$toSearch,$innerSearchArrayKey){<br />
	$arrayCount = count($array);<br />
	if($arrayCount&gt;0){<br />
		for($i=0;$i0)?$array:false;</p>
<p>	}else{<br />
		return false;<br />
	}<br />
}</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: simen</title>
		<link>http://dev-tips.com/featured/remove-an-item-from-an-array-by-value#comment-1526</link>
		<dc:creator>simen</dc:creator>
		<pubDate>Thu, 14 Jan 2010 04:09:19 +0000</pubDate>
		<guid isPermaLink="false">http://dev-tips.com/?p=436#comment-1526</guid>
		<description>as described on php.net on preg_grep;

$nomatch = preg_grep(&quot;/{$keyword}/i&quot;,$array,PREG_GREP_INVERT);

$nomatch returns array without $keywords.</description>
		<content:encoded><![CDATA[<p>as described on php.net on preg_grep;</p>
<p>$nomatch = preg_grep(&#8220;/{$keyword}/i&#8221;,$array,PREG_GREP_INVERT);</p>
<p>$nomatch returns array without $keywords.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sounds</title>
		<link>http://dev-tips.com/featured/remove-an-item-from-an-array-by-value#comment-1218</link>
		<dc:creator>Sounds</dc:creator>
		<pubDate>Fri, 18 Dec 2009 21:39:58 +0000</pubDate>
		<guid isPermaLink="false">http://dev-tips.com/?p=436#comment-1218</guid>
		<description>__fabrice hit the nail on the head. It doesn&#039;t even need to incur the overhead of an additional function, it&#039;s a one function call. I prefer writing it without casting. Also handles removing more than a single value at once and keeps indexes. Can reset indexes later if you want.

array_diff($orig_array, array($value_to_remove));</description>
		<content:encoded><![CDATA[<p>__fabrice hit the nail on the head. It doesn&#8217;t even need to incur the overhead of an additional function, it&#8217;s a one function call. I prefer writing it without casting. Also handles removing more than a single value at once and keeps indexes. Can reset indexes later if you want.</p>
<p>array_diff($orig_array, array($value_to_remove));</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: __fabrice</title>
		<link>http://dev-tips.com/featured/remove-an-item-from-an-array-by-value#comment-1213</link>
		<dc:creator>__fabrice</dc:creator>
		<pubDate>Thu, 17 Dec 2009 14:55:12 +0000</pubDate>
		<guid isPermaLink="false">http://dev-tips.com/?p=436#comment-1213</guid>
		<description>Hi,

And this is too simple ? :) :

&lt;code&gt;$search_array = array(&#039;gif&#039;, &#039;jpg&#039;, &#039;png&#039;);
$item = &#039;png&#039;;
$rez= removeItemFromArray($search_array,$item);

var_dump($rez);

function removeItemFromArray($list, $item) {
	return  array_diff($list, (array)$item);
}
&lt;/code&gt;

Fab</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>And this is too simple ? <img src='http://dev-tips.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  :</p>
<p><code>$search_array = array('gif', 'jpg', 'png');<br />
$item = 'png';<br />
$rez= removeItemFromArray($search_array,$item);</p>
<p>var_dump($rez);</p>
<p>function removeItemFromArray($list, $item) {<br />
	return  array_diff($list, (array)$item);<br />
}<br />
</code></p>
<p>Fab</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://dev-tips.com/featured/remove-an-item-from-an-array-by-value#comment-1094</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Fri, 20 Nov 2009 15:40:28 +0000</pubDate>
		<guid isPermaLink="false">http://dev-tips.com/?p=436#comment-1094</guid>
		<description>In PHP 5.3:

$array=array_filter($array, function($value)
{
	if($value==&quot;get rid of this&quot;)
	{
		return false;
	}
	
	return true;
});</description>
		<content:encoded><![CDATA[<p>In PHP 5.3:</p>
<p>$array=array_filter($array, function($value)<br />
{<br />
	if($value==&#8221;get rid of this&#8221;)<br />
	{<br />
		return false;<br />
	}</p>
<p>	return true;<br />
});</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chetan</title>
		<link>http://dev-tips.com/featured/remove-an-item-from-an-array-by-value#comment-929</link>
		<dc:creator>Chetan</dc:creator>
		<pubDate>Thu, 03 Sep 2009 08:55:39 +0000</pubDate>
		<guid isPermaLink="false">http://dev-tips.com/?p=436#comment-929</guid>
		<description>Good :)</description>
		<content:encoded><![CDATA[<p>Good <img src='http://dev-tips.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill Brown</title>
		<link>http://dev-tips.com/featured/remove-an-item-from-an-array-by-value#comment-866</link>
		<dc:creator>Bill Brown</dc:creator>
		<pubDate>Fri, 31 Jul 2009 13:12:18 +0000</pubDate>
		<guid isPermaLink="false">http://dev-tips.com/?p=436#comment-866</guid>
		<description>Sigh. With escaped entities this time.
&lt;code&gt;
error_reporting(E_ALL);
# remove by key:
function array_remove_key ()
{
  $args  = func_get_args();
  if (empty($args[0]) &#124;&#124; !is_array($args[0])) return false;
  return array_diff_key($args[0],array_flip(array_slice($args,1)));
}
# remove by value:
function array_remove_value ()
{
  $args = func_get_args();
  if (empty($args[0]) &#124;&#124; !is_array($args[0])) return false;
  return array_diff($args[0],array_slice($args,1));
}
$myArray = array(&#039;item1&#039;, &#039;item2&#039;, &#039;item3&#039;,&#039;item1&#039;, &#039;item2&#039;, &#039;item3&#039;,&#039;...&#039;);
$myArray = array_remove_value($myArray,&#039;item2&#039;);
echo &#039;&lt;pre&gt;&#039;,print_r($myArray,TRUE),&#039;&lt;/pre&gt;&#039;;
$myArray = array(&#039;item1&#039;, &#039;item2&#039;, &#039;item3&#039;,&#039;item1&#039;, &#039;item2&#039;, &#039;item3&#039;,&#039;...&#039;);
$myArray = array_remove_value($myArray,&#039;item1&#039;,&#039;item3&#039;);
echo &#039;&lt;pre&gt;&#039;,print_r($myArray,TRUE),&#039;&lt;/pre&gt;&#039;;
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Sigh. With escaped entities this time.<br />
<code><br />
error_reporting(E_ALL);<br />
# remove by key:<br />
function array_remove_key ()<br />
{<br />
  $args  = func_get_args();<br />
  if (empty($args[0]) || !is_array($args[0])) return false;<br />
  return array_diff_key($args[0],array_flip(array_slice($args,1)));<br />
}<br />
# remove by value:<br />
function array_remove_value ()<br />
{<br />
  $args = func_get_args();<br />
  if (empty($args[0]) || !is_array($args[0])) return false;<br />
  return array_diff($args[0],array_slice($args,1));<br />
}<br />
$myArray = array('item1', 'item2', 'item3','item1', 'item2', 'item3','...');<br />
$myArray = array_remove_value($myArray,'item2');<br />
echo '&lt;pre&gt;',print_r($myArray,TRUE),'&lt;/pre&gt;';<br />
$myArray = array('item1', 'item2', 'item3','item1', 'item2', 'item3','...');<br />
$myArray = array_remove_value($myArray,'item1','item3');<br />
echo '&lt;pre&gt;',print_r($myArray,TRUE),'&lt;/pre&gt;';<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill Brown</title>
		<link>http://dev-tips.com/featured/remove-an-item-from-an-array-by-value#comment-865</link>
		<dc:creator>Bill Brown</dc:creator>
		<pubDate>Fri, 31 Jul 2009 13:10:25 +0000</pubDate>
		<guid isPermaLink="false">http://dev-tips.com/?p=436#comment-865</guid>
		<description>What about something like this:
&lt;code&gt;
error_reporting(E_ALL);
# remove by key:
function array_remove_key ()
{
  $args  = func_get_args();
  if (empty($args[0]) &#124;&#124; !is_array($args[0])) return false;
  return array_diff_key($args[0],array_flip(array_slice($args,1)));
}
# remove by value:
function array_remove_value ()
{
  $args = func_get_args();
  if (empty($args[0]) &#124;&#124; !is_array($args[0])) return false;
  return array_diff($args[0],array_slice($args,1));
}
$myArray = array(&#039;item1&#039;, &#039;item2&#039;, &#039;item3&#039;,&#039;item1&#039;, &#039;item2&#039;, &#039;item3&#039;,&#039;...&#039;);
$myArray = array_remove_value($myArray,&#039;item2&#039;);
echo &#039;&#039;,print_r($myArray,TRUE),&#039;&#039;;
$myArray = array(&#039;item1&#039;, &#039;item2&#039;, &#039;item3&#039;,&#039;item1&#039;, &#039;item2&#039;, &#039;item3&#039;,&#039;...&#039;);
$myArray = array_remove_value($myArray,&#039;item1&#039;,&#039;item3&#039;);
echo &#039;&#039;,print_r($myArray,TRUE),&#039;&#039;;
&lt;code&gt;

And incidentally, there&#039;s an error on Line 3 of the second block of code in the article. Emptyempty isn&#039;t a function.</description>
		<content:encoded><![CDATA[<p>What about something like this:<br />
<code><br />
error_reporting(E_ALL);<br />
# remove by key:<br />
function array_remove_key ()<br />
{<br />
  $args  = func_get_args();<br />
  if (empty($args[0]) || !is_array($args[0])) return false;<br />
  return array_diff_key($args[0],array_flip(array_slice($args,1)));<br />
}<br />
# remove by value:<br />
function array_remove_value ()<br />
{<br />
  $args = func_get_args();<br />
  if (empty($args[0]) || !is_array($args[0])) return false;<br />
  return array_diff($args[0],array_slice($args,1));<br />
}<br />
$myArray = array('item1', 'item2', 'item3','item1', 'item2', 'item3','...');<br />
$myArray = array_remove_value($myArray,'item2');<br />
echo '',print_r($myArray,TRUE),'';<br />
$myArray = array('item1', 'item2', 'item3','item1', 'item2', 'item3','...');<br />
$myArray = array_remove_value($myArray,'item1','item3');<br />
echo '',print_r($myArray,TRUE),'';<br />
</code><code></p>
<p>And incidentally, there's an error on Line 3 of the second block of code in the article. Emptyempty isn't a function.</code></p>
]]></content:encoded>
	</item>
</channel>
</rss>
