Array

  • An array is a special variable using which we can store multiple values in a single variable
  • Names of 5 employee can be stored using 5 variables. If the case is for 50 employees, then array is the solution
  • An array can hold multiple values under a single name which can be accessed using the index position
  • Array can be created using array(); construct
  • The values in an array can be either an integer, float, string, object or even another array
  • An array can hold data of mixed types
  • No restriction in the number of values an array can hold
  • Each value in an array is termed as element/value. Each
    element has its own unique index

Advantages of Array

  • Easy to manipulate
  • Easy to work with many values
  • Can use many built-in array functions like sort, search, merge etc

Displaying the array as an output

Displaying output on the screen is differ with the
memory value on the computer i.e. The value of $a when output is 10 but the value in the memory is 11

1
2
3
echo ();
print ();
print_r();

Example 1

1
2
3
4
<?php
$car=array("volvo","mercedes","ferrari","lamborghini");
print_r($car);
?>

Array( [0] => volvo[1] => mercedes[2] => ferrari[3] => lamborghini )

Array Functions

Array Function Description
array() The function is used to create an array
array_change_key_case() It returns an array with all keys in lowercase or uppercase
array_chunk() It splits an array into chunks of arrays
array_combine() It is used to combine two arrays and creates one array for keys and another array for its values
array_count_values() It returns the count of all the array values
array_diff() The function compares array values and returns the difference

array_change_key_case(var, CASE);

Example 1

1
2
3
4
5
6
7
<?php
$car=array("s40"=>"volvo","amg"=>"mercedes","sauber"=>"ferrari","sil"=>"lamborghini");
print_r($car);
echo "<br>";
$car=array_change_key_case($car,CASE_UPPER);
print_r($car);
?>

Array( [s40] => volvo[amg] => mercedes[sauber] => ferrari[sil] => lamborghini )
Array( [S40] => volvo[AMG] => mercedes[SAUBER] => ferrari[SIL] => lamborghini )

Example 2

1
2
3
4
5
6
7
<?php
$car=array("s40"=>"volvo","amg"=>"mercedes","sauber"=>"ferrari","sil"=>"lamborghini");
print_r($car);
echo "<br>";
$car=array_change_key_case($car,CASE_LOWER);
print_r($car);
?>

Array( [S40] => volvo[AMG] => mercedes[SAUBER] => ferrari[SIL] => lamborghini )
Array( [s40] => volvo[amg] => mercedes[sauber] => ferrari[sil] => lamborghini )

Example 3

1
2
3
4
5
6
7
8
9
10
<?php
$car=array("S40"=>"volvo","AMG"=>"mercedes","SAUBER"=>"ferrari","SIL"=>"lamborghini");
print_r($car);
echo "<br>";
$car=array_change_key_case($car,CASE_LOWER);
print_r($car);
$car=array_change_key_case($car,CASE_UPPER);
echo "<br>";
print_r($car);
?>

Array( [S40] => volvo[AMG] => mercedes[SAUBER] => ferrari[SIL] => lamborghini )
Array( [s40] => volvo[amg] => mercedes[sauber] => ferrari[sil] => lamborghini )
Array( [S40] => volvo[AMG] => mercedes[SAUBER] => ferrari[SIL] => lamborghini )

array_flip()

Example 1

1
2
3
4
5
6
<?php
echo "before flip:"."<br>";
print_r($a=array_flip(array("a"=>"b","c"=>"d")));
echo "<br>"."after flip:","<br>";
print_r(array_flip($a));
?>

before flip:
Array( [b] => a [d] => c )
after flip:
Array( [a] => b [c] => d )

array_chunk(var, count)

Example 1

1
2
3
4
5
<?php
$car=array("volvo","mercedes","ferrari","lamborghini");
$car=array_chunk($car,2);
print_r($car);
?>

before flip:
Array( [b] => a [d] => c )
after flip:
Array( [a] => b [c] => d )

array_combine(arr1, arr2)

Example 1

1
2
3
4
5
6
<?php
$user_key=array("user1","user2","user3","user4");
$user_val=array("John","Alice","Ravi","Lim");
$cmbine=array_combine($user_key,$user_val);
print_r($cmbine);
?>

Array( [user1] => John [user2] => Alice [user3] => Ravi [user4] => Lim )

Both must have samenumber of arguments!

array_count_values(arr)

Example 1

1
2
3
4
5
<?php
$car=array("volvo","mercedes","ferrari","ferrari");
$car=array_count_values($car);
print_r($car);
?>

Array( [volvo] => 1 [mercedes] => 1 [ferrari] => 2 )

array_diff(arr1, arr2)

Example

1
2
3
4
5
6
<?php
$car=array("volvo","mercedes","ferrari","ferrar");
$cars=array("volvo","mercedes");
$dif=array_diff($car,$cars);
print_r($dif);
?>

Array( [2] => ferrari[3] => ferrar )

Types of array

Indexed array

  • Stores each array element with a numeric index. array

Example 1

1
2
3
4
<?php
$names=array("Pinky", "Tom","Prince");
echo "Name list : $names[0], $names[1], $names[2]";
?>

Name list: Pinky, Tom, Prince

Example 2

1
2
3
4
5
<?php
$names=array("Pinky", "Tom", "Prince");
foreach($names as $str){
echo $str ."&nbsp";}
?>

Pinky Tom Prince

Associative array

  • Stores each array element as a key to value as a pair. Key can be a user defined string

Example 1

1
2
3
4
5
6
7
8
9
<?php
$department = array("Pinky"=>"Testing", "Tom"=>"Development",
"Prince"=>"Admin"); echo 'Pinky belongs to
'.$department["Pinky"]."<br />";
echo 'Tom belongs to
'.$department["Tom"]."<br />"; echo
'Prince belongs to
'.$department["Prince"]."<br />";
?>

Pinky Tom Prince

Example 2

1
2
3
4
5
6
<?php
$department = array("Pinky"=>"Testing", "Tom"=>"Development",
"Prince"=>"Admin");
foreach($department as $k=>$v){
echo "Name : ".$k." Department :".$v."<br />";}
?>

Name : Pinky Department : Testing
Name : Tom Department : Development
Name : Prince Department : Admin

Multidimensional array

  • Element of an array can contain one or more array

Example

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$employee = array (
array(1,"Pinky","Testing"),
array(2,"Tom","Development"),
array(3,"Prince","Admin")
);
for ($row = 0; $row < 3; $row++) {
for ($col = 0; $col < 3; $col++) {
echo $employee[$row][$col]." "; }
echo "<br/>";
}
?>
1
2
3
4
5
6
7
8
9
10
11
<?php
$a = array (
array(1,"Pinky","Testing"),
array(2,"Tom","Development"),
array(3,"Prince","Admin")
);
foreach($a as $a){
foreach ($a as $a) { echo $a; }
echo "<br>";
}
?>

1 Pinky Testing
2 Tom Development
3 Prince Admin

Convert ARRAY to STRING

Syntax

1
implode([separator,]$arrayname)
  • The separator specifies what is to be inserted between the array elements
  • In implode function, the separator is optional
  • The default separator is” “

Example

1
2
3
4
5
<?php
$hobbies=array("Reading Books","PlayingGolf","WatchingTennis","Dancing");
$hobby=implode("::",$hobbies);
echo $hobby;
?>

Reading Books :: Playing Golf :: Watching Tennis ::Dancing

Convert STRING to ARRAY

Syntax

1
explode(separator,string[,limit])
  • The separator will specify where to break the string. It is mandatory
  • Limit specifies number of elements to return. This parameter is optional

Example 1

1
2
3
4
5
<?php
$hobbies=array("Reading Books","PlayingGolf","WatchingTennis","Dancing");
$hobby=implode("::",$hobbies);
echo $hobby;
?>

Array([0]=>Golf[1]=>Cricket[2]=>Tennis[3]=>Football[4]=>Hockey)

Example 2

1
2
3
4
5
<?php
$games="Golf,Cricket,Tennis,Football,Hockey";
$strgtoarr=explode(",",$games);
print_r($strgtoarr);
?>

Array([0]=>Golf,Cricket,Tennis,Football,Hockey)

Example 3

1
2
3
4
5
<?php
$games="Golf,Cricket,Tennis,Football,Hockey";
$strgtoarr=explode("s",$games);
print_r($strgtoarr);
?>

Array([0]=>Golf,Cricket,Tenni[1]=>,Football,Hockey)

Array Sorting

Functions used to sort

  • an array in a particular orderare
  • Sort by default based on value
  • asortbased pnvalue
  • ksortbased on key
  • rsort(opposite)
  • arsort(opposite)
  • krsort(opposite)

Example

1
2
3
4
5
6
<?php
$hobbies=array("a","b","d","c");
sort($hobbies);
foreach($hobbies as $temp) {
echo $temp."<br>";}
?>

a
b
c
d