89. ticks <?php // A function that records the time when it is called function profile($dump = FALSE) { static $profile; // Return the times stored in profile, then erase it if ($dump) { $temp = $profile; unset($profile); return $temp; } $profile[] = microtime(); } // Set up a tick handler register_tick_function("profile"); // Initialize the function before the declare block profile(); // Run a block of code, throw a tick every 2nd statement declare(ticks=2) { for ($x = 1; $x < 50; ++$x) { echo similar_text(md5($x), md5($x*$x)), "<br />;"; } } // Display the data stored in the profiler print_r(profile(TRUE)); ?>
105. array() <?php $firstquarter = array(1 => 'January', 'February', 'March'); print_r($firstquarter); ?> The above example will output: Array ( [1] => January [2] => February [3] => March ) 1-based index with array() <?php $foo = array('bar' => 'baz'); echo "Hello {$foo['bar']}!"; // Hello baz! ?>
106.
107.
108. rsort <?php $fruits = array("lemon", "orange", "banana", "apple"); rsort($fruits); foreach ($fruits as $key => $val) { echo "$key = $val"; } ?> The above example will output: 0 = orange 1 = lemon 2 = banana 3 = apple
109.
110. asort <?php $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); asort($fruits); foreach ($fruits as $key => $val) { echo "$key = $val"; } ?> The above example will output: c = apple b = banana d = lemon a = orange The fruits have been sorted in alphabetical order, and the index associated with each element has been maintained.
111.
112. arsort <?php $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); arsort($fruits); foreach ($fruits as $key => $val) { echo "$key = $val"; } ?> The above example will output: a = orange d = lemon b = banana c = apple The fruits have been sorted in reverse alphabetical order, and the index associated with each element has been maintained.
142. in_array <?php $os = array("Mac", "NT", "Irix", "Linux"); if (in_array("Irix", $os)) { echo "Got Irix"; } if (in_array("mac", $os)) { echo "Got mac"; } ?> The second condition fails because in_array() is case-sensitive, so the program above will display: Got Irix
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155. array_walk <?php $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 />"; } This example will output: Before ...: d. lemon a. orange b. banana c. apple ... and after: d. fruit: lemon a. fruit: orange b. fruit: banana c. fruit: apple echo "Before ...:"; array_walk($fruits, 'test_print'); array_walk($fruits, 'test_alter', 'fruit'); echo "... and after:"; array_walk($fruits, 'test_print'); ?>
156.
157. array_unique <?php $input = array("a" => "green", "red", "b" => "green", "blue", "red"); $result = array_unique($input); print_r($result); ?> The above example will output: Array ( [a] => green [0] => red [1] => blue ) <?php $input = array(4, "4", "3", 4, 3, "3"); $result = array_unique($input); var_dump($result); ?> The above example will output: array(2) { [0] => int(4) [2] => string(1) "3" }
158.
159.
160.
161.
162. array_reverse <?php $input = array("php", 4.0, array("green", "red")); $result = array_reverse($input); $result_keyed = array_reverse($input, true); ?> Array ( [2] => Array ( [0] => green [1] => red ) [1] => 4 [0] => php ) Array ( [0] => Array ( [0] => green [1] => red ) [1] => 4 [2] => php ) $result will be $result_keyed will be: This makes both $result and $result_keyed have the same elements, but note the difference between the keys.
163.
164.
165. array_shift <?php $stack = array("orange", "banana", "apple", "raspberry"); $fruit = array_shift($stack); print_r($stack); ?> This would result in $stack having 3 elements left: Array ( [0] => banana [1] => apple [2] => raspberry ) and orange will be assigned to $fruit.
166.
167.
168.
169. array_slice <?php $input = array("a", "b", "c", "d", "e"); $output = array_slice($input, 2); // returns "c", "d", and "e" $output = array_slice($input, -2, 1); // returns "d" $output = array_slice($input, 0, 3); // returns "a", "b", and "c" // note the differences in the array keys print_r(array_slice($input, 2, -1)); print_r(array_slice($input, 2, -1, true)); Array ( [0] => c [1] => d ) Array ( [2] => c [3] => d )