Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Notes on Array
Index -> Programming, PHP -> PHP Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
alpesh




PostPosted: Wed Aug 29, 2012 8:40 am   Post subject: Notes on Array

Quote:

An array is a multi-valued variable
An array stores one of more values, each is identified by an index. Array Notation $ arrayName [index]

Array Declaration
$myArray = array(12345, 23456, 34567, 45678);
Array Value Assignment $myArray[0] = 12345;
Array Value Retrieval $myVar = $myArray[0];

Associative Arrays
An alternative to numerically based arrays. An Associative Array defines its own indexes, or keys. Associative Array Declaration
$myArray = array ( ?number1?=>12345, ?number2?=>23456, ?number3?=>34567);
Associative Array Value Assignment $myArray[?number1?] = 12345; Associative Array Value Retrieval $myVar = $myArray[number1];
Multidimensional Arrays
An array can store more than simple variables, arrays can store other arrays. An array of arrays is a multidimensional array.

Multidimensional Array Declaration
$myArray = array (array(123,456,789)
array(134,468,824)
array(321,654,987));
Multidimensional Array Value Assignment $myArray[0,0] = 123; Multidimensional Array Value Retrieval $myVar = $myArray[1,1];
Multidimensional Associative Array Example: <?php
$cpu = array(
"AMD"=>array("price"=>"125.00","speed"=>"1.6 GHz","type"=>"Athlon XP"),
"INT"=>array("price"=>"135.00","speed"=>"1.7 GHz","type"=>"Pentium 4")
);
echo "An AMD ".$cpu['AMD']['type']." ";
echo $cpu['AMD']['speed']." costs $";
echo $cpu['AMD']['price'];
echo "<br />";
echo "An Intel".$cpu['INT']['type']." ";
echo $cpu['INt']['speed']." costs $";
echo $cpu['INT']['price'];
?>
An AMD Athlon XP 1.6 GHz costs $125.00 An Intel Pentium 4 1.7 GHz costs $135.00

Looping through Arrays
For scalar arrays and multidimensional arrays any standard counting schemes with any loop will suffice.
For associative arrays, each() and list() are available.
The function each() splits values out from an associative array
$element = each ($arrayName)
$element[?key?]
$element[?value?]
The function list () allows the developer to define the variables that the function each () splits into.
List ($myVar1, $myVar2) = each ($arrayName)
$myVar1
$myVar2
The functions each () & list () can be used with a while loop to traverse an associative array
Sorting Arrays
sort () normal array sorting
assort () associative array sorting on values
ksort () associative array sorting on keys
rsort () normal array sorting in reverse order
krsort () associative array sorting on keys in reverse order
arsort () associative array sorting on values in reverse order
usort () user defined sort, function accepts the array and a sorting function
usort () user defined sort for associative arrays, function accepts the array and a sorting function
Example: function compare ($x, $y) {
if ($x == $y)
return 0;
else if ($x < $y)
return -1;
else
return 1;
}
$nums ( 10, 5, 15, 25);
usort ( $nums, compare );
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, PHP -> PHP Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: