1. The array() function is used to create an array
2. The array_change_key_case() function changes all keys in an array to lowercase or uppercase.
3. The array_chunk() function splits an array into chunks of new arrays.
4. The array_combine() function creates an array by using the elements from one “keys” array and one “values” array.
5. The array_count_values() function counts all the values of an array.
6. The array_diff() function compares the values of two (or more) arrays, and returns the differences.
7. The array_fill() function fills an array with values.
8. The array_merge() function merges one or more arrays into one array.
9. The array_shift() function removes the first element from an array, and returns the value of the removed element.
10. The array_pop() function deletes the last element of an array.
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "<br>:: array() function ::<br>";
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
echo "<br>-----------------------------------------";
$cars = array("Volvo", "BMW", "Toyota", "Honda", "Mercedes", "Opel");
echo "<br><br>:: array_chunk() function :: </br>";
print_r(array_chunk($cars, 2));
echo "<br>-----------------------------------------";
$fname = array("Peter", "Ben", "Joe");
$age = array("35", "37", "43");
echo "<br><br>:: array_combine() function :: </br>";
$c = array_combine($fname, $age);
print_r($c);
echo "<br>-----------------------------------------";
$a = array("A", "Cat", "Dog", "A", "Dog");
echo "<br><br>:: array_count_values() function :: </br>";
print_r(array_count_values($a));
echo "<br>-----------------------------------------";
$a1 = array("a"=>"red", "b"=>"green", "c"=>"blue", "d"=>"yellow");
$a2 = array("e"=>"red", "f"=>"green", "g"=>"blue");
$result = array_diff($a1, $a2);
echo "<br><br>:: array_diff() function :: </br>";
print_r($result);
echo "<br>-----------------------------------------";
$a1 = array_fill(3, 4, "blue");
$b1 = array_fill(0, 1, "red");
echo "<br><br>:: array_fill() function :: </br>";
print_r($a1);
echo "<br>";
print_r($b1);
echo "<br>-----------------------------------------";
$a1 = array("red", "green");
$a2 = array("blue", "yellow");
echo "<br><br>:: array_merge() function :: </br>";
print_r(array_merge($a1, $a2));
echo "<br>-----------------------------------------";
$a = array("a"=>"red", "b"=>"green", "c"=>"blue");
echo "<br><br>:: array_shift() function :: </br>";
echo array_shift($a);
echo "<br>";
print_r ($a);
echo "<br>-----------------------------------------";
$a = array("red", "green", "blue");
array_pop($a);
echo "<br><br>:: array_pop() function :: </br>";
print_r($a);
echo "<br>-----------------------------------------";
?>
OUTPUT:
:: array() function ::
I like Volvo, BMW and Toyota.
-----------------------------------------
:: array_chunk() function ::
Array ( [0] => Array ( [0] => Volvo [1] => BMW ) [1] => Array ( [0] => Toyota [1] => Honda ) [2] => Array ( [0] => Mercedes [1] => Opel ) )
-----------------------------------------
:: array_combine() function ::
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
-----------------------------------------
:: array_count_values() function ::
Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
-----------------------------------------
:: array_diff() function ::
Array ( [d] => yellow )
-----------------------------------------
:: array_fill() function ::
Array ( [3] => blue [4] => blue [5] => blue [6] => blue )
Array ( [0] => red )
-----------------------------------------
:: array_merge() function ::
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
-----------------------------------------
:: array_shift() function ::
red
Array ( [b] => green [c] => blue )
-----------------------------------------
:: array_pop() function ::
Array ( [0] => red [1] => green )
-----------------------------------------
2. The array_change_key_case() function changes all keys in an array to lowercase or uppercase.
3. The array_chunk() function splits an array into chunks of new arrays.
4. The array_combine() function creates an array by using the elements from one “keys” array and one “values” array.
5. The array_count_values() function counts all the values of an array.
6. The array_diff() function compares the values of two (or more) arrays, and returns the differences.
7. The array_fill() function fills an array with values.
8. The array_merge() function merges one or more arrays into one array.
9. The array_shift() function removes the first element from an array, and returns the value of the removed element.
10. The array_pop() function deletes the last element of an array.
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "<br>:: array() function ::<br>";
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
echo "<br>-----------------------------------------";
$cars = array("Volvo", "BMW", "Toyota", "Honda", "Mercedes", "Opel");
echo "<br><br>:: array_chunk() function :: </br>";
print_r(array_chunk($cars, 2));
echo "<br>-----------------------------------------";
$fname = array("Peter", "Ben", "Joe");
$age = array("35", "37", "43");
echo "<br><br>:: array_combine() function :: </br>";
$c = array_combine($fname, $age);
print_r($c);
echo "<br>-----------------------------------------";
$a = array("A", "Cat", "Dog", "A", "Dog");
echo "<br><br>:: array_count_values() function :: </br>";
print_r(array_count_values($a));
echo "<br>-----------------------------------------";
$a1 = array("a"=>"red", "b"=>"green", "c"=>"blue", "d"=>"yellow");
$a2 = array("e"=>"red", "f"=>"green", "g"=>"blue");
$result = array_diff($a1, $a2);
echo "<br><br>:: array_diff() function :: </br>";
print_r($result);
echo "<br>-----------------------------------------";
$a1 = array_fill(3, 4, "blue");
$b1 = array_fill(0, 1, "red");
echo "<br><br>:: array_fill() function :: </br>";
print_r($a1);
echo "<br>";
print_r($b1);
echo "<br>-----------------------------------------";
$a1 = array("red", "green");
$a2 = array("blue", "yellow");
echo "<br><br>:: array_merge() function :: </br>";
print_r(array_merge($a1, $a2));
echo "<br>-----------------------------------------";
$a = array("a"=>"red", "b"=>"green", "c"=>"blue");
echo "<br><br>:: array_shift() function :: </br>";
echo array_shift($a);
echo "<br>";
print_r ($a);
echo "<br>-----------------------------------------";
$a = array("red", "green", "blue");
array_pop($a);
echo "<br><br>:: array_pop() function :: </br>";
print_r($a);
echo "<br>-----------------------------------------";
?>
OUTPUT:
:: array() function ::
I like Volvo, BMW and Toyota.
-----------------------------------------
:: array_chunk() function ::
Array ( [0] => Array ( [0] => Volvo [1] => BMW ) [1] => Array ( [0] => Toyota [1] => Honda ) [2] => Array ( [0] => Mercedes [1] => Opel ) )
-----------------------------------------
:: array_combine() function ::
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
-----------------------------------------
:: array_count_values() function ::
Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
-----------------------------------------
:: array_diff() function ::
Array ( [d] => yellow )
-----------------------------------------
:: array_fill() function ::
Array ( [3] => blue [4] => blue [5] => blue [6] => blue )
Array ( [0] => red )
-----------------------------------------
:: array_merge() function ::
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
-----------------------------------------
:: array_shift() function ::
red
Array ( [b] => green [c] => blue )
-----------------------------------------
:: array_pop() function ::
Array ( [0] => red [1] => green )
-----------------------------------------
0 comments:
Post a Comment