<?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>Naeh.net &#187; tableau</title>
	<atom:link href="http://naeh.net/tag/tableau/feed/" rel="self" type="application/rss+xml" />
	<link>http://naeh.net</link>
	<description>Le mémo du développeur</description>
	<lastBuildDate>Wed, 30 Jun 2010 18:56:54 +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>Trier un tableau multidimensionnel</title>
		<link>http://naeh.net/trier-un-tableau-multidimensionnel/</link>
		<comments>http://naeh.net/trier-un-tableau-multidimensionnel/#comments</comments>
		<pubDate>Sat, 02 Jun 2007 21:45:43 +0000</pubDate>
		<dc:creator>Naeh</dc:creator>
				<category><![CDATA[PHP / MySQL]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[multidimensionnel]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tableau]]></category>
		<category><![CDATA[tri]]></category>

		<guid isPermaLink="false">http://wp.naeh.info/?p=13</guid>
		<description><![CDATA[1. Le problème :
Nous avons un répertoire contenant plusieurs fichiers, nous voulons les afficher en faisant un tri sur la taille, la date de création etc.
2. Marche à suivre :
- récupérer les fichiers du répertoire.
- les mettre dans un tableau multidimensionnel.
- trier notre tableau.
En décomposant la tache en plusieurs parties, on s'aperçoit que ce n'est [...]]]></description>
			<content:encoded><![CDATA[<p><strong>1. Le problème :</strong><br />
Nous avons un répertoire contenant plusieurs fichiers, nous voulons les afficher en faisant un tri sur la taille, la date de création etc.</p>
<p><strong>2. Marche à suivre :</strong><br />
- récupérer les fichiers du répertoire.<br />
- les mettre dans un tableau multidimensionnel.<br />
- trier notre tableau.</p>
<p>En décomposant la tache en plusieurs parties, on s'aperçoit que ce n'est pas si compliqué que ça, et pourtant ça semblait difficile :)</p>
<p>allez, on se lance :<br />
<strong>1./2. lecture des fichiers, mise en tableau :</strong></p>

<div class="wp_codebox"><table><tr id="p133"><td class="code" id="p13code3"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$rep</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;NOTRE/REPERTOIRE/&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dir</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/opendir"><span style="color: #990000;">opendir</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rep</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$f</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/readdir"><span style="color: #990000;">readdir</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</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><a href="http://www.php.net/is_file"><span style="color: #990000;">is_file</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rep</span><span style="color: #339933;">.</span><span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sizeMo</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/filesize"><span style="color: #990000;">filesize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rep</span><span style="color: #339933;">.</span><span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1024</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1024</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//la taille du fichier en Mo</span>
    <span style="color: #000088;">$liste1</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$f</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//le nom du fichier</span>
    <span style="color: #000088;">$liste1</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/number_format"><span style="color: #990000;">number_format</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sizeMo</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//la taille en format 7,66</span>
    <span style="color: #000088;">$liste1</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> df<span style="color: #009900;">&#40;</span><a href="http://www.php.net/filectime"><span style="color: #990000;">filectime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rep</span><span style="color: #339933;">.</span><span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//la date de création au format FR (voir df() plus bas)</span>
    <span style="color: #000088;">$liste1</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/filectime"><span style="color: #990000;">filectime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rep</span><span style="color: #339933;">.</span><span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//la date de création au format timestamp (nous servira pour le tri)</span>
    <span style="color: #000088;">$liste</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$liste1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//on met notre tableau liste1 dans un autre tableau (le gros multidimensionnel)</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> df<span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">//pour convertir une date au format FR</span>
  <span style="color: #b1b100;">return</span> <a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;d/m/Y H:i:s&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>après cette opération le résultat pourrait ressembler à ça :</p>
<pre>
fichier1      10     01/05/2007     0123654747899 (exemple du timestamp)
fichier2      20     01/05/2007     0123654747899
fichier3      15     11/14/2006     0123654747899
fichier7      10     21/08/2007     0123654747899
fichier6      150    01/05/2005     0123654747899
etc.</pre>
<p><strong>3. tri de notre tableau :</strong></p>

<div class="wp_codebox"><table><tr id="p134"><td class="code" id="p13code4"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$liste</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$nom</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//c'est la colonne des nom de fichiers$taille[$key] = $row[1];//des tailles</span>
  <span style="color: #000088;">$date1</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//des dates fr</span>
  <span style="color: #000088;">$date2</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//des dates</span>
<span style="color: #009900;">&#125;</span>
<a href="http://www.php.net/array_multisort"><span style="color: #990000;">array_multisort</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$date2</span><span style="color: #339933;">,</span> SORT_DESC<span style="color: #339933;">,</span> <span style="color: #000088;">$nom</span><span style="color: #339933;">,</span> SORT_ASC<span style="color: #339933;">,</span> <span style="color: #000088;">$liste</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//la fonction de tri</span></pre></td></tr></table></div>

<p>et voilà, notre tableau est trié par date de création dans notre cas, on peut maintenant le parcourir et l'afficher comme on veut.</p>
<p>bons tests :)</p>
<p>Pour en discuter : <a href="http://forum.naeh.net/index.php?topic=33.0" target="_blank">http://forum.naeh.net/index.php?topic=33.0</a></p>
]]></content:encoded>
			<wfw:commentRss>http://naeh.net/trier-un-tableau-multidimensionnel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
