Arrays in PHP
1. What is an Array?
Variable can hold a single value at a time whereas an array is like a special variable,which can hold more than one number,or more than one string,at a time.
2. Setting up an Array in PHP
array()
<?
$names = array("Doree","Nemo","Marlin","Jeff");
$numbers = array("10","20","30","40");
$numStr = array("10","Doree","30","Nemo");
?>
3. Getting at the values stored in Arrays
<?
echo $names[0]; // print Doree
echo $names[1]; // print Nemo
echo $names[2]; // print Marlin
echo $names[3]; // print Jeff
?>
4. Arrays - Using Text as Keys
<?
$contactInfo = array( );
$contactInfo["name"] = "Some Name";
$contactInfo["address"] = "Some Address";
$contactInfo["phone"] = "123 456 7890";
$contactInfo["fax"] = "098 765 4321";
?>
5. Arrays and For Each
<?
foreach ($contactInfo as $key_name => $key_value)
{
echo "Key = " . $key_name . " Value = " . $key_value . "<BR>";
}
?>
6. Sorting Array values
There may be times when you want to sort the values inside of an array. There are many functions in php to sort values in an array. Some of them are;
<?
asort($names) – Sort an array and maintain index association
arsort($names) - Sort an array in reverse order and maintain index association
rsort($names) – Sorts an array in reverse order
krsort($names) - Sort an array by key in reverse order
?>
7. Random Keys from an Array
<?
$numbers = array(1 => 1,2 => 3,3 => 5,4 => 7,5 => 9,6 => 11);
$random_key = array_rand($numbers,1);
echo $random_key;
?>
8. The count function
<?
$numbers = array(1 => 1,2 => 3,3 => 5,4 => 7,5 => 9,6 => 11);
$array_count = count($numbers);
echo $array_count;
?>
Php Discussion
- - PHP 4 or 5 as a Deploymen
- - How do I decode a html/ph
- - PHP help
- - What is wamp?
- - How to execute a php file
Php Source Code
- - Wraps A String To A Given
number Of Characters Using A
string Break Character - - Uppercase The First Character
of Each Word In A String - - Make A String's First Character
uppercase - - Strip Whitespace (or Other
characters) From The Beginning
and End Of A String - - Return Part Of A String





