PHP Arrays
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.
-$array[key] = value;
3 Kinds of Arrays
1) Numeric Array
2) Associative Array
3) Multidimensional Array
Numeric Array
- A numeric array stores each
element with a numeric ID key.
- ways to write a numeric array.
Automatically
Example:
$myar = array(10,45,23,67);
$names = array(“naveed","Qamar",“Shazia");
Munually
Example:
$names[0] = “sofia";
$names[1] = “aysha";
$names[2] = “saif";
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 strings
as keys and assign values to them.
The Id can be used in a script
<?php
$ages[‘Bsit’] = ”22";
$ages[‘Anwar’] = ”25";
$ages[‘Javed’] = ”26";
echo Bsit is " . $ages[‘Bsit’] . " years old.";
?>
//output
-“Bsit is 22 years old.”
Array functions
sort
- To sort the values of an array
$myar = array( 30,45,10,20);
Sort($myar);
Print_r($myar);
//output
Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 45 )
rsort
To sort the values of an array into reverse order
$myar = array( 30,45,10,20);
Sort($myar);
Print_r($myar);
//output
Array ( [0] => 45 [1] => 30 [2] => 20 [3] => 10 )
asort
- To sort the values of an array, but keeping the
key/value relationships -
$myarr =
array("Mon"=>34,"Tue"=>33,"Wed"=>37,"Thr"=>40,"Fri"=>23,"Sat"=>20,"Sun"=>41);
//Out put Array
(
[Sat] => 20
[Fri] => 23
[Tue] => 33
[Mon] => 34
[Wed] => 37
[Thr] => 40
[Sun] => 41
)
arsort
- To sort the values of an array in descending
order, but keeping the key/value relationships -
$myarr =
array("Mon"=>34,"Tue"=>33,"Wed"=>37,"Thr"=>40,"Fri"=>23,"Sat"=>20,"Sun"=>41);
//Out put
Array
(
[Sun] => 41
[Thr] => 40
[Wed] => 37
[Mon] => 34
[Tue] => 33
[Fri] => 23
[Sat] => 20
)
Ksort
- To sort the elements of an array by the keys,
maintaining the key/value relationships
$myarr =
array("Mon"=>34,"Tue"=>33,"Wed"=>37,"Thr"=>40,"Fri"=>23,"Sat"=>20,"Sun"=>41);
//Out put
Array
(
[Fri] => 23
[Mon] => 34
[Sat] => 20
[Sun] => 41
[Thr] => 40
[Tue] => 33
[Wed] => 37
)
krsort
To sort the elements of an array by the keys,
maintaining the key/value relationships
but in descending order
$myarr =
array("Mon"=>34,"Tue"=>33,"Wed"=>37,"Thr"=>40,"Fri"=>23,"Sat"=>20,"Sun"=>41);
//Out put
Array
(
[Wed] => 37
[Tue] => 33
[Thr] => 40
[Sun] => 41
[Sat] => 20
[Mon] => 34
[Fri] => 23
)
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.

arrays.ppt

  • 1.
  • 2.
    What is anArray? -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. -$array[key] = value;
  • 3.
    3 Kinds ofArrays 1) Numeric Array 2) Associative Array 3) Multidimensional Array
  • 4.
    Numeric Array - Anumeric array stores each element with a numeric ID key. - ways to write a numeric array.
  • 5.
    Automatically Example: $myar = array(10,45,23,67); $names= array(“naveed","Qamar",“Shazia");
  • 6.
    Munually Example: $names[0] = “sofia"; $names[1]= “aysha"; $names[2] = “saif";
  • 7.
    Associative Arrays -An associativearray, 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 strings as keys and assign values to them.
  • 8.
    The Id canbe used in a script <?php $ages[‘Bsit’] = ”22"; $ages[‘Anwar’] = ”25"; $ages[‘Javed’] = ”26"; echo Bsit is " . $ages[‘Bsit’] . " years old."; ?> //output -“Bsit is 22 years old.”
  • 9.
    Array functions sort - Tosort the values of an array $myar = array( 30,45,10,20); Sort($myar); Print_r($myar); //output Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 45 ) rsort To sort the values of an array into reverse order $myar = array( 30,45,10,20); Sort($myar); Print_r($myar); //output Array ( [0] => 45 [1] => 30 [2] => 20 [3] => 10 )
  • 10.
    asort - To sortthe values of an array, but keeping the key/value relationships - $myarr = array("Mon"=>34,"Tue"=>33,"Wed"=>37,"Thr"=>40,"Fri"=>23,"Sat"=>20,"Sun"=>41); //Out put Array ( [Sat] => 20 [Fri] => 23 [Tue] => 33 [Mon] => 34 [Wed] => 37 [Thr] => 40 [Sun] => 41 )
  • 11.
    arsort - To sortthe values of an array in descending order, but keeping the key/value relationships - $myarr = array("Mon"=>34,"Tue"=>33,"Wed"=>37,"Thr"=>40,"Fri"=>23,"Sat"=>20,"Sun"=>41); //Out put Array ( [Sun] => 41 [Thr] => 40 [Wed] => 37 [Mon] => 34 [Tue] => 33 [Fri] => 23 [Sat] => 20 )
  • 12.
    Ksort - To sortthe elements of an array by the keys, maintaining the key/value relationships $myarr = array("Mon"=>34,"Tue"=>33,"Wed"=>37,"Thr"=>40,"Fri"=>23,"Sat"=>20,"Sun"=>41); //Out put Array ( [Fri] => 23 [Mon] => 34 [Sat] => 20 [Sun] => 41 [Thr] => 40 [Tue] => 33 [Wed] => 37 )
  • 13.
    krsort To sort theelements of an array by the keys, maintaining the key/value relationships but in descending order $myarr = array("Mon"=>34,"Tue"=>33,"Wed"=>37,"Thr"=>40,"Fri"=>23,"Sat"=>20,"Sun"=>41); //Out put Array ( [Wed] => 37 [Tue] => 33 [Thr] => 40 [Sun] => 41 [Sat] => 20 [Mon] => 34 [Fri] => 23 )
  • 14.
    Multidimensional Arrays - Ina 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.