is_array( array $input )

Returns TRUE if argument is an array, FALSE otherwise.

example:
  	
			<?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'; ?>
Output :
Array
not an Array

array_diff (array $array1 , array $array2 [, 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.

example:
  	
			<?php
				$array1 = array("a" => "green", "red", "blue", "red");
				$array2 = array("b" => "green", "yellow", "red");
				$result = array_diff($array1, $array2);
				print_r($result);
			?>
	
Output :
Array ( [1] => blue )

array_flip (array $trans)

Returns an array in flip order, i.e. keys from become values and values become keys.

example:
  	
			<?php
				$trans = array("a" => 12, "b" => 23, "c" => 34);
				$trans = array_flip($trans);
				print_r($trans);
			?>
	
Output :
Array ( [12] => a [23] => b [34] => c )

array_key_exists (mixed $key ,array $search )

Returns TRUE if the given key is set in the array. key can be any value possible for an array index.

example:
  	
			<?php
				$search_array = array('first' => 1, 'second' => 4);
					if (array_key_exists('first', $search_array)) {
					echo "The 'first' element is in the array";
					}
			?>
	
Output :
The 'first' element is in the array

array_keys (array $input [, mixed $search_value = NULL] )

Returns the keys, numeric and string, from the input array.

example:
  	
			<?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")); ?>
Output :
Array ( [0] => 0 [1] => color ) Array ( [0] => 0 [1] => 3 [2] => 4 )

array array_merge ( array $array1 [, array $... ] )

* 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.

example:
  	
			<?php
				$array1 = array("color" => "red", 2, 4);
				$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
				$result = array_merge($array1, $array2);
				print_r($result);
			?>
	
Output :
Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 )

array_pad ( array $input , int $pad_size , mixed $pad_value )

Returns a copy of the input padded to size specified by pad_size with value pad_value.

example:
  	
			<?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 ?>
Output :
Array ( [0] => 12 [1] => 10 [2] => 9 [3] => 0 [4] => 0 ) Array ( [0] => -1 [1] => -1 [2] => -1 [3] => -1 [4] => 12 [5] => 10 [6] => 9 ) Array ( [0] => 12 [1] => 10 [2] => 9 )

array_rand (array $input)

Returns the key for a random entry.

example:
  	
			<?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";
			?>
	
Output :
biswa
abhijit

array_reverse ( array $array [, bool $preserve_keys = false ] )

Return an array with elements in reverse order.

example:
  	
			<?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);
			?>
	
Output :
Array ( [0] => Array ( [0] => green [1] => red ) [1] => 4 [2] => php ) Array ( [2] => Array ( [0] => green [1] => red ) [1] => 4 [0] => php )

array_search ( mixed $needle , array $haystack [, bool $strict = false ] )

Searches key for value and returns the key if it is found in the array, FALSE otherwise.

example:
  	
			<?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; ?>
Output :
2
1

array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] )

* 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.

example:
  	
			<?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));
			?>
	
Output :
Array ( [0] => c [1] => d ) Array ( [2] => c [3] => d )

array_sum ( array $array )

Returns the sum of values in an array as an integer or float.

example:
  	
			<?php
				$a = array(2, 4, 6, 8);
				echo "sum(a) = " . array_sum($a);
			?>
	
Output :
sum(a) = 20

array_unique (array $array)

Takes input array and returns a new array without duplicate values.

example:
  	
			<?php
				$input = array("a" => "green", "red", "b" => "green", "blue", "red");
				$result = array_unique($input);
				print_r($result);
			?>
	
Output :
Array ( [a] => green [0] => red [1] => blue )

array_values ( array $input )

Returns all the values from the input array and indexes numerically the array.

example:
  	
			<?php
				$array = array("size" => "XL", "color" => "gold");
				print_r(array_values($array));
			?>
	
Output :
Array ( [0] => XL [1] => gold )

array_combine ( array $keys , array $values )

Returns the combined array, FALSE if the number of elements for each array isn't equal or if the arrays are empty.

example:
  	
			<?php
				$a = array('green', 'red', 'yellow');
				$b = array('avocado', 'apple', 'banana');
				$c = array_combine($a, $b);
				print_r($c);
			?>
	
Output :
Array ( [green] => avocado [red] => apple [yellow] => banana )

sort (array &array)

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.

example:
  	
			<?php
				$fruits = array("lemon", "orange", "banana", "apple");
				sort($fruits);
				foreach ($fruits as $key => $val) {
				echo "fruits[" . $key . "] = " . $val . "\n";
				}
			?>
	
Output :
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange

count (array &array)

Returns the number of elements in an array.

example:
  	
			<?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
				}
			?>
	
Output :
result=3
result=3
result=0
result=1

array_pop ( array &array )

Pop the element off the end of array.

example:
  	
			<?php
				$stack = array("orange", "banana", "apple", "raspberry");
				$fruit = array_pop($stack);
				print_r($stack);
			?>
	
Output :
Array ( [0] => orange [1] => banana [2] => apple )

array_push ()

Push one or more elements onto the end of array.

Syntax int array_push( array &$array,mixed $var[, mixed $... ]);
example:
  	
			<?php
				$stack = array("orange", "banana");
				array_push($stack, "apple", "raspberry");
				print_r($stack);
			?>
	
Output :
Array ( [0] => orange [1] => banana [2] => apple [3] => raspberry )

array_shift ()

Shift an element off the beginning of array.

Syntax mixed array_shift(array &$array);
example:
  	
			<?php
				$stack = array("orange", "banana", "apple", "raspberry");
				$fruit = array_shift($stack);
				print_r($stack);
			?>
	
Output :
Array ( [0] => banana [1] => apple [2] => raspberry )

array_unshift ()

Prepend one or more elements to the beginning of an array.

Syntax int array_unshift(array &$array , mixed $var[, mixed $... ]);
example:
  	
			<?php
				$queue = array("orange", "banana");
				array_unshift($queue, "apple", "raspberry");
				print_r($queue);
			?>
	
Output :
Array ( [0] => apple [1] => raspberry [2] => orange [3] => banana )

array_map()

Applies the callback to the elements of the given arrays.

Syntax array array_map (callback $callback , array $arr1 [, array $... ] );
example1:
  	
			<?php
				function cube($n)
						{
					return($n * $n * $n);
					}
					$a = array(1, 2, 3, 4, 5);
					$b = array_map("cube", $a);
					print_r($b);
			?>
	
Output :
Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 )
example2:
  	
<?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);
?>
	
Output :
Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 [4] => 7 )

in_array ( mixed $needle , array $haystack)

Searches array for value and returns TRUE if it is found in the array, FALSE otherwise.

example:
  	
			<?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";
				}
			?>
	
Output :
1.13 found with strict check

array_count_values ( array $input )

Counts number of times a value appears in an array .

example:
  	
			<?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";
			?>
	
Output :
1 exists 5 times
5 exists 4 times
2 exists 5 times
3 exists 4 times
4 exists 1 times

range ( mixed $start , mixed $end)

Create an array containing a range of elements .

example:
  	
			<?php
				foreach (range(0, 12) as $number) {
					echo $number.' ';
					}
			?>
	
Output :
0 1 2 3 4 5 6 7 8 9 10 11 12

unset($value)

Unset(delete) an element of an array .

example:
  	
			<?php
				$myArray = array("banana","apple","pear","banana");
				print_r($myArray);
				unset($myArray[1]);
				echo "
After unset:
"; print_r($myArray); ?>
Output :
Array ( [0] => banana [1] => apple [2] => pear [3] => banana )
After unset:
Array ( [0] => banana [2] => pear [3] => banana )

Questions

  1. How do I reverse the order of the elements in an array?
  2. How to create array of letters of the alphabet ? (ans : $arr = range(a,z))
  3. How to remove duplicate values from an array ?
  4. What is difference between array_combine() and array_merge()?
  5. How To Merge Values of Two Arrays into a Single Array?
  6. $array1 = array("a" => "green", "red", "blue", "red");
    $array2 = array("b" => "green", "yellow", "red");
    $result = array_diff($array2, $array1);
    What will be the result?
  7. $array1 = array("a" => "green", "red", "blue", "red");
    How to Convert keys to values and values to keys using predefined method?
  8. Give all the keys and values from array("a" => "green", "red", "blue", "red")?
  9. Difference between in_array() and array_search() ?
  10. What will be the outputs?
    $arr = array(
    array(
    "name"=>"ashok", "roll no"=>"890", "gender"=>"m"
    ),
    array(
    "name"=>"samar", "roll no"=>"880", "gender"=>"m"
    ),
    array(
    "name"=>"sameer", "roll no"=>"780", "gender"=>"m"
    )
    );
    $frr = array_flip($arr);
    print_r($frr);
    print_r(array_flip($arr[0]));
  11. Write code for making an sequence array whose min value is 30 and array size would be 40?
After editing Click here:
Output:
Hello World