cucinato Wed Nov 21 16:23:23 2001 EDT Modified files: /phpdoc/it/functions array.xml Log: Added CDATA sections, modified entities to iso8859-1 chars
Index: phpdoc/it/functions/array.xml diff -u phpdoc/it/functions/array.xml:1.27 phpdoc/it/functions/array.xml:1.28 --- phpdoc/it/functions/array.xml:1.27 Thu Nov 15 06:39:22 2001 +++ phpdoc/it/functions/array.xml Wed Nov 21 16:23:23 2001 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1"?> - <!-- EN-Revision: 1.122 Maintainer: cucinato Status: ready --> + <!-- EN-Revision: 1.127 Maintainer: cucinato Status: ready --> <reference id="ref.array"> <title>Funzioni di Array</title> <titleabbrev>Arrays</titleabbrev> @@ -72,11 +72,13 @@ <example> <title>Esempio di <function>array</function></title> <programlisting role="php"> +<![CDATA[ $frutta = array ( - "frutta" => array ("a"=>"arancia", "b"=>"banana", "c"=>"mela"), - "numeri" => array (1, 2, 3, 4, 5, 6), - "buche" => array ("prima", 5 => "seconda", "terza") + "frutta" => array ("a"=>"arancia", "b"=>"banana", "c"=>"mela"), + "numeri" => array (1, 2, 3, 4, 5, 6), + "buche" => array ("prima", 5 => "seconda", "terza") ); +]]> </programlisting> </example> </para> @@ -84,13 +86,15 @@ <example> <title>Indice automatico con <function>array</function></title> <programlisting role="php"> +<![CDATA[ $array = array( 1, 1, 1, 1, 1, 8=>1, 4=>1, 19, 3=>13); print_r($array); +]]> </programlisting> - </example> - che stamperà: - <informalexample> - <programlisting> + <para> + che stamperà: + <screen role="php"> +<![CDATA[ Array ( [0] => 1 @@ -100,9 +104,11 @@ [4] => 1 [8] => 1 [9] => 19 -) - </programlisting> - </informalexample> + ) +]]> + </screen> + </para> + </example> Si noti che l'indice '3' è definito due volte, e che mantiene il valore finale 13. L'indice 4 è definito dopo l'indice 8, e il successivo indice generato (valore 19) è 9, dal momento che l'indice più grande era 8. @@ -112,21 +118,25 @@ <example> <title>Indice 1-based con <function>array</function></title> <programlisting role="php"> +<![CDATA[ $primotrimestre = array(1 => 'Gennaio', 'Febbraio', 'Marzo'); print_r($primotrimestre); +]]> </programlisting> - </example> - che stamperà: - <informalexample> - <programlisting> + <para> + che stamperà: + <screen> +<![CDATA[ Array ( [1] => 'Gennaio' [2] => 'Febbraio' [3] => 'Marzo' ) - </programlisting> - </informalexample> +]]> + </screen> + </para> + </example> </para> <para> Vedere anche: <function>list</function>. @@ -156,28 +166,30 @@ Si possono usare due costanti, <constant>CASE_UPPER</constant> per le maiuscole e <constant>CASE_LOWER</constant> per le minuscole. Il default è - <constant>CASE_LOWER</constant>. La funziona non modifica + <constant>CASE_LOWER</constant>. La funzione non modifica le chiavi numeriche. </para> - <para> - <example> - <title>esempio di <function>array_change_key_case</function></title> - <programlisting role="php"> + <example> + <title>esempio di <function>array_change_key_case</function></title> + <programlisting role="php"> +<![CDATA[ $input_array = array("PriMo" => 1, "SecOndO" => 4); print_r(array_change_key_case($input_array, CASE_UPPER); - </programlisting> - </example> - Il risultato di questo programma sarà: - <informalexample> - <programlisting> +]]> + </programlisting> + <para> + Il risultato di questo programma sarà: + <screen> +<![CDATA[ Array ( [PRIMO] => 1 [SECONDO] => 2 ) - </programlisting> - </informalexample> - </para> +]]> + </screen> + </para> + </example> </refsect1> </refentry> @@ -211,18 +223,19 @@ usati in ogni array dei numeri crescenti a partire da zero. Il default è &false;. </para> - <para> - <example> - <title>esempio di <function>array_chunk</function></title> - <programlisting role="php"> + <example> + <title>esempio di <function>array_chunk</function></title> + <programlisting role="php"> +<![CDATA[ $input_array = array('a', 'b', 'c', 'd', 'e'); print_r(array_chunk($input_array, 2)); print_r(array_chunk($input_array, 2, TRUE)); - </programlisting> - </example> - Il risultato di questo programma sarà: - <informalexample> - <programlisting> +]]> + </programlisting> + <para> + Il risultato di questo programma sarà: + <screen> +<![CDATA[ Array ( [0] => Array @@ -263,9 +276,10 @@ ) ) - </programlisting> - </informalexample> - </para> +]]> + </screen> + </para> + </example> </refsect1> </refentry> @@ -291,8 +305,10 @@ <example> <title>Esempio di <function>array_count_values</function></title> <programlisting role="php"> +<![CDATA[ $array = array (1, "ciao", 1, "mondo", "ciao"); -array_count_values ($array); // restituisce l'array (1=>2, "ciao"=>2, "mondo"=>1) +array_count_values ($array); // restituisce l'array (1=>2, "ciao"=>2, "mondo"=>1) +]]> </programlisting> </example> </para> @@ -326,9 +342,11 @@ <example> <title>Esempio di <function>array_diff</function></title> <programlisting role="php"> -$array1 = array ("a" => "verde", "rosso", "blu", "rosso"); -$array2 = array ("b" => "verde", "giallo", "rosso"); +<![CDATA[ +$array1 = array ("a" => "verde", "rosso", "blu", "rosso"); +$array2 = array ("b" => "verde", "giallo", "rosso"); $risultato = array_diff ($array1, $array2); +]]> </programlisting> </example> </para> @@ -386,19 +404,21 @@ <example> <title>Esempio di <function>array_filter</function></title> <programlisting role="php"> +<![CDATA[ function dispari($var) { - return ($var % 2 == 1); + return ($var % 2 == 1); } function pari($var) { - return ($var % 2 == 0); + return ($var % 2 == 0); } -$array1 = array ("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5); +$array1 = array ("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5); $array2 = array (6, 7, 8, 9, 10, 11, 12); $arr_dispari = array_filter($array1, "dispari"); $arr_pari = array_filter($array2, "pari"); +]]> </programlisting> </example> </para> @@ -432,8 +452,8 @@ <function>array_flip</function> restituisce un array scambiato, ovvero le chiavi di <parameter>trans</parameter> diventano valori e i valori di <parameter>trans</parameter> diventano chiavi. - </para> - <para> + </para> + <para> Si noti che i valori di <parameter>trans</parameter> devono poter diventare chiavi valide, ovvero devo essere di tipo <type>integer</type> o <type>string</type>. Un errore verrà segnalato se un valore ha il tipo @@ -452,8 +472,10 @@ <example> <title>Esempio di <function>array_flip</function></title> <programlisting role="php"> +<![CDATA[ $trans = array_flip ($trans); $original = strtr ($str, $trans); +]]> </programlisting> </example> </para> @@ -461,9 +483,11 @@ <example> <title>Esempio di <function>array_flip</function>: collisione</title> <programlisting role="php"> +<![CDATA[ $trans = array ("a" => 1, "b" => 1, "c" => 2); $trans = array_flip ($trans); -// ora $trans è : array(1 => "b", 2 => "c"); +// ora $trans è : array(1 => "b", 2 => "c"); +]]> </programlisting> </example> </para> @@ -495,6 +519,7 @@ <example> <title>esempio di <function>array_fill</function></title> <programlisting role="php"> +<![CDATA[ $a = array_fill(5, 6, 'banana'); /* @@ -507,6 +532,7 @@ $a[9] = "banana"; $a[10] = "banana"; */ +]]> </programlisting> </example> </para> @@ -540,9 +566,11 @@ <example> <title>Esempio di <function>array_intersect</function></title> <programlisting role="php"> -$array1 = array ("a" => "verde", "rosso", "blu"); -$array2 = array ("b" => "verde", "giallo", "rosso"); +<![CDATA[ +$array1 = array ("a" => "verde", "rosso", "blu"); +$array2 = array ("b" => "verde", "giallo", "rosso"); $risultato = array_intersect ($array1, $array2); +]]> </programlisting> </example> </para> @@ -594,10 +622,12 @@ <example> <title>esempio di <function>array_key_exists</function></title> <programlisting role="php"> +<![CDATA[ $un_array = array("primo" => 1, "secondo" => 4); if (array_key_exists("primo", $un_array)) { - echo "L'elemento 'primo' è nell'array"; + echo "L'elemento 'primo' è nell'array"; } +]]> </programlisting> </example> </para> @@ -644,14 +674,16 @@ <example> <title>Esempio di <function>array_keys</function></title> <programlisting role="php"> -$array = array (0 => 100, "colore" => "rosso"); +<![CDATA[ +$array = array (0 => 100, "colore" => "rosso"); array_keys ($array); // restituisce array (0, "colore") $array = array ("blu", "rosso", "verde", "blu", "blu"); array_keys ($array, "blu"); // restituisce array (0, 3, 4) -$array = array ("colore" => array("blu", "rosso", "verde"), "misura" => array("piccola", "media", "grande")); +$array = array ("colore" => array("blu", "rosso", "verde"), "misura" => +array("piccola", "media", "grande")); array_keys ($array); // restituisce array ("colore", "misura") +]]> </programlisting> </example> </para> @@ -665,16 +697,18 @@ PHP 3 </title> <programlisting role="php"> +<![CDATA[ function array_keys ($arr, $term="") { $t = array(); while (list($k,$v) = each($arr)) { - if ($term && $v != $term) { + if ($term && $v != $term) { continue; $t[] = $k; } return $t; } } +]]> </programlisting> </example> </para> @@ -715,12 +749,14 @@ <example> <title>Esempio di <function>array_map</function></title> <programlisting role="php"> +<![CDATA[ function cubo($n) { - return $n*$n*$n; + return $n*$n*$n; } $a = array(1, 2, 3, 4, 5); $b = array_map("cubo", $a); +]]> </programlisting> </example> </para> @@ -732,12 +768,13 @@ <example> <title><function>array_map</function> - usare più array</title> <programlisting role="php"> +<![CDATA[ function mostra_Spagnolo($n, $m) { return "Il numero $n si dice $m in Spagnolo"; } function mappa_Spagnolo($n, $m) { - return array ($n => $m); + return array ($n => $m); } $a = array(1, 2, 3, 4, 5); @@ -747,7 +784,7 @@ print_r($c); -// darà: +// darà: // Array // ( // [0] => Il numero 1 si dice uno in Spagnolo @@ -761,7 +798,7 @@ print_r($d); -// darà: +// darà: // Array // ( // [0] => Array @@ -790,6 +827,7 @@ // ) // // ) +]]> </programlisting> </example> </para> @@ -809,6 +847,7 @@ <example> <title><function>Array_map</function> - creare un array di array</title> <programlisting role="php"> +<![CDATA[ $a = array(1, 2, 3, 4, 5); $b = array("uno", "due", "tre", "quattro", "cinque"); $c = array("uno", "dos", "tres", "cuatro", "cinco"); @@ -816,7 +855,7 @@ $d = array_map(null, $a, $b, $c); print_r($d); -// darà: +// darà: // Array // ( // [0] => Array @@ -855,6 +894,7 @@ // ) // // ) +]]> </programlisting> </example> </para> @@ -897,14 +937,16 @@ <example> <title>Esempio di <function>array_merge</function></title> <programlisting role="php"> -$array1 = array ("colore" => "rosso", 2, 4); -$array2 = array ("a", "b", "colore" => "verde", "forma" => "trapezio", 4); +<![CDATA[ +$array1 = array ("colore" => "rosso", 2, 4); +$array2 = array ("a", "b", "colore" => "verde", "forma" => "trapezio", 4); array_merge ($array1, $array2); +]]> </programlisting> </example> </para> <para> - L'array risultante sarà <literal>array("colore" => "verde", 2, 4, + L'array risultante sarà <literal>array("colore" => "verde", 2, 4, "a", "b", "forma" => "trapezio", 4)</literal>. </para> <para> @@ -947,9 +989,11 @@ <example> <title>Esempio di <function>array_merge_recursive</function></title> <programlisting role="php"> -$ar1 = array ("colore" => array ("preferito" => "rosso"), 5); -$ar2 = array (10, "colore" => array ("preferito" => "verde", "blu")); +<![CDATA[ +$ar1 = array ("colore" => array ("preferito" => "rosso"), 5); +$ar2 = array (10, "colore" => array ("preferito" => "verde", "blu")); $result = array_merge_recursive ($ar1, $ar2); +]]> </programlisting> </example> </para> @@ -1042,9 +1086,11 @@ <example> <title>Ordinamre più array</title> <programlisting role="php"> +<![CDATA[ $ar1 = array ("10", 100, 100, "a"); $ar2 = array (1, 3, "2", 1); array_multisort ($ar1, $ar2); +]]> </programlisting> </example> </para> @@ -1058,9 +1104,11 @@ <example> <title>Ordinare un array multi-dimensionale</title> <programlisting role="php"> +<![CDATA[ $ar = array (array ("10", 100, 100, "a"), array (1, 3, "2", 1)); array_multisort ($ar[0], SORT_ASC, SORT_STRING, $ar[1], SORT_NUMERIC, SORT_DESC); +]]> </programlisting> </example> </para> @@ -1105,6 +1153,7 @@ <example> <title>esempio di <function>array_pad</function></title> <programlisting role="php"> +<![CDATA[ $input = array (12, 10, 9); $risultato = array_pad ($input, 5, 0); @@ -1115,6 +1164,7 @@ $risultato = array_pad ($input, 2, "noop"); // ridimensionamento non efettuato +]]> </programlisting> </example> </para> @@ -1145,8 +1195,10 @@ <example> <title>esempio di <function>array_pop</function></title> <programlisting role="php"> +<![CDATA[ $pila = array ("arancia", "mela", "lampone"); $frutto = array_pop ($pila); +]]> </programlisting> </example> </para> @@ -1189,7 +1241,9 @@ lunghezza di <parameter>array</parameter> aumenta del numero di variabili accodate. Ha lo stesso effetto di: <programlisting role="php"> +<![CDATA[ $array[] = $var; +]]> </programlisting> ripetuto per ogni <parameter>var</parameter>. </para> @@ -1200,8 +1254,10 @@ <example> <title>esempio di <function>array_push</function></title> <programlisting role="php"> +<![CDATA[ $pila = array (1, 2); array_push ($pila, "+", 3); +]]> </programlisting> </example> </para> @@ -1256,11 +1312,13 @@ <example> <title>esempio di <function>array_rand</function></title> <programlisting role="php"> +<![CDATA[ srand ((float) microtime() * 10000000); $input = array ("Neo", "Morpheus", "Trinity", "Cypher", "Tank"); $chiavi = array_rand ($input, 2); print $input[$chiavi[0]]."\n"; print $input[$chiavi[1]]."\n"; +]]> </programlisting> </example> </para> @@ -1295,9 +1353,11 @@ <example> <title>esempio di <function>array_reverse</function></title> <programlisting role="php"> +<![CDATA[ $input = array ("php", 4.0, array ("verde", "rosso")); $risultato = array_reverse ($input); $resultato_chiavi = array_reverse ($input, TRUE); +]]> </programlisting> </example> </para> @@ -1348,14 +1408,15 @@ <example> <title>esempio di <function>array_reduce</function></title> <programlisting role="php"> +<![CDATA[ function rsum($v, $w) { - $v += $w; - return $v; + $v += $w; + return $v; } function rmul($v, $w) { - $v *= $w; - return $v; + $v *= $w; + return $v; } $a = array(1, 2, 3, 4, 5); @@ -1363,6 +1424,7 @@ $b = array_reduce($a, "rsum"); $c = array_reduce($a, "rmul", 10); $d = array_reduce($x, "rsum", 1); +]]> </programlisting> </example> </para> @@ -1405,8 +1467,10 @@ <example> <title>esempio di <function>array_shift</function></title> <programlisting role="php"> +<![CDATA[ $args = array ("-v", "-f"); $opt = array_shift ($args); +]]> </programlisting> </example> </para> @@ -1466,12 +1530,14 @@ <example> <title>esempi di <function>array_slice</function></title> <programlisting role="php"> +<![CDATA[ $input = array ("a", "b", "c", "d", "e"); $output = array_slice ($input, 2); // restituisce "c", "d", e "e" $output = array_slice ($input, 2, -1); // restituisce "c", "d" $output = array_slice ($input, -2, 1); // restituisce "d" $output = array_slice ($input, 0, 3); // restituisce "a", "b", e "c" +]]> </programlisting> </example> </para> @@ -1547,13 +1613,15 @@ </para> <para> Valgono le seguenti equivalenze: - <programlisting> + <programlisting role="php"> +<![CDATA[ array_push ($input, $x, $y) array_splice ($input, count ($input), 0, array ($x, $y)) array_pop ($input) array_splice ($input, -1) array_shift ($input) array_splice ($input, 0, 1) array_unshift ($input, $x, $y) array_splice ($input, 0, 0, array ($x, $y)) $a[$x] = $y array_splice ($input, $x, 1, $y) +]]> </programlisting> </para> <para> @@ -1563,22 +1631,24 @@ <example> <title>esempi di <function>array_splice</function></title> <programlisting role="php"> +<![CDATA[ $input = array ("rosso", "verde", "blu", "giallo"); array_splice ($input, 2); -// $input è ora array ("rosso", "verde") +// $input è ora array ("rosso", "verde") $input = array ("rosso", "verde", "blu", "giallo"); array_splice ($input, 1, -1); -// $input è ora array ("rosso", "giallo") +// $input è ora array ("rosso", "giallo") $input = array ("rosso", "verde", "blu", "giallo"); array_splice ($input, 1, count($input), "arancio"); -// $input è ora array ("rosso", "arancio") +// $input è ora array ("rosso", "arancio") $input = array ("rosso", "verde", "blu", "giallo"); array_splice ($input, -1, 1, array("nero", "marrone")); -// $input è ora array ("rosso", "verde", +// $input è ora array ("rosso", "verde", // "blu", "nero", "marrone") +]]> </programlisting> </example> </para> @@ -1611,6 +1681,7 @@ <example> <title>esempi di <function>array_sum</function></title> <programlisting role="php"> +<![[CDATA[ $a = array(2,4,6,8); echo "sum(a) = ".array_sum($a)."\n"; // stampa: sum(a) = 20 @@ -1618,6 +1689,7 @@ $b = array("a"=>1.2,"b"=>2.3,"c"=>3.4); echo "sum(b) = ".array_sum($b)."\n"; // stampa: sum(b) = 6.9 +]]> </programlisting> </example> </para> @@ -1668,16 +1740,18 @@ <example> <title>esempio di <function>array_unique</function></title> <programlisting role="php"> -$input = array ("a" => "verde", "rosso", "b" => "verde", "blu", "rosso"); +<![CDATA[ +$input = array ("a" => "verde", "rosso", "b" => "verde", "blu", "rosso"); $risultato = array_unique ($input); print_r($result); -// il risultato sarà +// il risultato sarà //Array //( // [a] => verde // [0] => rosso // [1] => blu //) +]]> </programlisting> </example> </para> @@ -1685,6 +1759,7 @@ <example> <title><function>array_unique</function> e i tipi</title> <programlisting role="php"> +<![CDATA[ $input = array (4,"4","3",4,3,"3"); $risultato = array_unique ($input); var_dump($risultato); @@ -1697,6 +1772,7 @@ string(1) "3" } */ +]]> </programlisting> </example> </para> @@ -1738,8 +1814,10 @@ <example> <title>esempio di <function>array_unshift</function></title> <programlisting role="php"> +<![CDATA[ $lista = array ("p1", "p3"); array_unshift ($lista, "p4", "p5", "p6"); +]]> </programlisting> </example> </para> @@ -1776,8 +1854,10 @@ <example> <title>esempio di <function>array_values</function></title> <programlisting role="php"> -$array = array ("taglia" => "XL", "colore" => "oro"); +<![CDATA[ +$array = array ("taglia" => "XL", "colore" => "oro"); array_values ($array); // restituisce l'array ("XL", "oro") +]]> </programlisting> </example> </para> @@ -1791,6 +1871,7 @@ PHP 3 </title> <programlisting role="php"> +<![CDATA[ function array_values ($arr) { $t = array(); while (list($k, $v) = each ($arr)) { @@ -1798,6 +1879,7 @@ } return $t; } +]]> </programlisting> </example> </para> @@ -1869,14 +1951,15 @@ <example> <title>esempio di <function>array_walk</function></title> <programlisting role="php"> -$frutta = array ("d"=>"limone", "a"=>"arancia", "b"=>"banana", "c"=>"mela"); +<![CDATA[ +$frutta = array ("d"=>"limone", "a"=>"arancia", "b"=>"banana", "c"=>"mela"); -function modifica (&$elemento1, $chiave, $prefisso) { +function modifica (&$elemento1, $chiave, $prefisso) { $elemento1 = "$prefisso: $elemento1"; } function stampa ($elemento2, $chiave) { - echo "$chiave. $elemento2<br>\n"; + echo "$chiave. $elemento2<br>\n"; } array_walk ($frutta, 'stampa'); @@ -1884,6 +1967,7 @@ array_walk ($frutta, 'modifica', 'frutto'); reset ($frutta); array_walk ($frutta, 'stampa'); +]]> </programlisting> </example> </para> @@ -1916,31 +2000,31 @@ la loro correlazione con gli elementi ai quali sono associati. Viene usata principalmente nell'ordinamento degli array associativi, quando la disposizione originaria degli elementi è importante. - <example> - <title>esempio di <function>arsort</function></title> - <programlisting role="php"> -$frutta = array ("d"=>"limone", "a"=>"arancia", "b"=>"banana", "c"=>"mela"); + </para> + <example> + <title>esempio di <function>arsort</function></title> + <programlisting role="php"> +<![CDATA[ +$frutta = array ("d"=>"limone", "a"=>;"arancia", "b"=>"banana", "c"=>"mela"); arsort ($frutta); reset ($frutta); while (list ($chiave, $valore) = each ($frutta)) { echo "$chiave = $valore\n"; } - </programlisting> - </example> - </para> - <para> - Questo esempio mostrerà: - </para> - <para> - <informalexample> - <programlisting> -frutta[c] = mela -frutta[d] = limone -frutta[b] = banana -frutta[a] = arancia - </programlisting> - </informalexample> - </para> +]]> + </programlisting> + <para> + Questo esempio mostrerà + <screen> +<![CDATA[ +c = mela +d = limone +b = banana +a = arancia +]]> + </screen> + </para> + </example> <para> I frutti sono ordinati in ordine alfabetico decrescente, e l'indice associato a ogni elemento è stato mantenuto. @@ -1978,31 +2062,31 @@ la loro correlazione con gli elementi ai quali sono associati. Viene usata principalmente nell'ordinamento degli array associativi, quando la disposizione originaria degli elementi è importante . - <example> - <title>esempio di <function>asort</function></title> - <programlisting role="php"> -$frutta = array ("d"=>"limone", "a"=>"arancia", "b"=>"banana", "c"=>"mela"); + </para> + <example> + <title>esempio di <function>asort</function></title> + <programlisting role="php"> +<![CDATA[ +$frutta = array ("d"=>"limone", "a"=>"arancia", "b"=>"banana", "c"=>"mela"); asort ($frutta); reset ($frutta); while (list ($chiave, $valore) = each ($frutta)) { echo "$chiave = $valore\n"; } - </programlisting> - </example> - </para> - <para> - Questo esempio mostrerà: - </para> - <para> - <informalexample> - <programlisting> -fruits[a] = arancia -fruits[b] = banana -fruits[d] = limone -fruits[c] = mela - </programlisting> - </informalexample> - </para> +]]> + </programlisting> + <para> + Questo esempio mostrerà: + <screen> +<![CDATA[ +a = arancia +b = banana +d = limone +c = mela +]]> + </screen> + </para> + </example> <para> I frutti sono ordinati in ordine alfabetico , e l'indice associato ad ogni elemento è stato mantenuto. @@ -2059,6 +2143,7 @@ <example> <title>esempio di <function>compact</function></title> <programlisting role="php"> +<![CDATA[ $citta = "Milano"; $provincia = "MI"; $evento = "SMAU"; @@ -2066,6 +2151,7 @@ $var_luoghi = array ("citta", "provincia"); $risultato = compact ("evento", "niente", $var_luoghi); +]]> </programlisting> <para> In questo modo, <varname>$risultato</varname> sarà <literal>array @@ -2099,28 +2185,29 @@ elemento). </para> <para> - Se <parameter>var</parameter> non è un array, verrà restituito <literal>1</literal> - (eccezione: <literal>count(&null;)</literal> restituisce - <literal>0</literal>). + Se <parameter>var</parameter> non è un array, verrà restituito +<literal>1</literal> + (eccezione: <literal>count(&null;)</literal> restituisce + <literal>0</literal>). </para> <warning> - <para> - <function>count</function> può restituire 0 per una variabile che - non è impostata, ma può anche restituire 0 per una variabile che è - stata inizializzata con un array vuoto. Usare - <function>isset</function> per verificare se una variabile è impostata. - </para> + <para> + <function>count</function> può restituire 0 per una variabile che + non è impostata, ma può anche restituire 0 per una variabile che +è + stata inizializzata con un array vuoto. Usare + <function>isset</function> per verificare se una variabile è impostata. + </para> </warning> <para> - Vedere la sezione <link linkend="language.types.array">Arrays</link> - nel manuale per una spiegazione dettagliata di come gli array siano - implementati ed usati in PHP. + Vedere la sezione <link linkend="language.types.array">Arrays</link> + nel manuale per una spiegazione dettagliata di come gli array siano + implementati ed usati in PHP. </para> <para> <example> <title>esempio di <function>count</function></title> <programlisting role="php"> - <!-- TODO: examples about count(null), count(false), count(object).. --> + <!-- TODO: examples about count(null), count(false), count(object).. --> +<![CDATA[ $a[0] = 1; $a[1] = 3; $a[2] = 5; @@ -2132,6 +2219,7 @@ $b[10] = 11; $risultato = count ($b); // $result == 3; +]]> </programlisting> </example> </para> @@ -2228,8 +2316,10 @@ <example> <title>esempi di<function>each</function></title> <programlisting role="php"> +<![CDATA[ $foo = array ("bob", "fred", "jussi", "jouni", "egon", "marliese"); $bar = each ($foo); +]]> </programlisting> <para> <varname>$bar</varname> ora contiene la seguente coppia @@ -2241,8 +2331,10 @@ <listitem><simpara>value => 'bob'</simpara></listitem> </itemizedlist> <programlisting role="php"> -$foo = array ("Robert" => "Bob", "Seppo" => "Sepi"); +<![CDATA[ +$foo = array ("Robert" => "Bob", "Seppo" => "Sepi"); $bar = each ($foo); +]]> </programlisting> </para> <para> @@ -2267,11 +2359,13 @@ <function>each</function> </title> <programlisting role="php"> -echo "Valori inviati con il metodo POST:<br>"; +<![CDATA[ +echo "Valori inviati con il metodo POST:<br>"; reset ($HTTP_POST_VARS); while (list ($chiave, $valore) = each ($HTTP_POST_VARS)) { - echo "$chiave => $valore<br>"; -} + echo "$chiave => $valore<br>"; + } +]]> </programlisting> </example> </para> @@ -2432,27 +2526,31 @@ <example> <title>esempio di<function>extract</function></title> <programlisting role="php"> -<?php +<![CDATA[ +<?php /* Si supponga che $array_variabili sia un array restituito da wddx_deserialize */ $dimensione = "grande"; -$array_variabili = array ("colore" => "blu", - "dimensione" => "media", - "forma" => "sfera"); +$array_variabili = array ("colore" => "blu", + "dimensione" => "media", + "forma" => "sfera"); extract ($array_variabili, EXTR_PREFIX_SAME, "wddx"); print "$colore, $dimensione, $forma, $wddx_dimensione\n"; ?> +]]> </programlisting> </example> </para> <para> Questo esempio mostrerà: <programlisting> +<![CDATA[ blu, grande, sfera, media +]]> </programlisting> </para> <para> @@ -2508,10 +2606,12 @@ <example> <title>esempio di <function>in_array</function></title> <programlisting role="php"> +<![CDATA[ $os = array ("Mac", "NT", "Irix", "Linux"); if (in_array ("Irix", $os)){ print "trovato Irix"; - } +} +]]> </programlisting> </example> </para> @@ -2519,18 +2619,20 @@ <example> <title>esempio di <function>in_array</function> con strict</title> <programlisting role="php"> -<?php +<![CDATA[ +<?php $a = array('1.10', 12.4, 1.13); if (in_array('12.4', $a, TRUE)) - echo "'12.4' trovato con controllo strict\n"; + echo "'12.4' trovato con controllo strict\n" if (in_array(1.13, $a, TRUE)) - echo "1.13 trovato con controllo strict\n"; -?> + echo "1.13 trovato con controllo strict\n" +?> -// Questo ritornerà: +// Questo ritornerà: 1.13 trovato con controllo strict +]]> </programlisting> </example> </para> @@ -2620,12 +2722,14 @@ <example> <title>Esempio di <function>krsort</function></title> <programlisting role="php"> -$frutti = array ("d"=>"limone", "a"=>"arancio", "b"=>"banana", "c"=>"mela"); +<![CDATA[ +$frutti = array ("d"=>"limone", "a"=>"arancio", "b"=>"banana", "c"=>"mela"); krsort ($frutti); reset ($frutti); while (list ($chiave, $valore) = each ($frutti)) { - echo "$chiave -> $valore\n"; + echo "$chiave = $valore\n"; } +]]> </programlisting> </example> </para> @@ -2635,10 +2739,12 @@ <para> <informalexample> <programlisting> +<![CDATA[ d = limone c = mela b = banana a = arancio +]]> </programlisting> </informalexample> </para> @@ -2677,12 +2783,14 @@ <example> <title>esempio di <function>ksort</function></title> <programlisting role="php"> -$frutti = array ("d"=>"limone", "a"=>"arancia", "b"=>"banana", "c"=>"mela"); +<![CDATA[ +$frutti = array ("d"=>"limone", "a"=>"arancia", "b"=>"banana", "c"=>"mela"); ksort ($frutti); reset ($frutti); while (list ($chiave, $valore) = each ($frutti)) { echo "$chiave = $valore\n"; } +]]> </programlisting> </example> </para> @@ -2692,10 +2800,12 @@ <para> <informalexample> <programlisting> +<![CDATA[ a = arancia b = banana c = mela d = limone +]]> </programlisting> </informalexample> </para> @@ -2739,25 +2849,27 @@ <example> <title>esempio di <function>list</function></title> <programlisting role="php"> -<table> - <tr> - <th>Nome dell'impiegato</th> - <th>Stipendio</th> - </tr> +<![CDATA[ +<table> + <tr> + <th>Nome dell'impiegato</th> + <th>Stipendio</th> + </tr> -<?php +<?php $risultato = mysql_query ($conn, "SELECT id, nome, stipendio FROM impiegati"); while (list ($id, $nome, $stipendio) = mysql_fetch_row ($risultato)) { - print (" <tr>\n". - " <td><a href=\"info.php3?id=$id\">$nome</a></td>\n". - " <td>$stipendio</td>\n". - " </tr>\n"); + print (" <tr>\n". + " <td><a href=\"info.php3?id=$id\">$nome</a></td>\n". + " <td>$stipendio</td>\n". + " </tr>\n"); } ?> -</table> +</table> +]]> </programlisting> </example> </para> @@ -2793,8 +2905,9 @@ <example> <title>esempio di <function>natsort</function></title> <programlisting role="php"> -$array1 = $array2 = array ("img12.png","img10.png","img2.png","img1.png"); - +<![CDATA[ +$array1 = $array2 = array ("img12.png", "img10.png", "img2.png", "img1.png"); + sort($array1); echo "Ordinamento standard\n"; print_r($array1); @@ -2802,6 +2915,7 @@ natsort($array2); echo "\nOrdinamento naturale\n"; print_r($array2); +]]> </programlisting> </example> </para> @@ -2811,23 +2925,25 @@ <para> <informalexample> <programlisting> +<![CDATA[ Ordinamento standard Array ( - [0] => img1.png - [1] => img10.png - [2] => img12.png - [3] => img2.png + [0] => img1.png + [1] => img10.png + [2] => img12.png + [3] => img2.png ) Ordinamento naturale Array ( - [3] => img1.png - [2] => img2.png - [1] => img10.png - [0] => img12.png -) + [3] => img1.png + [2] => img2.png + [1] => img10.png + [0] => img12.png + ) +]]> </programlisting> </informalexample> Per ulteriori informazioni vedere la pagina di Martin Pool <ulink @@ -3013,25 +3129,27 @@ <example> <title>esempi di <function>range</function></title> <programlisting role="php"> +<![CDATA[ foreach(range(0,9) as $numero) { - echo $numero; + echo $numero; } -foreach(range('a','z') as $lettera) { - echo $lettera; +foreach(range('a', 'z') as $lettera) { + echo $lettera; } foreach(range('z','a') as $lettera) { - echo $lettera; + echo $lettera; } +]]> </programlisting> </example> </para> - <note> - <para> - Prima della versione 4.1.0 la funzione <function>range</function> generava solo - array di interi crescenti. Il supporto per le sequenze di caratteri - e per le sequenza decrescenti è stato aggiunto nella 4.1.0. - </para> - </note> + <note> + <para> + Prima della versione 4.1.0 la funzione <function>range</function> generava solo + array di interi crescenti. Il supporto per le sequenze di caratteri + e per le sequenza decrescenti è stato aggiunto nella 4.1.0. + </para> + </note> <para> Vedere <function>shuffle</function> per un altro esempio d'uso. </para> @@ -3090,12 +3208,14 @@ <example> <title>esempio di <function>rsort</function></title> <programlisting role="php"> -$fruitti = array ("limone", "arancia", "banana", "mela"); +<![CDATA[ +$frutti = array ("limone", "arancia", "banana", "mela"); rsort ($frutti); reset ($frutti); while (list ($chiave, $valore) = each ($frutti)) { echo "$chiave = $valore\n"; } +]]> </programlisting> </example> </para> @@ -3105,10 +3225,12 @@ <para> <informalexample> <programlisting> +<![CDATA[ 0 = mela 1 = limone 2 = banana 3 = arancia +]]> </programlisting> </informalexample> </para> @@ -3148,12 +3270,14 @@ <example> <title>esempio di <function>shuffle</function></title> <programlisting role="php"> +<![CDATA[ $numeri = range (1,20); srand ((float)microtime()*1000000); shuffle ($numeri); while (list (, $numeero) = each ($numeri)) { echo "$numero "; } +]]> </programlisting> </example> </para> @@ -3209,17 +3333,19 @@ più piccolo al più grande. <example> <title>esempio di <function>sort</function></title> - <programlisting role="php"> -<?php + <programlisting role="php"> +<![CDATA[ +<?php -$frutti = array ("limon", "arancia", "banana", "mela"); +$frutti = array ("limone", "arancia", "banana", "mela"); sort ($frutti); reset ($frutti); while (list ($chiave, $valore) = each ($frutti)) { echo "frutti[".$chiave."] = ".$valore."\n"; } -?> +?> +]]> </programlisting> </example> </para> @@ -3229,10 +3355,12 @@ <para> <informalexample> <programlisting> +<![CDATA[ frutti[0] = arancia frutti[1] = banana frutti[2] = limone frutti[3] = mela +]]> </programlisting> </informalexample> </para> @@ -3339,18 +3467,20 @@ <example> <title>esempio di <function>uksort</function></title> <programlisting role="php"> +<![CDATA[ function cmp ($a, $b) { if ($a == $b) return 0; return ($a > $b) ? -1 : 1; } -$a = array (4 => "quattro", 3 => "tre", 20 => "venti", 10 => "dieci"); +$a = array (4 => "quattro", 3 => "tre", 20 => "venti", 10 => "dieci"); uksort ($a, "cmp"); while (list ($chiave, $valore) = each ($a)) { echo "$chiave: $valore\n"; } +]]> </programlisting> </example> </para> @@ -3360,10 +3490,12 @@ <para> <informalexample> <programlisting> +<![CDATA[ 20: venti 10: dieci 4: quattro 3: tre +]]> </programlisting> </informalexample> </para> @@ -3403,15 +3535,20 @@ o maggiore di zero a seconda che il primo argomento sia considerato rispettivamente minore, uguale o maggiore del secondo. Se due elementi risultano uguali, il loro ordine - dnell'array ordinato è indefinito. + nell'array ordinato è indefinito. + </para> + <para> + È anche possibile usare una funzione membro di un oggetto come + funzione di comparazione. Vedere l'esempio numer 3 pù sotto. </para> <para> <example> <title>esempio di <function>usort</function></title> <programlisting role="php"> +<![CDATA[ function cmp ($a, $b) { if ($a == $b) return 0; - return ($a < $b) ? -1 : 1; + return ($a > $b) ? -1 : 1; } $a = array (3, 2, 5, 6, 1); @@ -3421,6 +3558,7 @@ while (list ($chiave, $valore) = each ($a)) { echo "$chiave: $valore\n"; } +]]> </programlisting> </example> </para> @@ -3430,11 +3568,13 @@ <para> <informalexample> <programlisting> +<![CDATA[ 0: 6 1: 5 2: 3 3: 2 4: 1 +]]> </programlisting> </informalexample> </para> @@ -3450,8 +3590,9 @@ esempio di <function>usort</function> con un array multidimensionale </title> <programlisting role="php"> +<![CDATA[ function cmp ($a, $b) { - return strcmp($a["frutto"],$b["frutto"]); + return strcmp($a["frutto"], $b["frutto"]); } $frutti[0]["frutto"] = "limoni"; @@ -3463,12 +3604,13 @@ while (list ($chiave, $valore) = each ($frutti)) { echo "\$frutti[$chiave]: " . $valore["frutto"] . "\n"; } +]]> </programlisting> </example> </para> <para> Quando si ordina un array multidimensionale, $a e $b contengono - rierimenti al primo indice dell'array. + riferimenti al primo indice dell'array. </para> <para> Questo esempio mostrerà: @@ -3476,9 +3618,60 @@ <para> <informalexample> <programlisting> +<![CDATA[ $fruits[0]: arance $fruits[1]: limoni $fruits[2]: uva +]]> + </programlisting> + </informalexample> + </para> + + <para> + <example> + <title> + esempio di <function>usort</function> usando una funzione membro di un oggetto + </title> + <programlisting role="php"> +class OggettoTest { + var $nome; + + function OggettoTest($nome) + { + $this->nome = $nome; + } + + /* Questa è la funzione statica di comparazione: */ + function comp_ogg($a, $b) + { + $al = strtolower($a->nome); + $bl = strtolower($b->nome); + if ($al == $bl) return 0; + return ($al > $bl) ? +1 : -1; + } +} + +$a[] = new OggettoTest("c"); +$a[] = new OggettoTest("b"); +$a[] = new OggettoTest("d"); + +uasort($a, array ("OggettoTest", "comp_ogg")); + +foreach ($a as $voce) { + print $voce->nome."\n"; +} + </programlisting> + </example> + </para> + <para> + Questo esempio mostrerà: + </para> + <para> + <informalexample> + <programlisting> +b +c +d </programlisting> </informalexample> </para>