1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.161 $ -->
<reference id="ref.array">
<title>Array Functions</title>
<titleabbrev>Arrays</titleabbrev>
<partintro>
<simpara>
These functions allow you to interact with and manipulate
arrays in various ways. Arrays are essential for storing,
managing, and operating on sets of variables.
</simpara>
<simpara>
Simple and multi-dimensional arrays are supported, and may be
either user created or created by another function.
There are specific database handling functions for populating
arrays from database queries, and several functions return arrays.
</simpara>
<para>
Please see the <link linkend="language.types.array">Arrays</link>
section of the manual for a detailed explanation of how arrays are
implemented and used in PHP.
</para>
<para>
See also <function>is_array</function>, <function>explode</function>,
<function>implode</function>, <function>split</function>,
and <function>join</function>.
</para>
</partintro>
<refentry id="function.array">
<refnamediv>
<refname>array</refname>
<refpurpose>
Create an array
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array</methodname>
<methodparam choice="opt"><type>mixed</type><parameter>...</parameter></methodparam>
</methodsynopsis>
<para>
Returns an array of the parameters. The parameters can be given
an index with the <literal>=></literal> operator.
</para>
<para>
<note>
<para>
<function>array</function> is a language construct used to
represent literal arrays, and not a regular function.
</para>
</note>
</para>
<para>
Syntax "index => values", separated by commas, define index
and values. index may be of type string or numeric. When index is
omitted, a integer index is automatically generated, starting
at 0. If index is an integer, next generated index will
be the biggest integer index + 1. Note that when two identical
index are defined, the last overwrite the first.
</para>
<para>
The following example demonstrates how to create a
two-dimensional array, how to specify keys for associative
arrays, and how to skip-and-continue numeric indices in normal
arrays.
<example>
<title><function>array</function> example</title>
<programlisting role="php">
<![CDATA[
$fruits = array (
"fruits" => array ("a"=>"orange", "b"=>"banana", "c"=>"apple"),
"numbers" => array (1, 2, 3, 4, 5, 6),
"holes" => array ("first", 5 => "second", "third")
);
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>Automatic index with <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>
<para>
will display :
<screen role="php">
<![CDATA[
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 13
[4] => 1
[8] => 1
[9] => 19
)
]]>
</screen>
</para>
</example>
Note that index '3' is defined twice, and keep its final value of 13.
Index 4 is defined after index 8, and next generated index (value 19)
is 9, since biggest index was 8.
</para>
<para>
This example creates a 1-based array.
<example>
<title>1-based index with <function>array</function></title>
<programlisting role="php">
<![CDATA[
$firstquarter = array(1 => 'January', 'February', 'March');
print_r($firstquarter);
]]>
</programlisting>
<para>
will display :
<screen>
<![CDATA[
Array
(
[1] => 'January'
[2] => 'February'
[3] => 'March'
)
]]>
</screen>
</para>
</example>
</para>
<para>
See also <function>array_pad</function>,
<function>list</function>, and <function>range</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-change-key-case">
<refnamediv>
<refname>array_change_key_case</refname>
<refpurpose>Returns an array with all string keys lowercased or uppercased</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_change_key_case</methodname>
<methodparam><type>array</type><parameter>input</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>case</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_change_key_case</function> changes the
keys in the <parameter>input</parameter> array to
be all lowercase or uppercase. The change depends
on the last optional <parameter>case</parameter>
parameter. You can pass two constants there,
<constant>CASE_UPPER</constant> and
<constant>CASE_LOWER</constant>. The default is
<constant>CASE_LOWER</constant>. The function will leave
number indices as is.
</para>
<example>
<title><function>array_change_key_case</function> example</title>
<programlisting role="php">
<![CDATA[
$input_array = array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_UPPER);
]]>
</programlisting>
<para>
The printout of the above program will be:
<screen>
<![CDATA[
Array
(
[FIRST] => 1
[SECOND] => 2
)
]]>
</screen>
</para>
</example>
</refsect1>
</refentry>
<refentry id="function.array-chunk">
<refnamediv>
<refname>array_chunk</refname>
<refpurpose>Split an array into chunks</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_chunk</methodname>
<methodparam><type>array</type><parameter>input</parameter></methodparam>
<methodparam><type>int</type><parameter>size</parameter></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>preserve_keys</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_chunk</function> splits the array into
several arrays with <parameter>size</parameter> values
in them. You may also have an array with less values
at the end. You get the arrays as members of a
multidimensional array indexed with numbers starting
from zero.
</para>
<para>
By setting the optional <parameter>preserve_keys</parameter>
parameter to &true;, you can force PHP to preserve the original
keys from the input array. If you specify &false; new number
indices will be used in each resulting array with
indices starting from zero. The default is &false;.
</para>
<example>
<title><function>array_chunk</function> example</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>
<para>
The printout of the above program will be:
<screen>
<![CDATA[
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => c
[1] => d
)
[2] => Array
(
[0] => e
)
)
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[2] => c
[3] => d
)
[2] => Array
(
[4] => e
)
)
]]>
</screen>
</para>
</example>
</refsect1>
</refentry>
<refentry id="function.array-count-values">
<refnamediv>
<refname>array_count_values</refname>
<refpurpose>Counts all the values of an array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_count_values</methodname>
<methodparam><type>array</type><parameter>input</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_count_values</function> returns an array using
the values of the <parameter>input</parameter> array as keys and
their frequency in <parameter>input</parameter> as values.
</para>
<para>
<example>
<title><function>array_count_values</function> example</title>
<programlisting role="php">
<![CDATA[
$array = array (1, "hello", 1, "world", "hello");
print_r(array_count_values ($array));
]]>
</programlisting>
<para>
The printout of the above program will be:
<screen>
<![CDATA[
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
]]>
</screen>
</para>
</example>
</para>
</refsect1>
</refentry>
<refentry id="function.array-diff">
<refnamediv>
<refname>array_diff</refname>
<refpurpose>Computes the difference of arrays</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_diff</methodname>
<methodparam><type>array</type><parameter>array1</parameter></methodparam>
<methodparam><type>array</type><parameter>array2</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter> ...</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_diff</function> returns an array
containing all the values of <parameter>array1</parameter>
that are not present in any of the other arguments.
Note that keys are preserved.
</para>
<para>
<example>
<title><function>array_diff</function> example</title>
<programlisting role="php">
<![CDATA[
$array1 = array ("a" => "green", "red", "blue", "red");
$array2 = array ("b" => "green", "yellow", "red");
$result = array_diff ($array1, $array2);
]]>
</programlisting>
</example>
</para>
<para>
This makes <varname>$result</varname> have
<literal>array ("blue");</literal>. Multiple occurrences in
$array1 are all treated the same way.
</para>
<note>
<simpara>
Two elements are considered equal if and only if
<literal>(string) $elem1 === (string) $elem2</literal>. In words:
when the string representation is the same.
<!-- TODO: example of it... -->
</simpara>
</note>
<warning>
<simpara>
This was broken in PHP 4.0.4!
<!-- TODO: when exactly was this broken?... -->
</simpara>
</warning>
<para>
See also <function>array_intersect</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-filter">
<refnamediv>
<refname>array_filter</refname>
<refpurpose>
Filters elements of an array using a callback function
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_filter</methodname>
<methodparam><type>array</type><parameter>input</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>callback</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_filter</function> returns an array
containing all the elements of <parameter>input</parameter>
filtered according a callback function. If the
<parameter>input</parameter> is an associative array
the keys are preserved.
</para>
<para>
<example>
<title><function>array_filter</function> example</title>
<programlisting role="php">
<![CDATA[
function odd($var) {
return ($var % 2 == 1);
}
function even($var) {
return ($var % 2 == 0);
}
$array1 = array ("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array (6, 7, 8, 9, 10, 11, 12);
echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
print_r(array_filter($array2, "even"));
]]>
</programlisting>
<para>
The printout of the program above will be:
<screen role="php">
<![CDATA[
Odd :
Array
(
[a] => 1
[c] => 3
[e] => 5
)
Even:
Array
(
[0] => 6
[2] => 8
[4] => 10
[6] => 12
)
]]>
</screen>
</para>
</example>
</para>
¬e.func-callback;
<para>
See also <function>array_map</function> and
<function>array_reduce</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-flip">
<refnamediv>
<refname>array_flip</refname>
<refpurpose>Flip all the values of an array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_flip</methodname>
<methodparam><type>array</type><parameter>trans</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_flip</function> returns an <type>array</type> in flip
order, i.e. keys from <parameter>trans</parameter> become values and
<parameter>trans</parameter>'s values become keys.
</para>
<para>
Note that the values of <parameter>trans</parameter> need to be valid
keys, i.e. they need to be either <type>integer</type> or
<type>string</type>. A warning will be emitted if a value has the wrong
type, and the key/value pair in question <emphasis>will not be
flipped</emphasis>.
</para>
<para>
If a value has several occurrences, the latest key will be
used as its values, and all others will be lost.
</para>
<para>
<function>array_flip</function> returns &false;
if it fails.
</para>
<para>
<example>
<title><function>array_flip</function> example</title>
<programlisting role="php">
<![CDATA[
$trans = array_flip ($trans);
$original = strtr ($str, $trans);
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title><function>array_flip</function> example : collision</title>
<programlisting role="php">
<![CDATA[
$trans = array ("a" => 1, "b" => 1, "c" => 2);
$trans = array_flip ($trans);
print_r($trans);
]]>
</programlisting>
<para>
now $trans is :
<screen>
<![CDATA[
Array
(
[1] => b
[2] => c
)
]]>
</screen>
</para>
</example>
</para>
</refsect1>
</refentry>
<refentry id="function.array-fill">
<refnamediv>
<refname>array_fill</refname>
<refpurpose>Fill an array with values</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_fill</methodname>
<methodparam><type>int</type><parameter>start_index</parameter></methodparam>
<methodparam><type>int</type><parameter>num</parameter></methodparam>
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_fill</function> fills an array with
<parameter>num</parameter> entries of the value of the
<parameter>value</parameter> parameter, keys starting at the
<parameter>start_index</parameter> parameter.
</para>
<para>
<example>
<title><function>array_fill</function> example</title>
<programlisting role="php">
<![CDATA[
$a = array_fill(5, 6, 'banana');
]]>
</programlisting>
<para>
$a now has the following entries using <function>print_r</function>:
<screen>
<![CDATA[
Array
(
[5] => banana
[6] => banana
[7] => banana
[8] => banana
[9] => banana
[10] => banana
)
]]>
</screen>
</para>
</example>
</para>
</refsect1>
</refentry>
<refentry id="function.array-intersect">
<refnamediv>
<refname>array_intersect</refname>
<refpurpose>Computes the intersection of arrays</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_intersect</methodname>
<methodparam><type>array</type><parameter>array1</parameter></methodparam>
<methodparam><type>array</type><parameter>array2</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter> ...</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_intersect</function> returns an array
containing all the values of <parameter>array1</parameter>
that are present in all the arguments.
Note that keys are preserved.
</para>
<para>
<example>
<title><function>array_intersect</function> example</title>
<programlisting role="php">
<![CDATA[
$array1 = array ("a" => "green", "red", "blue");
$array2 = array ("b" => "green", "yellow", "red");
$result = array_intersect ($array1, $array2);
]]>
</programlisting>
<para>
This makes <varname>$result</varname> have
<screen role="php">
<![CDATA[
Array
(
[a] => green
[0] => red
)
]]>
</screen>
</para>
</example>
</para>
<note>
<simpara>
Two elements are considered equal if and only if
<literal>(string) $elem1 === (string) $elem2</literal>. In words:
when the string representation is the same.
<!-- TODO: example of it... -->
</simpara>
</note>
<warning>
<simpara>
This was broken in PHP 4.0.4!
<!-- TODO: when exactly was this broken?... -->
</simpara>
</warning>
<para>
See also <function>array_diff</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-key-exists">
<refnamediv>
<refname>array_key_exists</refname>
<refpurpose>Checks if the given key or index exists in the array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>bool</type><methodname>array_key_exists</methodname>
<methodparam><type>mixed</type><parameter>key</parameter></methodparam>
<methodparam><type>array</type><parameter>search</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_key_exists</function> returns &true; if the
given <parameter>key</parameter> is set in the array.
<parameter>key</parameter> can be any value possible
for an array index.
</para>
<para>
<example>
<title><function>array_key_exists</function> example</title>
<programlisting role="php">
<![CDATA[
$search_array = array("first" => 1, "second" => 4);
if (array_key_exists("first", $search_array)) {
echo "The 'first' element is in the array";
}
]]>
</programlisting>
</example>
</para>
<note>
<simpara>
This name of this function is <function>key_exists</function>
in PHP version 4.0.6.
</simpara>
</note>
<para>
See also <function>isset</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-keys">
<refnamediv>
<refname>array_keys</refname>
<refpurpose>Return all the keys of an array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_keys</methodname>
<methodparam><type>array</type><parameter>input</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>
search_value
</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_keys</function> returns the keys, numeric and
string, from the <parameter>input</parameter> array.
</para>
<para>
If the optional <parameter>search_value</parameter> is specified,
then only the keys for that value are returned. Otherwise, all
the keys from the <parameter>input</parameter> are returned.
</para>
<para>
<example>
<title><function>array_keys</function> example</title>
<programlisting role="php">
<![CDATA[
$array = array (0 => 100, "color" => "red");
print_r(array_keys ($array));
$array = array ("blue", "red", "green", "blue", "blue");
print_r(array_keys ($array, "blue"));
$array = array ("color" => array("blue", "red", "green"), "size" => array("small", "medium", "large"));
print_r(array_keys ($array));
]]>
</programlisting>
<para>
The printout of the program above will be:
<screen>
<![CDATA[
Array
(
[0] => 0
[1] => color
)
Array
(
[0] => 0
[1] => 3
[2] => 4
)
Array
(
[0] => color
[1] => size
)
]]>
</screen>
</para>
</example>
</para>
<note>
<para>
This function was added to PHP 4, below is an implementation for
those still using PHP 3.
<example>
<title>
Implementation of <function>array_keys</function> for PHP 3
users
</title>
<programlisting role="php">
<![CDATA[
function array_keys ($arr, $term="") {
$t = array();
while (list($k,$v) = each($arr)) {
if ($term && $v != $term) {
continue;
}
$t[] = $k;
}
return $t;
}
]]>
</programlisting>
</example>
</para>
</note>
<para>
See also <function>array_values</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-map">
<refnamediv>
<refname>array_map</refname>
<refpurpose>
Applies the callback to the elements of the given arrays
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_map</methodname>
<methodparam><type>mixed</type><parameter>callback</parameter></methodparam>
<methodparam><type>array</type><parameter>arr1</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>arr2...</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_map</function> returns an array containing all
the elements of <parameter>arr1</parameter> after applying the
callback function to each one. The number of parameters that the
callback function accepts should match the number of arrays
passed to the <function>array_map</function>
</para>
<para>
<example>
<title><function>array_map</function> example</title>
<programlisting role="php">
<![CDATA[
function cube($n) {
return $n*$n*$n;
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
]]>
</programlisting>
<para>
This makes <varname>$b</varname> have:
<screen>
<![CDATA[
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
]]>
</screen>
</para>
</example>
</para>
<para>
<example>
<title><function>array_map</function> - using more arrays</title>
<programlisting role="php">
<![CDATA[
function show_Spanish($n, $m) {
return "The number $n is called $m in Spanish";
}
function map_Spanish($n, $m) {
return array ($n => $m);
}
$a = array(1, 2, 3, 4, 5);
$b = array("uno", "dos", "tres", "cuatro", "cinco");
$c = array_map("show_Spanish", $a, $b);
print_r($c);
$d = array_map("map_Spanish", $a , $b);
print_r($d);
]]>
</programlisting>
<para>
This results:
<screen>
<![CDATA[
// printout of $c
Array
(
[0] => The number 1 is called uno in Spanish
[1] => The number 2 is called dos in Spanish
[2] => The number 3 is called tres in Spanish
[3] => The number 4 is called cuatro in Spanish
[4] => The number 5 is called cinco in Spanish
)
// printout of $d
Array
(
[0] => Array
(
[1] => uno
)
[1] => Array
(
[2] => dos
)
[2] => Array
(
[3] => tres
)
[3] => Array
(
[4] => cuatro
)
[4] => Array
(
[5] => cinco
)
)
]]>
</screen>
</para>
</example>
</para>
<para>
Usually when using two or more arrays, they should be of equal length
because the callback function is applied in parallel to the corresponding
elements.
If the arrays are of unequal length, the shortest one will be extended
with empty elements.
</para>
<para>
An interesting use of this function is to construct an array of arrays,
which can be easily performed by using &null;
as the name of the callback function
</para>
<para>
<example>
<title>Creating an array of arrays</title>
<programlisting role="php">
<![CDATA[
$a = array(1, 2, 3, 4, 5);
$b = array("one", "two", "three", "four", "five");
$c = array("uno", "dos", "tres", "cuatro", "cinco");
$d = array_map(null, $a, $b, $c);
print_r($d);
]]>
</programlisting>
</example>
</para>
<para>
The printout of the program above will be:
<screen>
<![CDATA[
Array
(
[0] => Array
(
[0] => 1
[1] => one
[2] => uno
)
[1] => Array
(
[0] => 2
[1] => two
[2] => dos
)
[2] => Array
(
[0] => 3
[1] => three
[2] => tres
)
[3] => Array
(
[0] => 4
[1] => four
[2] => cuatro
)
[4] => Array
(
[0] => 5
[1] => five
[2] => cinco
)
)
]]>
</screen>
</para>
<para>
See also <function>array_filter</function> and
<function>array_reduce</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-merge">
<refnamediv>
<refname>array_merge</refname>
<refpurpose>Merge two or more arrays</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_merge</methodname>
<methodparam><type>array</type><parameter>array1</parameter></methodparam>
<methodparam><type>array</type><parameter>array2</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>...</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_merge</function> merges the elements of two or
more arrays together so that the values of one are appended to
the end of the previous one. It returns the resulting array.
</para>
<para>
If the input arrays have the same string keys, then the later
value for that key will overwrite the previous one. If, however,
the arrays have the same numeric key, the later value will not
overwrite the original value, but will be appended.
</para>
<para>
<example>
<title><function>array_merge</function> example</title>
<programlisting role="php">
<![CDATA[
$array1 = array ("color" => "red", 2, 4);
$array2 = array ("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge ($array1, $array2);
]]>
</programlisting>
<para>
The <literal>$result</literal> will be:
<screen role="php">
<![CDATA[
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
]]>
</screen>
</para>
</example>
</para>
<para>
See also <function>array_merge_recursive</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-merge-recursive">
<refnamediv>
<refname>array_merge_recursive</refname>
<refpurpose>Merge two or more arrays recursively</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_merge_recursive</methodname>
<methodparam><type>array</type><parameter>array1</parameter></methodparam>
<methodparam><type>array</type><parameter>array2</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>...</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_merge_recursive</function> merges the elements of
two or more arrays together so that the values of one are appended
to the end of the previous one. It returns the resulting array.
</para>
<para>
If the input arrays have the same string keys, then the values for
these keys are merged together into an array, and this is done
recursively, so that if one of the values is an array itself, the
function will merge it with a corresponding entry in another array
too. If, however, the arrays have the same numeric key, the later
value will not overwrite the original value, but will be appended.
</para>
<para>
<example>
<title><function>array_merge_recursive</function> example</title>
<programlisting role="php">
<![CDATA[
$ar1 = array ("color" => array ("favorite" => "red"), 5);
$ar2 = array (10, "color" => array ("favorite" => "green", "blue"));
$result = array_merge_recursive ($ar1, $ar2);
]]>
</programlisting>
<para>
The <literal>$result</literal> will be:
<screen role="php">
<![CDATA[
Array
(
[color] => Array
(
[favorite] => Array
(
[0] => red
[1] => green
)
[0] => blue
)
[0] => 5
[1] => 10
)
]]>
</screen>
</para>
</example>
</para>
<para>
See also <function>array_merge</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-multisort">
<refnamediv>
<refname>array_multisort</refname>
<refpurpose>Sort multiple or multi-dimensional arrays</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>bool</type><methodname>array_multisort</methodname>
<methodparam><type>array</type><parameter>ar1</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>arg</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>...</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>...</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_multisort</function> can be used to sort several
arrays at once or a multi-dimensional array according by one of
more dimensions. It maintains key association when sorting.
</para>
<para>
The input arrays are treated as columns of a table to be sorted
by rows - this resembles the functionality of SQL ORDER BY
clause. The first array is the primary one to sort by. The rows
(values) in that array that compare the same are sorted by the
next input array, and so on.
</para>
<para>
The argument structure of this function is a bit unusual, but
flexible. The very first argument has to be an
array. Subsequently, each argument can be either an array or a
sorting flag from the following lists.
</para>
<para>
Sorting order flags:
<itemizedlist>
<listitem>
<simpara>SORT_ASC - sort in ascending order</simpara>
</listitem>
<listitem>
<simpara>SORT_DESC - sort in descending order</simpara>
</listitem>
</itemizedlist>
</para>
<para>
Sorting type flags:
<itemizedlist>
<listitem>
<simpara>SORT_REGULAR - compare items normally</simpara>
</listitem>
<listitem>
<simpara>SORT_NUMERIC - compare items numerically</simpara>
</listitem>
<listitem>
<simpara>SORT_STRING - compare items as strings</simpara>
</listitem>
</itemizedlist>
</para>
<para>
No two sorting flags of the same type can be specified after each
array. The sorting flags specified after an array argument apply
only to that array - they are reset to default SORT_ASC and
SORT_REGULAR after before each new array argument.
</para>
<para>
Returns &true; on success, &false;
on failure.
</para>
<para>
<example>
<title>Sorting multiple arrays</title>
<programlisting role="php">
<![CDATA[
$ar1 = array ("10", 100, 100, "a");
$ar2 = array (1, 3, "2", 1);
array_multisort ($ar1, $ar2);
]]>
</programlisting>
</example>
</para>
<para>
In this example, after sorting, the first array will contain 10,
"a", 100, 100. The second array will contain 1, 1, "2", 3. The
entries in the second array corresponding to the identical
entries in the first array (100 and 100) were sorted as well.
</para>
<para>
<example>
<title>Sorting multi-dimensional array</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>
<para>
In this example, after sorting, the first array will contain 10,
100, 100, "a" (it was sorted as strings in ascending order), and
the second one will contain 1, 3, "2", 1 (sorted as numbers, in
descending order).
</para>
</refsect1>
</refentry>
<refentry id="function.array-pad">
<refnamediv>
<refname>array_pad</refname>
<refpurpose>
Pad array to the specified length with a value
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_pad</methodname>
<methodparam><type>array</type><parameter>input</parameter></methodparam>
<methodparam><type>int</type><parameter>pad_size</parameter></methodparam>
<methodparam><type>mixed</type><parameter>pad_value</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_pad</function> returns a copy of the
<parameter>input</parameter> padded to size specified by
<parameter>pad_size</parameter> with value
<parameter>pad_value</parameter>. If
<parameter>pad_size</parameter> is positive then the array is
padded on the right, if it's negative then on the left. If the
absolute value of <parameter>pad_size</parameter> is less than or
equal to the length of the <parameter>input</parameter> then no
padding takes place.
</para>
<para>
<example>
<title><function>array_pad</function> example</title>
<programlisting role="php">
<![CDATA[
$input = array (12, 10, 9);
$result = array_pad ($input, 5, 0);
// result is array (12, 10, 9, 0, 0)
$result = array_pad ($input, -7, -1);
// result is array (-1, -1, -1, -1, 12, 10, 9)
$result = array_pad ($input, 2, "noop");
// not padded
]]>
</programlisting>
</example>
</para>
</refsect1>
</refentry>
<refentry id="function.array-pop">
<refnamediv>
<refname>array_pop</refname>
<refpurpose>Pop the element off the end of array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>mixed</type><methodname>array_pop</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_pop</function> pops and returns the last value of
the <parameter>array</parameter>, shortening the
<parameter>array</parameter> by one element.
If <parameter>array</parameter> is empty (or is not an array),
&null; will be returned.
</para>
<para>
<example>
<title><function>array_pop</function> example</title>
<programlisting role="php">
<![CDATA[
$stack = array ("orange", "banana", "apple", "raspberry");
$fruit = array_pop ($stack);
]]>
</programlisting>
<para>
After this, <varname>$stack</varname> will have only 3 elements:
<screen role="php">
<![CDATA[
Array
(
[0] => orange
[1] => banana
[2] => apple
)
]]>
</screen>
and <literal>rasberry</literal> will be assigned to
<varname>$fruit</varname>.
</para>
</example>
</para>
&return.falseproblem;
<para>
See also <function>array_push</function>,
<function>array_shift</function>, and
<function>array_unshift</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-push">
<refnamediv>
<refname>array_push</refname>
<refpurpose>
Push one or more elements onto the end of array
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>array_push</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam><type>mixed</type><parameter>var</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>...</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_push</function> treats
<parameter>array</parameter> as a stack, and pushes the passed
variables onto the end of <parameter>array</parameter>. The
length of <parameter>array</parameter> increases by the number of
variables pushed. Has the same effect as:
<programlisting role="php">
<![CDATA[
$array[] = $var;
]]>
</programlisting>
repeated for each <parameter>var</parameter>.
</para>
<para>
Returns the new number of elements in the array.
</para>
<para>
<example>
<title><function>array_push</function> example</title>
<programlisting role="php">
<![CDATA[
$stack = array ("orange", "banana");
array_push ($stack, "apple", "raspberry");
]]>
</programlisting>
<para>
This example would result in <varname>$stack</varname> having
the following elements:
<screen role="php">
<![CDATA[
Array
(
[0] => orange
[1] => banana
[2] => apple
[3] => raspberry
)
]]>
</screen>
</para>
</example>
</para>
<para>
See also <function>array_pop</function>,
<function>array_shift</function>, and
<function>array_unshift</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-rand">
<refnamediv>
<refname>array_rand</refname>
<refpurpose>
Pick one or more random entries out of an array
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>mixed</type><methodname>array_rand</methodname>
<methodparam><type>array</type><parameter>input</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>num_req</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_rand</function> is rather useful when you want to
pick one or more random entries out of an array. It takes an
<parameter>input</parameter> array and an optional argument
<parameter>num_req</parameter> which specifies how many entries you
want to pick - if not specified, it defaults to 1.
</para>
<para>
If you are picking only one entry, <function>array_rand</function>
returns the key for a random entry. Otherwise, it returns an array
of keys for the random entries. This is done so that you can pick
random keys as well as values out of the array.
</para>
<para>
Don't forget to call <function>srand</function> to seed the random
number generator.
</para>
<para>
<example>
<title><function>array_rand</function> example</title>
<programlisting role="php">
<![CDATA[
srand ((float) microtime() * 10000000);
$input = array ("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand ($input, 2);
print $input[$rand_keys[0]]."\n";
print $input[$rand_keys[1]]."\n";
]]>
</programlisting>
</example>
</para>
</refsect1>
</refentry>
<refentry id="function.array-reverse">
<refnamediv>
<refname>array_reverse</refname>
<refpurpose>
Return an array with elements in reverse order
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_reverse</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>preserve_keys</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_reverse</function> takes input
<parameter>array</parameter> and returns a new array with the
order of the elements reversed, preserving the keys if
<parameter>preserve_keys</parameter> is &true;.
</para>
<para>
<example>
<title><function>array_reverse</function> example</title>
<programlisting role="php">
<![CDATA[
$input = array ("php", 4.0, array ("green", "red"));
$result = array_reverse ($input);
$result_keyed = array_reverse ($input, TRUE);
]]>
</programlisting>
<para>
This makes both <varname>$result</varname> and
<varname>$result_keyed</varname> have the same elements, but
note the difference between the keys. The printout of
<varname>$result</varname> and
<varname>$result_keyed</varname> will be:
<screen role="php">
<![CDATA[
Array
(
[0] => Array
(
[0] => green
[1] => red
)
[1] => 4
[2] => php
)
Array
(
[2] => Array
(
[0] => green
[1] => red
)
[1] => 4
[0] => php
)
]]>
</screen>
</para>
</example>
</para>
<note>
<para>
The second parameter was added in PHP 4.0.3.
</para>
</note>
</refsect1>
</refentry>
<refentry id="function.array-reduce">
<refnamediv>
<refname>array_reduce</refname>
<refpurpose>
Iteratively reduce the array to a single value using a callback
function
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>mixed</type><methodname>array_reduce</methodname>
<methodparam><type>array</type><parameter>input</parameter></methodparam>
<methodparam><type>mixed</type><parameter>callback</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>initial</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_reduce</function> applies iteratively the
<parameter>callback</parameter> function to the elements of the
array <parameter>input</parameter>, so as to reduce the array to
a single value. If the optional <parameter>initial</parameter> is
available, it will be used at the beginning of the process, or as
a final result in case the array is empty.
</para>
<para>
<example>
<title><function>array_reduce</function> example</title>
<programlisting role="php">
<![CDATA[
function rsum($v, $w) {
$v += $w;
return $v;
}
function rmul($v, $w) {
$v *= $w;
return $v;
}
$a = array(1, 2, 3, 4, 5);
$x = array();
$b = array_reduce($a, "rsum");
$c = array_reduce($a, "rmul", 10);
$d = array_reduce($x, "rsum", 1);
]]>
</programlisting>
</example>
</para>
<para>
This will result in <varname>$b</varname> containing
<literal>15</literal>, <varname>$c</varname> containing
<literal>1200</literal> (= 1*2*3*4*5*10), and
<varname>$d</varname> containing <literal>1</literal>.
</para>
<para>
See also <function>array_filter</function> and
<function>array_map</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-shift">
<refnamediv>
<refname>array_shift</refname>
<refpurpose>
Shift an element off the beginning of array
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>mixed</type><methodname>array_shift</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_shift</function> shifts the first value of the
<parameter>array</parameter> off and returns it, shortening the
<parameter>array</parameter> by one element and moving everything
down. If <parameter>array</parameter> is empty (or is not an
array), &null; will be returned.
</para>
<para>
<example>
<title><function>array_shift</function> example</title>
<programlisting role="php">
<![CDATA[
$stack = array ("orange", "banana", "apple", "raspberry");
$fruit = array_shift ($stack);
]]>
</programlisting>
<para>
This would result in <varname>$stack</varname> having 3 elements left:
<screen role="php">
<![CDATA[
Array
(
[0] => banana
[1] => apple
[2] => raspberry
)
]]>
</screen>
and <literal>orange</literal> will be assigned to
<varname>$fruit</varname>.
</para>
</example>
</para>
<para>
See also <function>array_unshift</function>,
<function>array_push</function>, and
<function>array_pop</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-slice">
<refnamediv>
<refname>array_slice</refname>
<refpurpose>Extract a slice of the array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_slice</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam><type>int</type><parameter>offset</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>
length
</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_slice</function> returns the sequence of elements
from the array <parameter>array</parameter> as specified by the
<parameter>offset</parameter> and <parameter>length</parameter>
parameters.
</para>
<para>
If <parameter>offset</parameter> is positive, the sequence will
start at that offset in the <parameter>array</parameter>. If
<parameter>offset</parameter> is negative, the sequence will
start that far from the end of the <parameter>array</parameter>.
</para>
<para>
If <parameter>length</parameter> is given and is positive, then
the sequence will have that many elements in it. If
<parameter>length</parameter> is given and is negative then the
sequence will stop that many elements from the end of the
array. If it is omitted, then the sequence will have everything
from <parameter>offset</parameter> up until the end of the
<parameter>array</parameter>.
</para>
<para>
Note that <function>array_slice</function> will ignore array
keys, and will calculate offsets and lengths based on the
actual positions of elements within the array.
</para>
<para>
<example>
<title><function>array_slice</function> examples</title>
<programlisting role="php">
<![CDATA[
$input = array ("a", "b", "c", "d", "e");
$output = array_slice ($input, 2); // returns "c", "d", and "e"
$output = array_slice ($input, 2, -1); // returns "c", "d"
$output = array_slice ($input, -2, 1); // returns "d"
$output = array_slice ($input, 0, 3); // returns "a", "b", and "c"
]]>
</programlisting>
</example>
</para>
<para>
See also <function>array_splice</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-splice">
<refnamediv>
<refname>array_splice</refname>
<refpurpose>
Remove a portion of the array and replace it with something
else
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_splice</methodname>
<methodparam><type>array</type><parameter>input</parameter></methodparam>
<methodparam><type>int</type><parameter>offset</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>length</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>
replacement
</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_splice</function> removes the elements designated
by <parameter>offset</parameter> and
<parameter>length</parameter> from the
<parameter>input</parameter> array, and replaces them with the
elements of the <parameter>replacement</parameter> array, if
supplied. It returns an array containing the extracted elements.
</para>
<para>
If <parameter>offset</parameter> is positive then the start of
removed portion is at that offset from the beginning of the
<parameter>input</parameter> array. If
<parameter>offset</parameter> is negative then it starts that far
from the end of the <parameter>input</parameter> array.
</para>
<para>
If <parameter>length</parameter> is omitted, removes everything
from <parameter>offset</parameter> to the end of the array. If
<parameter>length</parameter> is specified and is positive, then
that many elements will be removed. If
<parameter>length</parameter> is specified and is negative then
the end of the removed portion will be that many elements from
the end of the array. Tip: to remove everything from
<parameter>offset</parameter> to the end of the array when
<parameter>replacement</parameter> is also specified, use
<literal>count($input)</literal> for
<parameter>length</parameter>.
</para>
<para>
If <parameter>replacement</parameter> array is specified, then
the removed elements are replaced with elements from this array.
If <parameter>offset</parameter> and
<parameter>length</parameter> are such that nothing is removed,
then the elements from the <parameter>replacement</parameter>
array are inserted in the place specified by the
<parameter>offset</parameter>. Tip: if the replacement is just
one element it is not necessary to put <literal>array()</literal>
around it, unless the element is an array itself.
</para>
<para>
The following equivalences hold:
<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>
Returns the array consisting of removed elements.
</para>
<para>
<example>
<title><function>array_splice</function> examples</title>
<programlisting role="php">
<![CDATA[
$input = array ("red", "green", "blue", "yellow");
array_splice ($input, 2);
// $input is now array ("red", "green")
$input = array ("red", "green", "blue", "yellow");
array_splice ($input, 1, -1);
// $input is now array ("red", "yellow")
$input = array ("red", "green", "blue", "yellow");
array_splice ($input, 1, count($input), "orange");
// $input is now array ("red", "orange")
$input = array ("red", "green", "blue", "yellow");
array_splice ($input, -1, 1, array("black", "maroon"));
// $input is now array ("red", "green",
// "blue", "black", "maroon")
]]>
</programlisting>
</example>
</para>
<para>
See also <function>array_slice</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-sum">
<refnamediv>
<refname>array_sum</refname>
<refpurpose>
Calculate the sum of values in an array.
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>mixed</type><methodname>array_sum</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_sum</function> returns the sum of values
in an array as an integer or float.
</para>
<para>
<example>
<title><function>array_sum</function> examples</title>
<programlisting role="php">
<![CDATA[
$a = array(2, 4, 6, 8);
echo "sum(a) = ".array_sum($a)."\n";
$b = array("a"=>1.2,"b"=>2.3,"c"=>3.4);
echo "sum(b) = ".array_sum($b)."\n";
]]>
</programlisting>
<para>
The printout of the program above will be:
<screen role="php">
<![CDATA[
sum(a) = 20
sum(b) = 6.9
]]>
</screen>
</para>
</example>
</para>
<note>
<para>
PHP versions prior to 4.0.6 modified the passed array
itself and converted strings to numbers (which most
of the time converted them to zero, depending on
their value).
</para>
</note>
</refsect1>
</refentry>
<refentry id="function.array-unique">
<refnamediv>
<refname>array_unique</refname>
<refpurpose>Removes duplicate values from an array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_unique</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_unique</function> takes input
<parameter>array</parameter> and returns a new array
without duplicate values.
</para>
<para>
Note that keys are preserved. <function>array_unique</function> sorts
the values treated as string at first, then will keep the first key
encountered for every value, and ignore all following keys. It does not
mean that the key of the first related value from the unsorted
<parameter>array</parameter> will be kept.
</para>
<note>
<simpara>
Two elements are considered equal if and only if
<literal>(string) $elem1 === (string) $elem2</literal>. In words:
when the string representation is the same.
<!-- TODO: example of it... -->
</simpara>
<simpara>
The first element will be used.
</simpara>
</note>
<warning>
<simpara>
This was broken in PHP 4.0.4!
<!-- TODO: when exactly was this broken?... -->
</simpara>
</warning>
<para>
<example>
<title><function>array_unique</function> example</title>
<programlisting role="php">
<![CDATA[
$input = array ("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique ($input);
print_r($result);
]]>
</programlisting>
<para>
This will output:
<screen role="php">
<![CDATA[
Array
(
[b] => green
[1] => blue
[2] => red
)
]]>
</screen>
</para>
</example>
</para>
<para>
<example>
<title><function>array_unique</function> and types</title>
<programlisting role="php">
<![CDATA[
$input = array (4,"4","3",4,3,"3");
$result = array_unique ($input);
var_dump($result);
]]>
</programlisting>
<para>
The printout of the program above will be (PHP 4.0.6):
<screen role="php">
<![CDATA[
array(2) {
[3]=>
int(4)
[4]=>
int(3)
}
]]>
</screen>
</para>
</example>
</para>
</refsect1>
</refentry>
<refentry id="function.array-unshift">
<refnamediv>
<refname>array_unshift</refname>
<refpurpose>
Prepend one or more elements to the beginning of array
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>array_unshift</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam><type>mixed</type><parameter>var</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>
...
</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_unshift</function> prepends passed elements to
the front of the <parameter>array</parameter>. Note that the list
of elements is prepended as a whole, so that the prepended
elements stay in the same order.
</para>
<para>
Returns the new number of elements in the
<parameter>array</parameter>.
</para>
<para>
<example>
<title><function>array_unshift</function> example</title>
<programlisting role="php">
<![CDATA[
$queue = array ("orange", "banana");
array_unshift ($queue, "apple", "raspberry");
]]>
</programlisting>
<para>
This would result in <varname>$queue</varname> having the
following elements:
<screen role="php">
<![CDATA[
Array
(
[0] => apple
[1] => raspberry
[2] => orange
[3] => banana
)
]]>
</screen>
</para>
</example>
</para>
<para>
See also <function>array_shift</function>,
<function>array_push</function>, and
<function>array_pop</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-values">
<refnamediv>
<refname>array_values</refname>
<refpurpose>Return all the values of an array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>array_values</methodname>
<methodparam><type>array</type><parameter>input</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_values</function> returns all the values from the
<parameter>input</parameter> array.
</para>
<para>
<example>
<title><function>array_values</function> example</title>
<programlisting role="php">
<![CDATA[
$array = array ("size" => "XL", "color" => "gold");
print_r(array_values ($array));
]]>
</programlisting>
<para>
This will output:
<screen role="php">
<![CDATA[
Array
(
[0] => XL
[1] => gold
)
]]>
</screen>
</para>
</example>
</para>
<note>
<para>
This function was added to PHP 4, below is an implementation for
those still using PHP 3.
<example>
<title>
Implementation of <function>array_values</function> for PHP 3
users
</title>
<programlisting role="php">
<![CDATA[
function array_values ($arr) {
$t = array();
while (list($k, $v) = each ($arr)) {
$t[] = $v;
}
return $t;
}
]]>
</programlisting>
</example>
</para>
</note>
<para>
See also <function>array_keys</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-walk">
<refnamediv>
<refname>array_walk</refname>
<refpurpose>
Apply a user function to every member of an array
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>array_walk</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam><type>string</type><parameter>func</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>userdata</parameter></methodparam>
</methodsynopsis>
<simpara>
Applies the user-defined function named by <parameter>func</parameter>
to each element of <parameter>array</parameter>.
<parameter>func</parameter> will be passed array value as the
first parameter and array key as the second parameter. If
<parameter>userdata</parameter> is supplied, it will be passed as
the third parameter to the user function. <parameter>func</parameter>
must be a user-defined function, and can't be a native PHP function.
Thus, you can't use <function>array_walk</function> straight with
<function>str2lower</function>, you must build a user-defined function
with it first, and pass this function as argument.
</simpara>
¬e.func-callback;
<simpara>
If <parameter>func</parameter> requires more than two or three
arguments, depending on <parameter>userdata</parameter>, a
warning will be generated each time
<function>array_walk</function> calls
<parameter>func</parameter>. These warnings may be suppressed by
prepending the '@' sign to the <function>array_walk</function>
call, or by using <function>error_reporting</function>.
</simpara>
<note>
<para>
If <parameter>func</parameter> needs to be working with the
actual values of the array, specify that the first parameter of
<parameter>func</parameter> should be passed by reference. Then
any changes made to those elements will be made in the array
itself.
</para>
<para>
Modifying the array from inside <parameter>func</parameter>
may cause unpredictable behavior.
</para>
</note>
<note>
<para>
Passing the key and userdata to <parameter>func</parameter> was
added in 4.0.
</para>
<para>
In PHP 4 <function>reset</function> needs to be called as
necessary since <function>array_walk</function> does not reset
the array by default.
</para>
</note>
<para>
<example>
<title><function>array_walk</function> example</title>
<programlisting role="php">
<![CDATA[
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
function test_alter (&$item1, $key, $prefix) {
$item1 = "$prefix: $item1";
}
function test_print ($item2, $key) {
echo "$key. $item2<br>\n";
}
echo "Before ...:\n";
array_walk ($fruits, 'test_print');
reset ($fruits);
array_walk ($fruits, 'test_alter', 'fruit');
echo "... and after:\n";
reset ($fruits);
array_walk ($fruits, 'test_print');
]]>
</programlisting>
<para>
The printout of the program above will be:
<screen role="php">
<![CDATA[
Before ...:
d. lemon
a. orange
b. banana
c. apple
... and after:
d. fruit: lemon
a. fruit: orange
b. fruit: banana
c. fruit: apple
]]>
</screen>
</para>
</example>
</para>
<simpara>
See also <function>each</function> and <function>list</function>.
</simpara>
</refsect1>
</refentry>
<refentry id="function.arsort">
<refnamediv>
<refname>arsort</refname>
<refpurpose>
Sort an array in reverse order and maintain index association
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>void</type><methodname>arsort</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>sort_flags</parameter></methodparam>
</methodsynopsis>
<para>
This function sorts an array such that array indices maintain
their correlation with the array elements they are associated
with. This is used mainly when sorting associative arrays where
the actual element order is significant.
</para>
<example>
<title><function>arsort</function> example</title>
<programlisting role="php">
<![CDATA[
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
arsort ($fruits);
reset ($fruits);
while (list ($key, $val) = each ($fruits)) {
echo "$key = $val\n";
}
]]>
</programlisting>
<para>
This example would display:
<screen>
<![CDATA[
a = orange
d = lemon
b = banana
c = apple
]]>
</screen>
</para>
</example>
<para>
The fruits have been sorted in reverse alphabetical order, and
the index associated with each element has been maintained.
</para>
<para>
You may modify the behavior of the sort using the optional
parameter <parameter>sort_flags</parameter>, for details
see <function>sort</function>.
</para>
<para>
See also <function>asort</function>, <function>rsort</function>,
<function>ksort</function>, and <function>sort</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.asort">
<refnamediv>
<refname>asort</refname>
<refpurpose>Sort an array and maintain index association</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>void</type><methodname>asort</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>sort_flags</parameter></methodparam>
</methodsynopsis>
<para>
This function sorts an array such that array indices maintain
their correlation with the array elements they are associated
with. This is used mainly when sorting associative arrays where
the actual element order is significant.
</para>
<example>
<title><function>asort</function> example</title>
<programlisting role="php">
<![CDATA[
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
asort ($fruits);
reset ($fruits);
while (list ($key, $val) = each ($fruits)) {
echo "$key = $val\n";
}
]]>
</programlisting>
<para>
This example would display:
<screen>
<![CDATA[
c = apple
b = banana
d = lemon
a = orange
]]>
</screen>
</para>
</example>
<para>
The fruits have been sorted in alphabetical order, and the index
associated with each element has been maintained.
</para>
<para>
You may modify the behavior of the sort using the optional
parameter <parameter>sort_flags</parameter>, for details
see <function>sort</function>.
</para>
<para>
See also <function>arsort</function>, <function>rsort</function>,
<function>ksort</function>, and <function>sort</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.compact">
<refnamediv>
<refname>compact</refname>
<refpurpose>
Create array containing variables and their values
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>compact</methodname>
<methodparam><type>mixed</type><parameter>varname</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>...</parameter></methodparam>
</methodsynopsis>
<para>
<function>compact</function> takes a variable number of
parameters. Each parameter can be either a string containing the
name of the variable, or an array of variable names. The array
can contain other arrays of variable names inside it;
<function>compact</function> handles it recursively.
</para>
<para>
For each of these, <function>compact</function> looks for a
variable with that name in the current symbol table and adds it
to the output array such that the variable name becomes the key
and the contents of the variable become the value for that key.
In short, it does the opposite of <function>extract</function>.
It returns the output array with all the variables added to it.
</para>
<para>
Any strings that are not set will simply be skipped.
</para>
<para>
<example>
<title><function>compact</function> example</title>
<programlisting role="php">
<![CDATA[
$city = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";
$location_vars = array ("city", "state");
$result = compact ("event", "nothing_here", $location_vars);
]]>
</programlisting>
<para>
After this, <varname>$result</varname> will be:
<screen role="php">
<![CDATA[
Array
(
[event] => SIGGRAPH
[city] => San Francisco
[state] => CA
)
]]>
</screen>
</para>
</example>
</para>
<para>
See also <function>extract</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.count">
<refnamediv>
<refname>count</refname>
<refpurpose>Count elements in a variable</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>count</methodname>
<methodparam><type>mixed</type><parameter>var</parameter></methodparam>
</methodsynopsis>
<para>
Returns the number of elements in <parameter>var</parameter>,
which is typically an <type>array</type> (since anything else will have
one element).
</para>
<para>
If <parameter>var</parameter> is not an array, <literal>1</literal> will
be returned (exception: <literal>count(&null;)</literal> equals
<literal>0</literal>).
</para>
<warning>
<para>
<function>count</function> may return 0 for a variable that
isn't set, but it may also return 0 for a variable that has
been initialized with an empty array. Use
<function>isset</function> to test if a variable is set.
</para>
</warning>
<para>
Please see the <link linkend="language.types.array">Arrays</link>
section of the manual for a detailed explanation of how arrays
are implemented and used in PHP.
</para>
<para>
<example>
<title><function>count</function> example</title>
<programlisting role="php">
<!-- TODO: examples about count(null), count(false), count(object).. -->
<![CDATA[
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count ($a);
// $result == 3
$b[0] = 7;
$b[5] = 9;
$b[10] = 11;
$result = count ($b);
// $result == 3;
]]>
</programlisting>
</example>
</para>
<note>
<para>
The <function>sizeof</function> function is an
<link linkend="aliases">alias</link> for <function>count</function>.
</para>
</note>
<para>
See also <function>is_array</function>,
<function>isset</function>, and
<function>strlen</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.current">
<refnamediv>
<refname>current</refname>
<refpurpose>Return the current element in an array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>mixed</type><methodname>current</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<para>
Every array has an internal pointer to its "current" element,
which is initialized to the first element inserted into the
array.
</para>
<para>
The <function>current</function> function simply returns the
array element that's currently being pointed by the internal
pointer. It does not move the pointer in any way. If the
internal pointer points beyond the end of the elements list,
<function>current</function> returns &false;.
<warning>
<para>
If the array contains empty elements (0 or "", the empty
string) then this function will return &false;
for these elements as well. This makes it impossible to
determine if you are really at the end of the list in such
an array using <function>current</function>. To properly
traverse an array that may contain empty elements, use the
<function>each</function> function.
</para>
</warning>
</para>
<para>
See also <function>end</function>, <function>next</function>,
<function>prev</function>, and <function>reset</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.each">
<refnamediv>
<refname>each</refname>
<refpurpose>
Return the current key and value pair from an array and advance
the array cursor
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>each</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<para>
Returns the current key and value pair from the array
<parameter>array</parameter> and advances the array cursor. This
pair is returned in a four-element array, with the keys
<emphasis>0</emphasis>, <emphasis>1</emphasis>,
<emphasis>key</emphasis>, and
<emphasis>value</emphasis>. Elements <emphasis>0</emphasis> and
<emphasis>key</emphasis> contain the key name of the array
element, and <emphasis>1</emphasis> and
<emphasis>value</emphasis> contain the data.
</para>
<para>
If the internal pointer for the array points past the end of the
array contents, <function>each</function> returns
&false;.
</para>
<para>
<example>
<title><function>each</function> examples</title>
<programlisting role="php">
<![CDATA[
$foo = array ("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each ($foo);
]]>
</programlisting>
<para>
<varname>$bar</varname> now contains the following key/value
pairs:
<itemizedlist spacing="compact">
<listitem><simpara>0 => 0</simpara></listitem>
<listitem><simpara>1 => 'bob'</simpara></listitem>
<listitem><simpara>key => 0</simpara></listitem>
<listitem><simpara>value => 'bob'</simpara></listitem>
</itemizedlist>
<programlisting role="php">
<![CDATA[
$foo = array ("Robert" => "Bob", "Seppo" => "Sepi");
$bar = each ($foo);
]]>
</programlisting>
</para>
<para>
<varname>$bar</varname> now contains the following key/value
pairs:
<itemizedlist spacing="compact">
<listitem><simpara>0 => 'Robert'</simpara></listitem>
<listitem><simpara>1 => 'Bob'</simpara></listitem>
<listitem><simpara>key => 'Robert'</simpara></listitem>
<listitem><simpara>value => 'Bob'</simpara></listitem>
</itemizedlist>
</para>
</example>
</para>
<para>
<function>each</function> is typically used in conjunction with
<function>list</function> to traverse an array; for instance,
<varname>$HTTP_POST_VARS</varname>:
<example>
<title>
Traversing <varname>$HTTP_POST_VARS</varname> with
<function>each</function>
</title>
<programlisting role="php">
<![CDATA[
echo "Values submitted via POST method:<br>";
reset ($HTTP_POST_VARS);
while (list ($key, $val) = each ($HTTP_POST_VARS)) {
echo "$key => $val<br>";
}
]]>
</programlisting>
</example>
</para>
<para>
After <function>each</function> has executed, the array cursor
will be left on the next element of the array, or on the last
element if it hits the end of the array. You have to use
<function>reset</function> if you want to traverse the array
again using each.
</para>
<para>
See also <function>key</function>, <function>list</function>,
<function>current</function>, <function>reset</function>,
<function>next</function>, and <function>prev</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.end">
<refnamediv>
<refname>end</refname>
<refpurpose>
Set the internal pointer of an array to its last element
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>mixed</type><methodname>end</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<para>
<function>end</function> advances <parameter>array</parameter>'s
internal pointer to the last element, and returns that element.
</para>
<para>
See also <function>current</function>,
<function>each</function>,
<function>next</function>, and <function>reset</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.extract">
<refnamediv>
<refname>extract</refname>
<refpurpose>
Import variables into the current symbol table from an array
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>extract</methodname>
<methodparam><type>array</type><parameter>var_array</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>extract_type</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>prefix</parameter></methodparam>
</methodsynopsis>
<para>
This function is used to import variables from an array into the
current symbol table. It takes an associative array
<parameter>var_array</parameter> and treats keys as variable
names and values as variable values. For each key/value pair it
will create a variable in the current symbol table, subject to
<parameter>extract_type</parameter> and
<parameter>prefix</parameter> parameters.
</para>
<note>
<para>
Since version 4.0.5 this function returns the number of
variables extracted.
</para>
</note>
<note>
<para>
EXTR_IF_EXISTS and EXTR_PREFIX_IF_EXISTS was introduced in version 4.2.0.
</para>
</note>
<para>
<function>extract</function> checks each key to see whether it
constitutes a valid variable name and also for collisions with
existing variables in the symbol table. The way invalid/numeric
keys and collisions are treated is determined by
<parameter>extract_type</parameter>. It can be one of the
following values:
<variablelist>
<varlistentry>
<term>EXTR_OVERWRITE</term>
<listitem>
<simpara>
If there is a collision, overwrite the existing variable.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>EXTR_SKIP</term>
<listitem>
<simpara>
If there is a collision, don't overwrite the existing
variable.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>EXTR_PREFIX_SAME</term>
<listitem>
<simpara>If there is a collision, prefix the variable name with
<parameter>prefix</parameter>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>EXTR_PREFIX_ALL</term>
<listitem>
<simpara>
Prefix all variable names with
<parameter>prefix</parameter>. Since PHP 4.0.5 this includes
numeric ones as well.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>EXTR_PREFIX_INVALID</term>
<listitem>
<simpara>
Only prefix invalid/numeric variable names with
<parameter>prefix</parameter>. This flag was added in
PHP 4.0.5.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>EXTR_IF_EXISTS</term>
<listitem>
<simpara>
Only overwrite the variable if it already exists in the
current symbol table, otherwise do nothing. This is useful
for defining a list of valid variables and then extracting
only those variables you have defined out of $_REQUEST, for
example. This flag was added in PHP 4.2.0.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>EXTR_PREFIX_IF_EXISTS</term>
<listitem>
<simpara>
Only create prefixed variable names if the non-prefixed version
of the same variable exists in the current symbol table. This
flag was added in PHP 4.2.0.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</para>
<para>
If <parameter>extract_type</parameter> is not specified, it is
assumed to be EXTR_OVERWRITE.
</para>
<para>
Note that <parameter>prefix</parameter> is only required if
<parameter>extract_type</parameter> is EXTR_PREFIX_SAME,
EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If
the prefixed result is not a valid variable name, it is not
imported into the symbol table.
</para>
<para>
<function>extract</function> returns the number of variables
successfully imported into the symbol table.
</para>
<para>
A possible use for extract is to import into the symbol table
variables contained in an associative array returned by
<function>wddx_deserialize</function>.
</para>
<para>
<example>
<title><function>extract</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
/* Suppose that $var_array is an array returned from
wddx_deserialize */
$size = "large";
$var_array = array ("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract ($var_array, EXTR_PREFIX_SAME, "wddx");
print "$color, $size, $shape, $wddx_size\n";
?>
]]>
</programlisting>
</example>
</para>
<para>
The above example will produce:
<programlisting>
<![CDATA[
blue, large, sphere, medium
]]>
</programlisting>
</para>
<para>
The <varname>$size</varname> wasn't overwritten, because we
specified EXTR_PREFIX_SAME, which resulted in
<varname>$wddx_size</varname> being created. If EXTR_SKIP was
specified, then $wddx_size wouldn't even have been created.
EXTR_OVERWRITE would have caused <varname>$size</varname> to have
value "medium", and EXTR_PREFIX_ALL would result in new variables
being named <varname>$wddx_color</varname>,
<varname>$wddx_size</varname>, and
<varname>$wddx_shape</varname>.
</para>
<para>
You must use an associative array, a numerically indexed array
will not produce results unless you use EXTR_PREFIX_ALL or
EXTR_PREFIX_INVALID.
</para>
<para>
See also <function>compact</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.in-array">
<refnamediv>
<refname>in_array</refname>
<refpurpose>Return &true; if a value exists in an array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>bool</type><methodname>in_array</methodname>
<methodparam><type>mixed</type><parameter>needle</parameter></methodparam>
<methodparam><type>array</type><parameter>haystack</parameter></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>strict</parameter></methodparam>
</methodsynopsis>
<para>
Searches <parameter>haystack</parameter> for
<parameter>needle</parameter> and returns &true;
if it is found in the array, &false; otherwise.
</para>
<para>
If the third parameter <parameter>strict</parameter> is set to
&true; then the <function>in_array</function> function
will also check the <link linkend="language.types">types</link> of
the <parameter>needle</parameter> in the <parameter>haystack</parameter>.
</para>
<note>
<para>
If <parameter>needle</parameter> is a string, the comparison is done in
a case-sensitive manner.
</para>
</note>
<note>
<para>
In PHP versions before 4.2.0 <parameter>needle</parameter> was not
allowed to be an array.
</para>
</note>
<para>
<example>
<title><function>in_array</function> example</title>
<programlisting role="php">
<![CDATA[
$os = array ("Mac", "NT", "Irix", "Linux");
if (in_array ("Irix", $os)) {
print "Got Irix";
}
if (in_array ("mac", $os)) {
print "Got mac";
}
]]>
</programlisting>
<para>
The second condition fails because <function>in_array</function>
is case-sensitive, so the program above will display:
<screen role="php">
<![CDATA[
Got Irix
]]>
</screen>
</para>
</example>
</para>
<para>
<example>
<title><function>in_array</function> with strict example</title>
<programlisting role="php">
<![CDATA[
<?php
$a = array('1.10', 12.4, 1.13);
if (in_array('12.4', $a, TRUE))
echo "'12.4' found with strict check\n";
if (in_array(1.13, $a, TRUE))
echo "1.13 found with strict check\n";
?>
]]>
</programlisting>
<para>
This will display:
<screen role="php">
<![CDATA[
1.13 found with strict check
]]>
</screen>
</para>
</example>
</para>
<para>
<example>
<title><function>in_array</function> with an array as needle</title>
<programlisting role="php">
<![CDATA[
<?php
$a = array(array('p', 'h'), array('p', 'r'), 'o');
if (in_array(array ('p', 'h'), $a))
echo "'ph' is found\n";
if (in_array(array ('f', 'i'), $a))
echo "'fi' is not found\n";
if (in_array('o', $a))
echo "'o' is found\n";
?>
// This will output:
'ph' is found
'o' is found
]]>
</programlisting>
</example>
</para>
<para>
See also <function>array_search</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.array-search">
<refnamediv>
<refname>array_search</refname>
<refpurpose>
Searches the array for a given value and returns the
corresponding key if successful
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>mixed</type><methodname>array_search</methodname>
<methodparam><type>mixed</type><parameter>needle</parameter></methodparam>
<methodparam><type>array</type><parameter>haystack</parameter></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>strict</parameter></methodparam>
</methodsynopsis>
<para>
Searches <parameter>haystack</parameter> for
<parameter>needle</parameter> and returns the key if it is found in
the array, &false; otherwise.
</para>
<para>
If the optional third parameter <parameter>strict</parameter> is set to
&true; then the <function>array_search</function>
will also check the types of the <parameter>needle</parameter>
in the <parameter>haystack</parameter>.
</para>
&return.falseproblem;
<para>
See also <function>in_array</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.key">
<refnamediv>
<refname>key</refname>
<refpurpose>Fetch a key from an associative array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>mixed</type><methodname>key</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<para>
<function>key</function> returns the index element of the
current array position.
</para>
<para>
See also <function>current</function> and <function>next</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.krsort">
<refnamediv>
<refname>krsort</refname>
<refpurpose>Sort an array by key in reverse order</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>krsort</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>sort_flags</parameter></methodparam>
</methodsynopsis>
<para>
Sorts an array by key in reverse order, maintaining key to data
correlations. This is useful mainly for associative arrays.
<example>
<title><function>krsort</function> example</title>
<programlisting role="php">
<![CDATA[
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
krsort ($fruits);
reset ($fruits);
while (list ($key, $val) = each ($fruits)) {
echo "$key = $val\n";
}
]]>
</programlisting>
</example>
</para>
<para>
This example would display:
</para>
<para>
<screen>
<![CDATA[
d = lemon
c = apple
b = banana
a = orange
]]>
</screen>
</para>
<para>
You may modify the behavior of the sort using the optional
parameter <parameter>sort_flags</parameter>, for details
see <function>sort</function>.
</para>
<simpara>
See also <function>asort</function>, <function>arsort</function>,
<function>ksort</function>, <function>sort</function>,
<function>natsort</function>, and <function>rsort</function>.
</simpara>
</refsect1>
</refentry>
<refentry id="function.ksort">
<refnamediv>
<refname>ksort</refname>
<refpurpose>Sort an array by key</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>ksort</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>sort_flags</parameter></methodparam>
</methodsynopsis>
<para>
Sorts an array by key, maintaining key to data correlations. This
is useful mainly for associative arrays.
<example>
<title><function>ksort</function> example</title>
<programlisting role="php">
<![CDATA[
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort ($fruits);
reset ($fruits);
while (list ($key, $val) = each ($fruits)) {
echo "$key = $val\n";
}
]]>
</programlisting>
</example>
</para>
<para>
This example would display:
</para>
<para>
<screen>
<![CDATA[
a = orange
b = banana
c = apple
d = lemon
]]>
</screen>
</para>
<para>
You may modify the behavior of the sort using the optional
parameter <parameter>sort_flags</parameter>, for details
see <function>sort</function>.
</para>
<simpara>
See also <function>asort</function>, <function>arsort</function>,
<function>krsort</function>, <function>uksort</function>,
<function>sort</function>, <function>natsort</function>, and
<function>rsort</function>.
</simpara>
<note>
<para>
The second parameter was added in PHP 4.
</para>
</note>
</refsect1>
</refentry>
<refentry id="function.list">
<refnamediv>
<refname>list</refname>
<refpurpose>
Assign variables as if they were an array
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>void</type><methodname>list</methodname>
<methodparam rep="repeat"><type>mixed</type><parameter>...</parameter></methodparam>
</methodsynopsis>
<para>
Like <function>array</function>, this is not really a function,
but a language construct. <function>list</function> is used to
assign a list of variables in one operation.
<example>
<title><function>list</function> example</title>
<programlisting role="php">
<![CDATA[
<table>
<tr>
<th>Employee name</th>
<th>Salary</th>
</tr>
<?php
$result = mysql_query ("SELECT id, name, salary FROM employees",$conn);
while (list ($id, $name, $salary) = mysql_fetch_row ($result)) {
print (" <tr>\n".
" <td><a href=\"info.php?id=$id\">$name</a></td>\n".
" <td>$salary</td>\n".
" </tr>\n");
}
?>
</table>
]]>
</programlisting>
</example>
</para>
<para>
See also <function>each</function> and <function>array</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.natsort">
<refnamediv>
<refname>natsort</refname>
<refpurpose>
Sort an array using a "natural order" algorithm
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>void</type><methodname>natsort</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<para>
This function implements a sort algorithm that orders
alphanumeric strings in the way a human being would. This is
described as a "natural ordering". An example of the difference
between this algorithm and the regular computer string sorting
algorithms (used in <function>sort</function>) can be seen below:
</para>
<para>
<example>
<title><function>natsort</function> example</title>
<programlisting role="php">
<![CDATA[
$array1 = $array2 = array ("img12.png", "img10.png", "img2.png", "img1.png");
sort($array1);
echo "Standard sorting\n";
print_r($array1);
natsort($array2);
echo "\nNatural order sorting\n";
print_r($array2);
]]>
</programlisting>
</example>
</para>
<para>
The code above will generate the following output:
</para>
<para>
<screen>
<![CDATA[
Standard sorting
Array
(
[0] => img1.png
[1] => img10.png
[2] => img12.png
[3] => img2.png
)
Natural order sorting
Array
(
[3] => img1.png
[2] => img2.png
[1] => img10.png
[0] => img12.png
)
]]>
</screen>
For more information see: Martin Pool's <ulink
url="&url.strnatcmp;">Natural Order String Comparison</ulink>
page.
</para>
<para>
See also <function>natcasesort</function>,
<function>strnatcmp</function>, and
<function>strnatcasecmp</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.natcasesort">
<refnamediv>
<refname>natcasesort</refname>
<refpurpose>
Sort an array using a case insensitive "natural order" algorithm
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>void</type><methodname>natcasesort</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<para>
This function implements a sort algorithm that orders
alphanumeric strings in the way a human being would. This is
described as a "natural ordering".
</para>
<para>
<function>natcasesort</function> is a case insensitive version of
<function>natsort</function>. See <function>natsort</function>
for an example of the difference between this algorithm and the
regular computer string sorting algorithms.
</para>
<para>
For more information see: Martin Pool's <ulink
url="&url.strnatcmp;">Natural Order String Comparison</ulink>
page.
</para>
<para>
See also <function>sort</function>,
<function>natsort</function>,
<function>strnatcmp</function>, and
<function>strnatcasecmp</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.next">
<refnamediv>
<refname>next</refname>
<refpurpose>
Advance the internal array pointer of an array
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>mixed</type><methodname>next</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<para>
Returns the array element in the next place that's pointed by the
internal array pointer, or &false; if
there are no more elements.
</para>
<para>
<function>next</function> behaves like
<function>current</function>, with one difference. It advances
the internal array pointer one place forward before returning the
element. That means it returns the next array element and
advances the internal array pointer by one. If advancing the
internal array pointer results in going beyond the end of the
element list, <function>next</function> returns &false;.
<warning>
<para>
If the array contains empty elements, or elements that have a key
value of 0 then this function will return &false;
for these elements as well. To properly traverse an array which
may contain empty elements or elements with key values of 0 see the
<function>each</function> function.
</para>
</warning>
</para>
<para>
See also
<function>current</function>, <function>end</function>,
<function>prev</function>, and <function>reset</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.pos">
<refnamediv>
<refname>pos</refname>
<refpurpose>Get the current element from an array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>mixed</type><methodname>pos</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<simpara>
This is an <link linkend="aliases">alias</link>
for <function>current</function>.
</simpara>
<para>
See also
<function>end</function>, <function>next</function>,
<function>prev</function>, and <function>reset</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.prev">
<refnamediv>
<refname>prev</refname>
<refpurpose>Rewind the internal array pointer</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>mixed</type><methodname>prev</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<para>
Returns the array element in the previous place that's pointed by
the internal array pointer, or &false; if there are no more
elements.
<warning>
<para>
If the array contains empty elements then this function will
return &false; for these elements as well.
To properly traverse an array which may contain empty elements
see the <function>each</function> function.
</para>
</warning>
</para>
<para>
<function>prev</function> behaves just like
<function>next</function>, except it rewinds the internal array
pointer one place instead of advancing it.
</para>
<para>
See also <function>current</function>, <function>end</function>,
<function>next</function>, and <function>reset</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.range">
<refnamediv>
<refname>range</refname>
<refpurpose>
Create an array containing a range of elements
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>range</methodname>
<methodparam><type>mixed</type><parameter>low</parameter></methodparam>
<methodparam><type>mixed</type><parameter>high</parameter></methodparam>
</methodsynopsis>
<para>
<function>range</function> returns an array of elements from
<parameter>low</parameter> to <parameter>high</parameter>,
inclusive. If low > high, the sequence will be from high to low.
</para>
<example>
<title><function>range</function> examples</title>
<programlisting role="php">
<![CDATA[
foreach(range(0, 9) as $number) {
echo $number;
}
foreach(range('a', 'z') as $letter) {
echo $letter;
}
foreach(range('z', 'a') as $letter) {
echo $letter;
}
]]>
</programlisting>
</example>
<note>
<para>
Prior to version 4.1.0 the <function>range</function> function
only generated incrementing integer arrays. Support for
character sequences and decrementing arrays was added in 4.1.0.
</para>
<example>
<title>Simulating decrementing ranges and character sequences</title>
<programlisting role="php">
<![CDATA[
# array_reverse can be used to flip the order of a range
foreach(array_reverse(range(0,9)) as $number) {
echo $number;
}
# array_map() can be used to turn integers into characters using chr()
foreach(array_map('chr', range(ord('a'),ord('z'))) as $character) {
echo $character;
}
]]>
</programlisting>
</example>
</note>
<para>
See <function>shuffle</function> for another example of its use.
</para>
</refsect1>
</refentry>
<refentry id="function.reset">
<refnamediv>
<refname>reset</refname>
<refpurpose>
Set the internal pointer of an array to its first element
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>mixed</type><methodname>reset</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<para>
<function>reset</function> rewinds <parameter>array</parameter>'s
internal pointer to the first element.
</para>
<para>
<function>reset</function> returns the value of the first array
element.
</para>
<para>
See also <function>current</function>,
<function>each</function>, <function>next</function>,
and <function>prev</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.rsort">
<refnamediv>
<refname>rsort</refname>
<refpurpose>Sort an array in reverse order</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>void</type><methodname>rsort</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>sort_flags</parameter></methodparam>
</methodsynopsis>
<para>
This function sorts an array in reverse order (highest to lowest).
<example>
<title><function>rsort</function> example</title>
<programlisting role="php">
<![CDATA[
$fruits = array ("lemon", "orange", "banana", "apple");
rsort ($fruits);
reset ($fruits);
while (list ($key, $val) = each ($fruits)) {
echo "$key = $val\n";
}
]]>
</programlisting>
</example>
</para>
<para>
This example would display:
</para>
<para>
<screen>
<![CDATA[
0 = orange
1 = lemon
2 = banana
3 = apple
]]>
</screen>
</para>
<para>
The fruits have been sorted in reverse alphabetical order.
</para>
<para>
You may modify the behavior of the sort using the optional
parameter <parameter>sort_flags</parameter>, for details
see <function>sort</function>.
</para>
<para>
See also <function>arsort</function>,
<function>asort</function>, <function>ksort</function>,
<function>sort</function>, and <function>usort</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.shuffle">
<refnamediv>
<refname>shuffle</refname>
<refpurpose>Shuffle an array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>void</type><methodname>shuffle</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
</methodsynopsis>
<para>
This function shuffles (randomizes the order of the elements in)
an array. You must use <function>srand</function> to seed this
function.
<example>
<title><function>shuffle</function> example</title>
<programlisting role="php">
<![CDATA[
$numbers = range (1,20);
srand ((float)microtime()*1000000);
shuffle ($numbers);
while (list (, $number) = each ($numbers)) {
echo "$number ";
}
]]>
</programlisting>
</example>
</para>
<para>
See also <function>arsort</function>, <function>asort</function>,
<function>ksort</function>, <function>rsort</function>,
<function>sort</function>, and <function>usort</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.sizeof">
<refnamediv>
<refname>sizeof</refname>
<refpurpose>Get the number of elements in variable</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>sizeof</methodname>
<methodparam><type>mixed</type><parameter>var</parameter></methodparam>
</methodsynopsis>
<para>
The <function>sizeof</function> function is an
<link linkend="aliases">alias</link> for <function>count</function>.
</para>
<para>
See also <function>count</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.sort">
<refnamediv>
<refname>sort</refname>
<refpurpose>Sort an array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>void</type><methodname>sort</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>sort_flags</parameter></methodparam>
</methodsynopsis>
<para>
This function sorts an array. Elements will be arranged from
lowest to highest when this function has completed.
<example>
<title><function>sort</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
$fruits = array ("lemon", "orange", "banana", "apple");
sort ($fruits);
reset ($fruits);
while (list ($key, $val) = each ($fruits)) {
echo "fruits[".$key."] = ".$val."\n";
}
?>
]]>
</programlisting>
</example>
</para>
<para>
This example would display:
</para>
<para>
<screen>
<![CDATA[
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
]]>
</screen>
</para>
<para>
The fruits have been sorted in alphabetical order.
</para>
<para>
The optional second parameter <parameter>sort_flags</parameter>
may be used to modify the sorting behavior using these values:
</para>
<para>
Sorting type flags:
<itemizedlist>
<listitem>
<simpara>SORT_REGULAR - compare items normally</simpara>
</listitem>
<listitem>
<simpara>SORT_NUMERIC - compare items numerically</simpara>
</listitem>
<listitem>
<simpara>SORT_STRING - compare items as strings</simpara>
</listitem>
</itemizedlist>
</para>
<para>
See also <function>arsort</function>,
<function>asort</function>, <function>ksort</function>,
<function>natsort</function>, <function>natcasesort</function>,
<function>rsort</function>, <function>usort</function>,
<function>array_multisort</function>, and
<function>uksort</function>.
</para>
<note>
<para>
The second parameter was added in PHP 4.
</para>
</note>
</refsect1>
</refentry>
<refentry id="function.uasort">
<refnamediv>
<refname>uasort</refname>
<refpurpose>
Sort an array with a user-defined comparison function and
maintain index association
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>void</type><methodname>uasort</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam><type>function</type><parameter>cmp_function</parameter></methodparam>
</methodsynopsis>
<para>
This function sorts an array such that array indices maintain
their correlation with the array elements they are associated
with. This is used mainly when sorting associative arrays where
the actual element order is significant. The comparison function
is user-defined.
</para>
<note>
<para>
Please see <function>usort</function> and
<function>uksort</function> for examples of user-defined
comparison functions.
</para>
</note>
¬e.func-callback;
<para>
See also <function>usort</function>, <function>uksort</function>,
<function>sort</function>, <function>asort</function>,
<function>arsort</function>, <function>ksort</function>,
and <function>rsort</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.uksort">
<refnamediv>
<refname>uksort</refname>
<refpurpose>
Sort an array by keys using a user-defined comparison function
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>void</type><methodname>uksort</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam><type>function</type><parameter>cmp_function</parameter></methodparam>
</methodsynopsis>
<para>
This function will sort the keys of an array using a
user-supplied comparison function. If the array you wish to sort
needs to be sorted by some non-trivial criteria, you should use
this function.
</para>
<example>
<title><function>uksort</function> example</title>
<programlisting role="php">
<![CDATA[
function cmp ($a, $b) {
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}
$a = array (4 => "four", 3 => "three", 20 => "twenty", 10 => "ten");
uksort ($a, "cmp");
while (list ($key, $value) = each ($a)) {
echo "$key: $value\n";
}
]]>
</programlisting>
</example>
<para>
This example would display:
</para>
<para>
<screen>
<![CDATA[
20: twenty
10: ten
4: four
3: three
]]>
</screen>
</para>
¬e.func-callback;
<para>
See also <function>usort</function>, <function>uasort</function>,
<function>sort</function>, <function>asort</function>,
<function>arsort</function>, <function>ksort</function>,
<function>natsort</function>, and <function>rsort</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.usort">
<refnamediv>
<refname>usort</refname>
<refpurpose>
Sort an array by values using a user-defined comparison function
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>void</type><methodname>usort</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam><type>string</type><parameter>cmp_function</parameter></methodparam>
</methodsynopsis>
<para>
This function will sort an array by its values using a
user-supplied comparison function. If the array you wish to sort
needs to be sorted by some non-trivial criteria, you should use
this function.
</para>
<para>
The comparison function must return an integer less than, equal
to, or greater than zero if the first argument is considered to
be respectively less than, equal to, or greater than the
second. If two members compare as equal, their order in the
sorted array is undefined.
</para>
<para>
<example>
<title><function>usort</function> example</title>
<programlisting role="php">
<![CDATA[
function cmp ($a, $b) {
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}
$a = array (3, 2, 5, 6, 1);
usort ($a, "cmp");
while (list ($key, $value) = each ($a)) {
echo "$key: $value\n";
}
]]>
</programlisting>
</example>
</para>
<para>
This example would display:
</para>
<para>
<screen>
<![CDATA[
0: 6
1: 5
2: 3
3: 2
4: 1
]]>
</screen>
</para>
<note>
<para>
Obviously in this trivial case the <function>rsort</function>
function would be more appropriate.
</para>
</note>
<para>
<example>
<title>
<function>usort</function> example using multi-dimensional array
</title>
<programlisting role="php">
<![CDATA[
function cmp ($a, $b) {
return strcmp($a["fruit"], $b["fruit"]);
}
$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";
usort($fruits, "cmp");
while (list ($key, $value) = each ($fruits)) {
echo "\$fruits[$key]: " . $value["fruit"] . "\n";
}
]]>
</programlisting>
</example>
</para>
<para>
When sorting a multi-dimensional array, $a and $b contain
references to the first index of the array.
</para>
<para>
This example would display:
</para>
<para>
<screen>
<![CDATA[
$fruits[0]: apples
$fruits[1]: grapes
$fruits[2]: lemons
]]>
</screen>
</para>
¬e.func-callback;
<para>
<example>
<title>
<function>usort</function> example using a member function of an object
</title>
<programlisting role="php">
<![CDATA[
class TestObj {
var $name;
function TestObj($name)
{
$this->name = $name;
}
/* This is the static comparing function: */
function cmp_obj($a, $b)
{
$al = strtolower($a->name);
$bl = strtolower($b->name);
if ($al == $bl) return 0;
return ($al > $bl) ? +1 : -1;
}
}
$a[] = new TestObj("c");
$a[] = new TestObj("b");
$a[] = new TestObj("d");
uasort($a, array ("TestObj", "cmp_obj"));
foreach ($a as $item) {
print $item->name."\n";
}
]]>
</programlisting>
</example>
</para>
<para>
This example would display:
</para>
<para>
<screen>
b
c
d
</screen>
</para>
<warning>
<para>
The underlying quicksort function in some C libraries (such as
on Solaris systems) may cause PHP to crash if the comparison
function does not return consistent values.
</para>
</warning>
<para>
See also <function>uasort</function>,
<function>uksort</function>, <function>sort</function>,
<function>asort</function>,
<function>arsort</function>,<function>ksort</function>,
<function>natsort</function>, and <function>rsort</function>.
</para>
</refsect1>
</refentry>
</reference>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|