By Sergey Skudaev
Arrays are used in any programming language. You can imagine an array as a long box with many the same compartments, like this |___|___|___|___|___|.
What ever you put in a compartment is its value. The following array |_a_|_b_|_c_|_d_|_e_| contains characters a,b,c,d,e. To access a value you should know in which compartment it is placed. For example 'b' is in the second compartment.
In most computer languages array index (counting) starts from 0, not from 1. Index of the first element of the array is 0, Index of the second element of the array is 1 and so on. In array of names below you can see indexes and values.
To display "Anna" array value you have to access element (compartement) with index 3. print($names[3]); To display all array values - use for loop or print array function.
The code below, I provided demonstrate main array functions:
The sort function sorts array ascending order.
The rsort function sorts array in reverse order.
The array_pop function removes and returns the last element of an array.
The rray_push function adds element to the end of array.
The array_unique function remove duplicate values from an array.
The print_r function prints an array.
The array_rand function select a random array element.
The implode function converts an array into a string.
//declare an array of names $names=array(); $message="Hello "; $prefix1="Mr."; $prefix2="Mrs."; $names[0]="John"; $names[1]="George"; $names[2]="James"; $names[3]="Anna"; $names[4]="Robert"; $names[5]="John"; $names[6]="James"; $names[7]="George"; $names[8]="Maria"; $names[9]="Peter"; $names[10]="James"; print('<br>The sort function sorts array<br>'); sort($names); //Get size of array $asize=sizeof($names); for($i=0; $i<$asize; $i++) { //Check if it is female name put Mrs. prefix //else put Mr. prefix if(($names[$i]=="Anna")||($names[$i]=="Maria")) { print($message.$prefix2.$names[$i]."<br>"); } else { print($message.$prefix1.$names[$i]."<br>"); } } print('<br>');
echo "The array_unique function removes duplicate array values<br>"; $array=array(); $array=array_unique($names); foreach($array as $key => $value) { echo $key . "-". $value . "<br>"; } print('<br>'); rsort($array); print("The rsort function sorts array in reverse order<br>"); foreach($array as $key => $value) { echo $key . "-". $value . "<br>"; } print('<br>The array_pop($array) functions returns the last element<br>'); $lastelement=array_pop($array); print('<br>The last element='.$lastelement.'<br>'); print('<br>Array after calling array_pop($array): The last element removed<br><br>'); foreach($array as $key => $value) { echo $key . "-". $value . "<br>"; } //The Array_push function add elements to the end of an array //The print_r print array function prints array key - value pairs array_push($array, "Chris", "Colin"); print_r($array); print('<br><br>The array_rand($array) function returns random array index<br>'); $random=array_rand($array); print('<br>print array element by random index<br>'); print('<br>Random element='.$array[$random].'<br>'); $string=implode($array); print("<br>Array imploded in string:<br>"); print($string); ?>
Autput:
The sort function sorts an array
Hello Mrs.Anna
Hello Mr.George
Hello Mr.George
Hello Mr.James
Hello Mr.James
Hello Mr.James
Hello Mr.John
Hello Mr.John
Hello Mrs.Maria
Hello Mr.Peter
Hello Mr.Robert
The array_unique function removes duplicate values
0-Anna
1-George
3-James
6-John
8-Maria
9-Peter
10-Robert
The rsort function sorts an array in reverse order
0-Robert
1-Peter
2-Maria
3-John
4-James
5-George
6-Anna
The array_pop($array) functions returns the last element
The last element=Anna
Array after caling array_pop($array): The last element removed
0-Robert
1-Peter
2-Maria
3-John
4-James
5-George
Array after calling array_push($array, "Chris", "Colin")
and print_r: Chris and Colin are added to the end of array
Array ( [0] => Robert [1] => Peter [2] => Maria [3] => John [4] => James [5] => George [6] => Chris [7] => Colin )
The array_rand($array) function returns random array index
print array element by random index
Random element=Colin
Array imploded in string:
RobertPeterMariaJohnJamesGeorgeChrisColin
Download PHP source code for this tutrial