Array Code Examples - PHP Array Functions - PHP code

++++++

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.

//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>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 "function array_unique 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("rsort function sorts array in reverse order<br>");

     foreach($array as $key => $value)
     {
     echo $key . "-". $value . "<br>";
     }

print('<br>array_pop($array) functions returns the last element<br>');

$lastelement=array_pop($array);

print('<br>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>";
     }

//Array_push function add elements to the end of an array
//print_r print array function prints array key - value pairs

array_push($array, "Chris", "Colin");

print_r($array);

print('<br><br>array_rand($array) 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:

sort function sorts 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

function array_unique removes duplicate values
0-Anna
1-George
3-James
6-John
8-Maria
9-Peter
10-Robert

rsort function sorts array in reverse order
0-Robert
1-Peter
2-Maria
3-John
4-James
5-George
6-Anna

array_pop($array) functions returns the last element

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 )

array_rand($array) 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

Did you find information useful? Send to your friend a link to this page

Please rate the tutorial

1 2 3 4 5 6 7 8 9 10



Comments
Register to add comments ( 1000 char ) for arrays.php.
Are you human? Please select two the same numbers