<?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>Codigo Manso &#187; lista</title>
	<atom:link href="http://www.codigomanso.com/es/tag/lista/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codigomanso.com</link>
	<description>Programación, informática y tecnología</description>
	<lastBuildDate>Sun, 21 Aug 2011 10:54:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Como saber si tienes un diccionario o una lista en PHP</title>
		<link>http://www.codigomanso.com/es/2008/11/como-saber-si-tienes-un-diccionario-o-una-lista-en-php/</link>
		<comments>http://www.codigomanso.com/es/2008/11/como-saber-si-tienes-un-diccionario-o-una-lista-en-php/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 13:09:09 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[array asociativo]]></category>
		<category><![CDATA[lista]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/?p=127</guid>
		<description><![CDATA[A veces, en casos muy concretos, es interesante saber si un array es asociativo o es una mera lista de elementos.
La diferencia entre tener un array asociativo (o mapa/diccionario/&#8230;), y una mera lista de elementos, es que no hay claves para acceder a los valores.
Básicamente, la diferencia expresada en código sería:

&#60;?php
$asociativo = array &#40;&#34;key1&#34; =&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>A veces, en casos muy concretos, es interesante saber si un array es asociativo o es una mera lista de elementos.</p>
<p>La diferencia entre tener un array asociativo (o mapa/diccionario/&#8230;), y una mera lista de elementos, es que no hay claves para acceder a los valores.</p>
<p>Básicamente, la diferencia expresada en código sería:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$asociativo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;key1&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;value1&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;key2&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;value2&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;keyN&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;valueN&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$lista</span>      <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value1&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;value2&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;valueN&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Aunque uno pueda pensar que es sencillo saber si un array es un diccionario o es una lista, no existe ninguna función en PHP (al menos no la he encontrado), que te de esta información. Y programarlo no es demasiado complicado, pero tampoco se hace en 2 minutos.</p>
<p>A continuación, para el que le interese, pongo las funciones <strong>is_list</strong> y <strong>is_dict</strong> que básicamente nos dicen si un array es una lista o un diccionario (con sólo una llamada podremos diferenciarlos):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> is_list <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$keys</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_keys</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$keys</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> is_dict <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">is_array</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>is_list <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Y como ejemplo, tras ejecutar el siguiente código:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;asociativo: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span>is_dict <span style="color: #009900;">&#40;</span><span style="color: #000088;">$asociativo</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;DICCIONARIO!&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;No diccionario...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;lista: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span>is_dict <span style="color: #009900;">&#40;</span><span style="color: #000088;">$lista</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;DICCIONARIO!&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;No diccionario...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;asociativo: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span>is_list <span style="color: #009900;">&#40;</span><span style="color: #000088;">$asociativo</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;LISTA!&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;No lista...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;lista: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span>is_list <span style="color: #009900;">&#40;</span><span style="color: #000088;">$lista</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;LISTA!&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;No lista...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Se obtiene como salida:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">asociativo: DICCIONARIO!
lista: No diccionario...
&nbsp;
asociativo: No lista...
lista: LISTA!</pre></div></div>

<p>Que es justo lo que queremos <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Y aquí, <a href="http://www.codigomanso.com/archives/phpscripts/list_or_dict.php.txt" target="_blank">el enlace con el ejemplo y las funciones para validar en PHP si un array es una lista o un diccionario</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/es/2008/11/como-saber-si-tienes-un-diccionario-o-una-lista-en-php/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

