‘Array’ Example
<?php $arr = array('one', 2, 'three'); $arr[] = 'the fourth element'; echo $arr[3]."<br>"; echo $arr[1]; ?>
Output :
the fourth element
2
2
Array indexing
Example 1
<?php $arr1 = array( "Jan ", "Feb ", "Mar " ); $arr2 = array( "11 ", "12 ", "13 " ); $arr3 = array( "2001", "2002", "2003" ); echo( $arr1[1] . $arr2[0] . $arr3[1] ); ?>
Output :
Feb 11 2002.Example 2
<?php $arr[0] = 32; $arr[1] = 52.15; $arr[2] = $arr[0] + $arr[1]; echo "Result = "."$arr[0] + $arr[1] = $arr[2]"; ?>
Output :
Result = 32 + 52.15 = 84.15.
Example 3
<?php $arr[] = "PHP"; $arr[] = "C"; $arr[] = "C++"; $arr[] = "Java"; $arr[] = "Perl"; $arr[] = "Python"; $arr[] = "Ruby"; foreach($arr as $lang) { if(!strcmp($lang, "PHP")){ print("<b>" . $lang . "</b> <br />"); }else{ print($lang . " <br />"); } } ?>
Output :
PHP
C
C++
Java
Perl
Python
Ruby
C
C++
Java
Perl
Python
Ruby