<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Leandro Vieira Pinho´s Blog &#187; array</title>
	<atom:link href="http://leandrovieira.com/archive/tag/array/feed" rel="self" type="application/rss+xml" />
	<link>http://leandrovieira.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sat, 20 Aug 2011 19:22:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Função PHP para remover &#8220;keys&#8221; de um Array</title>
		<link>http://leandrovieira.com/archive/funcao-php-para-remover-keys-de-um-array</link>
		<comments>http://leandrovieira.com/archive/funcao-php-para-remover-keys-de-um-array#comments</comments>
		<pubDate>Sat, 26 Jan 2008 18:29:12 +0000</pubDate>
		<dc:creator>Leandro Vieira</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[unset]]></category>

		<guid isPermaLink="false">http://leandrovieira.com/archive/funcao-php-para-remover-keys-de-um-array</guid>
		<description><![CDATA[Acabei de criar um função em PHP para remover &#8220;keys&#8221; de um Array. Considere o array abaixo: $array = array ( 'nome' =&#62; 'Leandro', 'sobrenome' =&#62; 'Vieira Pinho', 'email' =&#62; 'spam@efoda.com' ); E considere também que você deseja excluir a &#8230; <a href="http://leandrovieira.com/archive/funcao-php-para-remover-keys-de-um-array">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Acabei de criar um <strong>função em PHP para remover &#8220;keys&#8221; de um Array</strong>.</p>
<p>Considere o array abaixo:</p>
<pre>$array = array (

'nome' =&gt; 'Leandro',

'sobrenome' =&gt; 'Vieira Pinho',

'email' =&gt; 'spam@efoda.com'

);</pre>
<p>E considere também que você deseja excluir a key &#8220;email&#8221;, por exemplo. Portanto, o procedimento seria o seguinte:</p>
<pre>unset( $array['email'] );</pre>
<p>Logo, a estrutura do Array se modificaria com a ação executada. Resultando em:</p>
<pre>$array = array (

'nome' =&gt; 'Leandro',

'sobrenome' =&gt; 'Vieira Pinho'

);</pre>
<p>Quando houver necessidade de remover várias &#8220;keys&#8221; de um Array, utilizaremos várias vezes a função <a href="http://php.net/unset">unset</a> como exemplificado acima. Para simplificar meu trabalho, criei uma função intitulada como <strong>remove_key_from_array</strong>. Nela utilizo a função <a href="http://php.net/unset">unset</a> quantas vezes for necessário para remover as &#8220;keys&#8221; desejadas do um array, de acordo com os parâmetros passado à função.</p>
<p>Exemplo de uso da função considerando o array demonstrado acima.</p>
<p>print_r( remove_key_from_array( $array, array(&#8216;sobrenome&#8217;,'email&#8217;) ) );</p>
<p>O resultado seria:</p>
<p>Array ( [nome] =&gt; Leandro )</p>
<p>Abaixo o código da função.</p>
<pre>/**
* This function remove specified key from a given array
*
* @version 0.1
* @author Leandro Vieira Pinho
* @date Saturday, January 26, 2008
* @param array $array Array that contains the keys to be removed
* @param mixed $keys String with the key name; or, an Array with the keys names
* @return array Return the given array without the specified key(s)
*/
function remove_key_from_array( $array, $keys )
{
if ( is_array( $keys ) )
foreach( $keys as $key )
unset( $array[$key] );
else
unset( $array[$keys] );
return $array;
}</pre>
<p><strong>[UPDATE]</strong>Nova versão. Obrigado Fernando pela sugestão. <strong>[/UPDATE]</strong></p>
<pre>/**
 * This function remove specified key from a given Array and reindex it, if wanted.
 *
 * @version 0.2
 * @author Leandro Vieira Pinho
 * @date Monday, January 28, 2008
 * @param array $array Given Array that contains the keys to be removed
 * @param array $keys Array with the keys names
 * @param boolean $reindex Use "true" if you want to reindex the given array or convert associative array in indexed; "false" is default.
 * @return array Return the given array without the specified key(s)
 */
function remove_key_from_array( $array, $keys, $reindex = false )
{
	$keys = (array) $keys;
	foreach( $keys as $key )
		unset( $array[$key] );
	return ( $reindex ) ? $array = array_values( $array ) : $array;
}</pre>
<p>Se lhe for útil, faço bom proveito.</p>
]]></content:encoded>
			<wfw:commentRss>http://leandrovieira.com/archive/funcao-php-para-remover-keys-de-um-array/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

