76-PHP-Arrays and Array functions-10-10-2024 (1)
76-PHP-Arrays and Array functions-10-10-2024 (1)
E.g.:
<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r($a);
?>
Output:
Array ( [a] => Dog [b] => Cat [c] => Horse )
Array Functions
array_chunk(array,size,preserve_key ) - splits an array
into chunks of new arrays
E.g.: 1
<?php
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse","d"=>"
Cow");
print_r(array_chunk($a,2));
?>
Output:
Array (
[0] => Array ( [0] => Cat [1] => Dog )
[1] => Array ( [0] => Horse [1] => Cow )
)
Array Functions
E.g.: 2
<?php
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse","d"=>"
Cow");
print_r(array_chunk($a,2,true));
?>
Output:
Array (
[0] => Array ( [a] => Cat [b] => Dog )
[1] => Array ( [c] => Horse [d] => Cow )
)
Array Functions
array_combine(array1,array2) creates an array by combining
two other arrays, where the first array is the keys, and the other array
is the values.
E.g.:
<?php
Output:
$a1=array("a","b","c","d"); Array ( [a] => Cat [b] => Dog [c] =>
$a2=array("Cat","Dog","Horse","Cow"); Horse [d] => Cow )
print_r(array_combine($a1,$a2));
?>
Array Functions
E.g:
<?php
Output:
$a=array("Cat","Dog","Horse","Dog"); Array ( [Cat] => 1 [Dog] => 2 [Horse] =>
print_r(array_count_values($a)); 1)
?>
Array Functions
E.g:
<?php Output:
$a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
$a2=array(3=>"Horse",4=>"Dog",5=>"Fish"); Array ( [0] => Cat )
print_r(array_diff($a1,$a2));
?>
Array Functions
E.g:
<?php
$a1=array(0=>"Cat",1=>"Dog";,2=>"Horse");
Output:
$a2=array(0=>"Rat",1=>"Horse";,2=>"Dog"); Array ( [0] => Cat [2] => Horse
$a3=array(0=>"Horse",1=>"Dog",2=>"Cat"); )
print_r(array_diff_assoc($a1,$a2,$a3));
?>
Array Functions
E.g:
<?php
Output:
$a=array_fill(2,3,"Dog"); Array ( [2] => Dog [3] => Dog
print_r($a); [4] => Dog )
?>
Array Functions
E.g:
<?php
Output:
$a=array(0=>"Dog",1=>"Cat",2=>"Horse"); Array ( [Dog] => 0 [Cat] => 1
print_r(array_flip($a)); [Horse] => 2 )
?>
Array Functions
E.g:
<?php
$a=array("a"=>"Dog","b"=>"Cat");
Output:
if (array_key_exists("a",$a))
echo "Key exists!"; Key exists!
else
echo "Key does not exist!";
?>
Array Functions
E.g:
<?php
Output:
$a1=array("a"=>"Horse","b"=>"Dog"); Array ( [a] => Horse [b] => Dog [c]
$a2=array("c"=>"Cow",“d"=>"Cat"); => Cow [d]=>Cat )
print_r(array_merge($a1,$a2));
?>
Array Functions
E.g:
<?php
$a=array(3=>"Horse",4=>"Dog");
print_r(array_merge($a));
?>
Output:
Array ( [0] => Horse [1] => Dog )
Array Functions
E.g:
<?php
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido");
array_multisort($a1,SORT_ASC,$a2,SORT_DESC); print_r($a1);
print_r($a2);
?>
Output:
Array ( [0] => Cat [1] => Dog [2] => Dog )
Array ( [0] => Pluto [1] => Fido )
Array Functions
E.g:
<?php
$a=array("Dog","Cat","Horse");
Output:
array_pop($a); Array ( [0] => Dog [1] => Cat )
print_r($a); Array ( [0] => Cat )
array_shift($a);
print_r($a);
?>
Array Functions
E.g:
<?php
$a=array("Dog","Cat");
array_push($a,"Horse","Bird");
Print_r($a);
Output:
array_unshift($a, “fish”); Array ( [0] => Dog [1] => Cat [2] => Horse [3] => Bird )
Array ( [0] => fish [1]=>Dog [2] => Cat [3] => Horse [4] => Bird
print_r($a);
)
?>
Array Functions
Output ????????
E.g:
<?php
$a=array("a"=>"Dog","b"=>"Cat"); Output:
array_push($a,"Horse","Bird");
print_r($a); Array ( [a] => Dog [b] => Cat [0] =>
?> Horse [1] => Bird )
Array Functions
E.g:
<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Hors Output:
e");
print_r(array_rand($a,1)); b
?>
Array Functions
E.g:
<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Hors Output:
e");
print_r(array_reverse($a)); Array ( [c] => Horse [b] => Cat [a] =>
?> Dog )
Array Functions
E.g:
<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Hors Output:
e");
echo array_search("Dog",$a); a
?>
Array Functions
E.g:
<?php
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3= Output:
>"Bird");
print_r(array_slice($a,1,2)); Array ( [0] => Cat [1] => Horse )
?>
Array Functions
E.g:
<?php
$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3
=>"Bird"); Output:
$a2=array(0=>"Tiger",1=>"Lion");
array_splice($a1,0,2,$a2); Array ( [0] => Tiger [1] => Lion [2] =>
print_r($a1); Horse [3] => Bird )
?>
Array Functions
The array_sum(array) function returns the sum of all the values in the array.
E.g:
<?php
$a=array(0=>"5",1=>"15",2=>"25"); Output:
echo array_sum($a);
?> 45
Sorting
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the
value
ksort() - sort associative arrays in ascending order, according to the
key
arsort() - sort associative arrays in descending order, according to
the value
krsort() - sort associative arrays in descending order, according to
the key
Sorting arrays -example
Sort
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";
}
Sort numbers
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
Array Functions
E.g:
<?php Output:
$my_array = array("a" => "Dog", "b" => "Cat", Array
"c" => "Horse"); (
arsort($my_array); [c] => Horse
print_r($my_array); [a] => Dog
?> [b] => Cat
)
Array Functions
E.g:
<?php Output:
$my_array = array("a" => "Dog", "b" => "Cat", Array
"c" => "Horse"); (
asort($my_array); [b] => Cat
print_r($my_array); [a] => Dog
?> [c] => Horse
)
Array Functions
E.g:
<?php Output:
$my_array = array("a" => "Dog", "b" => "Cat", Array
"c" => "Horse"); (
krsort($my_array); [c] => Horse
print_r($my_array); [b] => Cat
?> [a] => Dog
)
Array Functions
The ksort(array,sorttype) function sorts an array by the keys. The values keep
their original keys.
E.g:
<?php Output:
$my_array = array("a" => "Dog", "b" => "Cat", Array
"c" => "Horse"); (
ksort($my_array); [a] => Dog
print_r($my_array); [b] => Cat
?> [c] => Horse
)
Array Functions
E.g:
<?php Output:
$my_array = array("a" => "Dog", "b" => "Cat", Array
"c" => "Horse"); (
rsort($my_array); [0] => Horse
print_r($my_array); [1] => Dog
?> [2] => Cat
)
Array Functions
E.g:
<?php Output:
$my_array = array("a" => "Dog", "b" => "Cat", Array
"c" => "Horse"); (
sort($my_array); [0] => Cat
print_r($my_array); [1] => Dog
?> [2] => Horse
)
Array Functions
E.g:
<?php
$a1=array(“Dog","Cat");
Output:
$a2=array(“salo",“fido"); Array ( [0] => Cat [1] => Dog )
array_multisort($a1,$a2); Array ( [0] => fido [1] => salo )
print_r($a1);
print_r($a2);
?>
Array Functions
CASE_LOWER
CASE_UPPER
SORT_ASC
SORT_DESC
SORT_REGULAR
SORT_NUMERIC
SORT_STRING
SORT_LOCALE_STRING
COUNT_NORMAL
COUNT_RECURSIVE