PHP Arrays
By:- Jalpesh Vasa
What is Array?
• Arrays are lists: list of people;
list of size;
list of books;
• To store a group of related items in a single
variable is called....
• Types of array:
 Numeric array(or just an array)
 Associative array
 Multidimensional array
 Mixed array
What is Array? (contd.)
Numeric array(or just an array)
if you want to find and entry, you need to know
its position(index) within an array.
Eg. a[0], a[7], a[34]
Associative array(also known as Hash)
in this index are not integer, but strings.
Eg. a*“honest”+ = (“Mahatma Gandhi”);
a*“good_man”+ = (“Abraham lincoln”);
How to create Array?
You can create PHP array by using array()
function.
$num = array(5,4,3,2,1);
$word = array(“web”, “db”, “app”);
echo $num[2] = ?
echo $word[1] = ?
How to create Array? (contd.)
Also array is produced by:
$num[0] = 5;
$num[1] = 4;
$num[2] = 3;
Another way is:
$shopping = array();
$shopping*+ = “milk”;
$shopping*+ = “coffee”;
$shopping*+ = “potato”;
How to create Array? (contd.)
When you want to print an array in a double-quoted
string, you need to use the braces.
echo “the first item in the list is ,$shopping*0+-”;
print out entire contents of an array using
print_r(arr_name) function.
print_r($shopping); // used only for debugging purpose
How to create Array? (contd.)
PHP array is an ordered set of variable in which
each variable – called an element – has an
associated key.
PHP automatically assigns a key values if keys are
not specified when arrays are constructed.
$f*+ = “v1”
$f*+ = “v2”
$f*5+ = “v3”
$f*+ = “v4”
Keys
0
1
5
6
How to create Array? (contd.)
In associative array, you can access elements in
an array by name.
$abc = array(
name=>”jil”,
occupation=>”Business”,
age=>30
);
echo $abc[age];
How to create Array? (contd.)
Also array is produced by:
$abc*name+=“jil”;
$abc*occupation+=“business”;
$abc[age] = 30;
echo $abc[age];
How to create Array? (contd.)
Multidimensional array is....array within a
array.
Eg.
$abc = array(
array(name=>”bob”, age=>25),
array(name=>”alice”, age=>35),
array(name=>”greg”, age=>45)
);
print $abc[0][age];
How to create Array? (contd.)
$f*‘a’+ = 1;
$f*‘b’+ = 2;
$f*‘c’+ = 3;
$f*‘d’+ = 4;
$f*‘b’+ = $f*‘a’+ +$f*‘c’+ // ans is 4
$var = “the value of $f*‘b’+ = ,$f*‘b’+-”;
How to create Array? (contd.)
$a1 = array(‘a’, ‘b’, ‘c’);
= array(‘a’=>1, ‘a’, ‘b’=>2, ‘b’, ‘c’=>3, ‘c’);
= array(1=>“Sunday”, “Monday”, “Tuesday”);
=array(‘key’=>‘value’, ‘key2’=>array(1,2,3));
How to Print Array?
To print simple array:
For($i=0; $i<count($a1);$i++)
{
echo “the value at index $i = {$a1[$i+-”;
}
How to Print Array?
<?php
$my = array(“php”, “is”, “cool”);
foreach($my as $k => $val)
{
echo “the value of index $k is:$val”;
}
foreach($my as $val)
{
echo “value:$val<br/>”;
}
Array Functions
Array_merge() – joining two arrays.
Eg.
$f1 = array(“a”, “b”, “c”);
$f2 = array(1,2,3);
$f3 = array_merge($f1,$f2);
foreach($f3 as $val)
{
echo “$val”;
}
Array Functions
Array_push() – add multiple variables to array.
- accepts an array & no. of further para.
- returns total number of elements in the array.
Eg.
$f1 = array(“a”, “b”, “c”);
$total = array_push($f1,1,2,3);
echo “there are $total elements in $f1”;
foreach($f1 as $val)
{
echo “$val”;
}
Array Functions
Array_slice() – allows you to extract chunk of an array.
- array, start position, length.
- doesn’t alter the array you passed to it
- returns new array containing elements
Eg.
$f1 = array(“a”, “b”, “c”, “d”, “e”, “f”);
$f2 = array_slice($f1,2,3);
foreach($f2 as $val)
{
echo “$val”;
}
Array Functions
sort() – sort numerically indexed array based on value
Rsort() – reverse sort
Don’t pass associative array
Eg.
$f1 = array(“x”, “b”, “p”, “q”, “r”, “f”);
Sort($f1)
foreach($f1 as $val)
{
echo “$val”;
}
Array Functions
asort() – sort associative array based on value
- sorts its values as sort() also preserves key
Eg.
$f1 = array(“first”=>4, “second”=>3, “third”=>1);
asort($f1)
foreach($f1 as $key=> $val)
{
echo “$val”;
}
Array Functions
ksort() – sort associative array based on key
- sorts its values as sort() also preserves key
Eg.
$f1 = array(“d”=>4, “a”=>3, “c”=>1);
asort($f1)
foreach($f1 as $key=> $val)
{
echo “$val”;
}

4.1 PHP Arrays

  • 1.
  • 2.
    What is Array? •Arrays are lists: list of people; list of size; list of books; • To store a group of related items in a single variable is called.... • Types of array:  Numeric array(or just an array)  Associative array  Multidimensional array  Mixed array
  • 3.
    What is Array?(contd.) Numeric array(or just an array) if you want to find and entry, you need to know its position(index) within an array. Eg. a[0], a[7], a[34] Associative array(also known as Hash) in this index are not integer, but strings. Eg. a*“honest”+ = (“Mahatma Gandhi”); a*“good_man”+ = (“Abraham lincoln”);
  • 4.
    How to createArray? You can create PHP array by using array() function. $num = array(5,4,3,2,1); $word = array(“web”, “db”, “app”); echo $num[2] = ? echo $word[1] = ?
  • 5.
    How to createArray? (contd.) Also array is produced by: $num[0] = 5; $num[1] = 4; $num[2] = 3; Another way is: $shopping = array(); $shopping*+ = “milk”; $shopping*+ = “coffee”; $shopping*+ = “potato”;
  • 6.
    How to createArray? (contd.) When you want to print an array in a double-quoted string, you need to use the braces. echo “the first item in the list is ,$shopping*0+-”; print out entire contents of an array using print_r(arr_name) function. print_r($shopping); // used only for debugging purpose
  • 7.
    How to createArray? (contd.) PHP array is an ordered set of variable in which each variable – called an element – has an associated key. PHP automatically assigns a key values if keys are not specified when arrays are constructed. $f*+ = “v1” $f*+ = “v2” $f*5+ = “v3” $f*+ = “v4” Keys 0 1 5 6
  • 8.
    How to createArray? (contd.) In associative array, you can access elements in an array by name. $abc = array( name=>”jil”, occupation=>”Business”, age=>30 ); echo $abc[age];
  • 9.
    How to createArray? (contd.) Also array is produced by: $abc*name+=“jil”; $abc*occupation+=“business”; $abc[age] = 30; echo $abc[age];
  • 10.
    How to createArray? (contd.) Multidimensional array is....array within a array. Eg. $abc = array( array(name=>”bob”, age=>25), array(name=>”alice”, age=>35), array(name=>”greg”, age=>45) ); print $abc[0][age];
  • 11.
    How to createArray? (contd.) $f*‘a’+ = 1; $f*‘b’+ = 2; $f*‘c’+ = 3; $f*‘d’+ = 4; $f*‘b’+ = $f*‘a’+ +$f*‘c’+ // ans is 4 $var = “the value of $f*‘b’+ = ,$f*‘b’+-”;
  • 12.
    How to createArray? (contd.) $a1 = array(‘a’, ‘b’, ‘c’); = array(‘a’=>1, ‘a’, ‘b’=>2, ‘b’, ‘c’=>3, ‘c’); = array(1=>“Sunday”, “Monday”, “Tuesday”); =array(‘key’=>‘value’, ‘key2’=>array(1,2,3));
  • 13.
    How to PrintArray? To print simple array: For($i=0; $i<count($a1);$i++) { echo “the value at index $i = {$a1[$i+-”; }
  • 14.
    How to PrintArray? <?php $my = array(“php”, “is”, “cool”); foreach($my as $k => $val) { echo “the value of index $k is:$val”; } foreach($my as $val) { echo “value:$val<br/>”; }
  • 15.
    Array Functions Array_merge() –joining two arrays. Eg. $f1 = array(“a”, “b”, “c”); $f2 = array(1,2,3); $f3 = array_merge($f1,$f2); foreach($f3 as $val) { echo “$val”; }
  • 16.
    Array Functions Array_push() –add multiple variables to array. - accepts an array & no. of further para. - returns total number of elements in the array. Eg. $f1 = array(“a”, “b”, “c”); $total = array_push($f1,1,2,3); echo “there are $total elements in $f1”; foreach($f1 as $val) { echo “$val”; }
  • 17.
    Array Functions Array_slice() –allows you to extract chunk of an array. - array, start position, length. - doesn’t alter the array you passed to it - returns new array containing elements Eg. $f1 = array(“a”, “b”, “c”, “d”, “e”, “f”); $f2 = array_slice($f1,2,3); foreach($f2 as $val) { echo “$val”; }
  • 18.
    Array Functions sort() –sort numerically indexed array based on value Rsort() – reverse sort Don’t pass associative array Eg. $f1 = array(“x”, “b”, “p”, “q”, “r”, “f”); Sort($f1) foreach($f1 as $val) { echo “$val”; }
  • 19.
    Array Functions asort() –sort associative array based on value - sorts its values as sort() also preserves key Eg. $f1 = array(“first”=>4, “second”=>3, “third”=>1); asort($f1) foreach($f1 as $key=> $val) { echo “$val”; }
  • 20.
    Array Functions ksort() –sort associative array based on key - sorts its values as sort() also preserves key Eg. $f1 = array(“d”=>4, “a”=>3, “c”=>1); asort($f1) foreach($f1 as $key=> $val) { echo “$val”; }