0% found this document useful (0 votes)
43 views

PHP Array AND Array Functions: by Kiran and Nikita

The document discusses PHP arrays and array functions. It defines three types of arrays: numeric, associative, and multidimensional. Numeric arrays use numeric indices to store elements, associative arrays use named keys, and multidimensional arrays can store arrays within arrays. The document also describes several PHP functions for sorting and manipulating array elements, including sort(), asort(), ksort(), and array_change_key_case().

Uploaded by

Kiran Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

PHP Array AND Array Functions: by Kiran and Nikita

The document discusses PHP arrays and array functions. It defines three types of arrays: numeric, associative, and multidimensional. Numeric arrays use numeric indices to store elements, associative arrays use named keys, and multidimensional arrays can store arrays within arrays. The document also describes several PHP functions for sorting and manipulating array elements, including sort(), asort(), ksort(), and array_change_key_case().

Uploaded by

Kiran Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

PHP Array

AND
ARRAY
FUNCTIONS
By Kiran and Nikita.

What is an Array.?
-An array can store one or more
values in a single variable name.
-Each element in the array is assigned
its own ID so that it can be easily
accessed.
Eg:$array[key] = value;
$actor[0] =ram;

3 Kinds of Arrays:
1) Numeric Array(Indexed)
2) Associative Array
3) Multidimensional Array

Numeric Array
- A numeric array stores each element
with a numeric ID key.
Example:
Automatically:
$names = array(PHP",FUNCTION",ARRAY");
Manually:
$names[0] = PHP";.
$names[2] = ARRAY";
$names[1] = FUNCTION";

- .

The ID can be used in a


script
Example:
<?php
$names[0] = SAM";
$names[1] = RAM";
$names[2] = FRIENDS";
echo $names[0] . " and " . $names[1] .
" are ".good. $names[2] ;
?>
OUTPUT : Sam and RAM are good Friends

Example.
Using an array to assign an age to a person.
$ages = array(Brent"=>42, Andrew"=>25,
"Joshua16=>);
$ages[Brent'] = 42";
$ages[Andrew'] = 25";
$ages['Joshua'] = 16";

Associative Arrays
-An

associative array, each ID key is associated with a


value.

-When storing data about specific named values, a


numerical array is not always the best way to do it.
-With associative arrays we can use the values as keys and
assign values to them.

The Id can be used in a


script
<?php
$ages[Brent] = 42";
$ages[Andrew] = 25";
$ages[Joshua] = 16";
echo Brent is " . $ages[Brent] . " years old.";
?>

Output
-Brent is 42 years old.

Multidimensional Arrays.
- In a multidimensional array, each
element in the main array can also
be an array.
- And each element in the sub-array
can be an array, and so on.
- If an array is a list of information
then MDA is list of information
with in a single list.

Example
$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan" ),
"Quagmire"=>array ( "Glenn" ),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);
echo "Is " . $families['Griffin'][2] .
" a part of the Griffin family?";

Ouput
Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)

Is Megan a part of the


Griffin family?

PHP FUNCTIONS

Sort Functions For Arrays


sort() - sortarraysinascending order
<?php
$numbers=array(4,6,2,22,11);
sort($numbers);
OUTPUT:2 4 6 1122
$arrlength=count($numbers);
for($x=0;$x<$arrlength;$x++)
{
echo $numbers[$x];

}
?>

rsort() - sort arrays in descending


order

<?php
$numbers=array(4,6,2,22,11);
rsort($numbers);
$arrlength=count($numbers);
for($x=0;$x<$arrlength;$x++)
{
echo $numbers[$x];

?>
OUTPUT:2211642

asort() - sortassociativearrays in
ascending order, according to the
value

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
asort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

O/P: Key=Peter, Value=35

Key=Ben, Value=37
Key=Joe, Value=43

ksort() - sort associative arrays in


ascending order, according to
thekey

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
ksort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

OUTPUT:Key=Ben, Value=37

Key=Joe, Value=43
Key=Peter, Value=35

arsort() - sort associative arrays in


descending order, according to the
value

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
arsort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

Output:Key=Joe, Value=43

Key=Ben, Value=37
Key=Peter, Value=35

krsort() - sort associative arrays in


descending order, according to the
key

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
krsort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

O/p:Key=Peter, Value=35

Key=Joe, Value=43
Key=Ben, Value=37

array_change_key_case():Changes all
keys in an array tolowercaseor
uppercase
Returns an array with all keys
fromarraylowercased or uppercased.
Numbered indices are left as is.
<?php
$input_array=array("FirSt"=>1,"SecOnd"=>4);
print_r(array_change_key_case($input_array,CASE_UPPER));
?>

O/P:
Array (
[FIRST] => 1
[SECOND] => 4
)

You might also like