PHP arrays allow storing multiple values in a single variable. There are several types of arrays including numeric, associative, multidimensional, and mixed. Arrays can be created using the array() function or by directly assigning values with indexes. Array elements can be accessed using their index/key and printed. Common array functions include array_merge() to join arrays, array_push() to add elements, and sort functions like sort(), asort(), ksort() to sort arrays.
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 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] = ?
5. 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”;
6. 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
7. 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
8. 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];
9. How to create Array? (contd.)
Also array is produced by:
$abc*name+=“jil”;
$abc*occupation+=“business”;
$abc[age] = 30;
echo $abc[age];
10. 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];
11. 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’+-”;
13. How to Print Array?
To print simple array:
For($i=0; $i<count($a1);$i++)
{
echo “the value at index $i = {$a1[$i+-”;
}
14. 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/>”;
}
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”;
}