Returns TRUE if argument is an array, FALSE otherwise.
<?php $yes = array('this', 'is', 'an array'); echo is_array($yes) ? 'Array' : 'not an Array'; echo ''; $no = 'this is a string'; echo '
'; echo is_array($no) ? 'Array' : 'not an Array'; ?>
* Compares array1 against array2 and returns the difference. * It returns the values that are present in the first array not in the second array. * It returns a blank array if all elements of first array are present in the second array.
<?php $array1 = array("a" => "green", "red", "blue", "red"); $array2 = array("b" => "green", "yellow", "red"); $result = array_diff($array1, $array2); print_r($result); ?>
Returns an array in flip order, i.e. keys from become values and values become keys.
<?php $trans = array("a" => 12, "b" => 23, "c" => 34); $trans = array_flip($trans); print_r($trans); ?>
Returns TRUE if the given key is set in the array. key can be any value possible for an array index.
<?php $search_array = array('first' => 1, 'second' => 4); if (array_key_exists('first', $search_array)) { echo "The 'first' element is in the array"; } ?>
Returns the keys, numeric and string, from the input array.
<?php $array = array(0 => 80, "color" => "red"); print_r(array_keys($array)); echo '
'; $array = array("blue", "red", "green", "blue", "blue"); print_r(array_keys($array, "blue")); ?>
* Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one.
* It returns the value of second array if the key of two arrays are same.
<?php $array1 = array("color" => "red", 2, 4); $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4); $result = array_merge($array1, $array2); print_r($result); ?>
Returns a copy of the input padded to size specified by pad_size with value pad_value.
<?php $input = array(12, 10, 9); $result = array_pad($input, 5, 0); print_r($result); echo '
'; // result is array(12, 10, 9, 0, 0) $result = array_pad($input, -7, -1); print_r($result); echo '
'; // result is array(-1, -1, -1, -1, 12, 10, 9) $result = array_pad($input, 2, "noop"); print_r($result); // not padded ?>
Returns the key for a random entry.
<?php $input = array("abhijit", "susanta", "sushil", "biswa"); $rand_keys = array_rand($input, 2); echo $input[$rand_keys[0]] . "\n"; echo $input[$rand_keys[1]] . "\n"; ?>
Return an array with elements in reverse order.
<?php $input = array("php", 4.0, array("green", "red")); $result = array_reverse($input); $result_keyed = array_reverse($input, true); print_r($result); print_r($result_keyed); ?>
Searches key for value and returns the key if it is found in the array, FALSE otherwise.
<?php $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red'); $key1 = array_search('green', $array); // $key = 2; print $key1; echo "
"; $key2 = array_search('red', $array); // $key = 1; print $key2; ?>
* Returns the sequence of elements from the array as specified by the offset and length parameters.
* If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.
* If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length 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 offset up until the end of the array.
<?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)); ?>
Returns the sum of values in an array as an integer or float.
<?php $a = array(2, 4, 6, 8); echo "sum(a) = " . array_sum($a); ?>
Takes input array and returns a new array without duplicate values.
<?php $input = array("a" => "green", "red", "b" => "green", "blue", "red"); $result = array_unique($input); print_r($result); ?>
Returns all the values from the input array and indexes numerically the array.
<?php $array = array("size" => "XL", "color" => "gold"); print_r(array_values($array)); ?>
Returns the combined array, FALSE if the number of elements for each array isn't equal or if the arrays are empty.
<?php $a = array('green', 'red', 'yellow'); $b = array('avocado', 'apple', 'banana'); $c = array_combine($a, $b); print_r($c); ?>
This function assigns new keys for the elements in array. It will remove any existing keys you may have assigned, rather than just reordering the keys.Returns TRUE on success or FALSE on failure.
<?php $fruits = array("lemon", "orange", "banana", "apple"); sort($fruits); foreach ($fruits as $key => $val) { echo "fruits[" . $key . "] = " . $val . "\n"; } ?>
Returns the number of elements in an array.
<?php $a[0] = 1; $a[1] = 3; $a[2] = 5; $result = count($a); print "result=".$result; // $result == 3 $b[0] = 7; $b[5] = 9; $b[10] = 11; $result = count($b); print "result=".$result; // $result == 3 $result = count(null); print "result=".$result; // $result == 0 $result = count(false); print "result=".$result; // $result == 1 } ?>
Pop the element off the end of array.
<?php $stack = array("orange", "banana", "apple", "raspberry"); $fruit = array_pop($stack); print_r($stack); ?>
Push one or more elements onto the end of array.
<?php $stack = array("orange", "banana"); array_push($stack, "apple", "raspberry"); print_r($stack); ?>
Shift an element off the beginning of array.
<?php $stack = array("orange", "banana", "apple", "raspberry"); $fruit = array_shift($stack); print_r($stack); ?>
Prepend one or more elements to the beginning of an array.
<?php $queue = array("orange", "banana"); array_unshift($queue, "apple", "raspberry"); print_r($queue); ?>
Applies the callback to the elements of the given arrays.
<?php function cube($n) { return($n * $n * $n); } $a = array(1, 2, 3, 4, 5); $b = array_map("cube", $a); print_r($b); ?>
<?php $array = array(1,2,3,4,5); class test { public $classarray; public function adding($data) { $this->classarray = array_map(array('test','dash'), $data); //test is the class name } public function dash($item) { $item2 = $item+2; return $item2; } } $test = new test(); $test->adding($array); print_r($test->classarray); ?>
Searches array for value and returns TRUE if it is found in the array, FALSE otherwise.
<?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"; } ?>
Counts number of times a value appears in an array .
<?php $myArray = array(1,1,5,1,2,2,2,2,3,3,4,2,5,1,3,5,3,1,5); $myArrayCount = array_count_values($myArray); foreach($myArrayCount as $key=>$value) echo $key." exists ".$value." times"; ?>
Create an array containing a range of elements .
<?php foreach (range(0, 12) as $number) { echo $number.' '; } ?>
Unset(delete) an element of an array .
<?php $myArray = array("banana","apple","pear","banana"); print_r($myArray); unset($myArray[1]); echo "
After unset:
"; print_r($myArray); ?>
After editing Click here: |
Output:
|
Hello World
|