PHP Function: Array()

Arrays in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

array_diff( ) Difference of two arrays
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

OUTPUT Array ( [1] => blue )


array_count_values( ) counting the frequency of values inside an array

$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));

OUTPUT Array( [1] => 2 [hello] => 2 [world] => 1 )


count ( ) sizeof Array Size or length

$array = array ("ABC","DEF", "GHI", "KLM");
print count($array); // 4
$value= array(2,5,6,8,9);
echo "size of array = ".sizeof($value)."; // Output = 5
array_push( ) : Adding element to an Array
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);

OUTPUT Array( [0] => orange [1] => banana [2] => apple [3] => raspberry )


array_sum( ) : Array Sum of all elements

$a = array(2,4,6,8);
echo "sum(a) = " . array_sum($a) . "\n"; // sum(a) = 20
in_array ( ): IN Array to search for elements inside an array
Case sensitive; Checks if a value exists in an array
$value= array("Rabin","Reid","Chi","KVJ","John");
if(in_array("Chi",$value))
{
echo "Chi is there inside the array";
}
else
{
    echo " Chi is not there ";// chi is there inside the array
} 
array_search( ):Searches the array for a given value and returns the corresponding key if successful
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;

array_keys — Return all the keys of an array

$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));

OUTPUT Array ( [0] => 0 [1] => 3 [2] => 4 )


array_values — Return all the values of an array

$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));

OUTPUT Array ( [0] => XL [1] => gold )


array_key_exists — Checks if the given key or index exists in the array

$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_rand( ) : Random elements of Array

$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";

OUTPUT: Trinity Cypher //diff everytime


array_unique( ) : Array Unique values; Case sensitive

$old_array=array("Mango","Banana","Orange","Banana");
$new_array=array_unique($old_array);

OUTPUT: Array ( [0] => Mango [1] => Banana [2] => Orange )


array_uintersect( ) — Computes the intersection of arrays, compares data by a callback function

$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
print_r(array_uintersect($array1, $array2, "strcasecmp"));

OUTPUT Array ( [a] => green [b] => brown [0] => red )


array_intersect( ) — Computes the intersection of arrays

$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);

OUTPUT Array ( [a] => green [0] => red )


split( ) — Split string into array by regular expression

$months="Jan, Feb, Mar, Apr, May, Jun, July, Aug, Sep, Oct, Nov,Dec";
$mn=split(",",$months);

echo "$mn[2]"; //Mar
echo "$mn[3]"; //Apr
str_split( ) — Convert a string to an array
$spl1 = str_split("Long"); //char
$spl2 = str_split("x"); //only 1 char
$spl3 = str_split(""); //Empty String
$spl4 = str_split(34); //Int
$spl5 = str_split(3.4); //Float
$spl6 = str_split(true); //Bool
$spl7 = str_split(null); // Null

OUTPUT:
Array ( [0] => L [1] => o [2] => n [3] => g )
Array ( [0] => x )
Array ( [0] => )
Array ( [0] => 3 [1] => 4 )
Array ( [0] => 3 [1] => . [2] => 4 ) Array ( [0] => 1 )
Array ( [0] => )


implode( ) — Join array elements with a string

  $array = array('lastname', 'email', 'phone');
  $comma_separated = implode(",", $array);

  echo $comma_separated; // lastname,email,phone

  explode()— Split a string by string

  $pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
  $pieces = explode(" ", $pizza);

  echo $pieces[0]; // piece1
  echo $pieces[1]; // piece2

  sort() — Sort an array

  $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


asort( ) — Sort an array and maintain index association

  $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
  
  asort($fruits);
     foreach ($fruits as $key => $val) {
        echo "$key = $val\n";
     }
   

OUTPUT: c = apple b = banana d = lemon a = orange


arsort( ) — Sort an array in reverse order and maintain index association

  $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
 
  arsort($fruits);
    foreach ($fruits as $key => $val) {
       echo "$key = $val\n";
    }
  

OUTPUT: a = orange d = lemon b = banana c = apple


rsort( ) — Sort an array in reverse order

  $fruits = array("lemon", "orange", "banana", "apple");

  rsort($fruits);

    foreach ($fruits as $key => $val) {
      echo "$key = $val\n";
    }
  

OUTPUT: 0 = orange 1 = lemon 2 = banana 3 = apple


krsort( ) — Sort an array by key in reverse order

  $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");

  krsort($fruits);

    foreach ($fruits as $key => $val) {
      echo "$key = $val\n";
    }
    
OUTPUT: d = lemon c = apple b = banana a = orange
  ksort()— Sort an array by key
  
OUTPUT: a = orange b = banana c = apple d = lemon
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

0 comments: on "PHP Function: Array()"

:) ) ;) ) ;;) :D ;) :p :( ( :) :( :X =(( :-o :-/ :-* :| 8-} :) ] ~x( :-t b-( :-L x( =))
Post a Comment