PHP Arrays


 An array in PHP is actually an ordered map.  A map is a type that maps values to keys. You can use it as a real array, or a list (vector), hashtable, dictionary, collection, stack, queue and probably more. Because you can have another PHP array as a value, you can also quite easily simulate trees.  An array's index (key) can simply be an integer value, which is equivalent to C++ arrays.  To reference an element in an array, you also use the same notation as in C++.  Elements are added dynamically -- when an index is specified, if it doesn't already exist, it will be added.  PHP Arrays also differ from C++ arrays in that each value can be a different type.  $num[4] = 256; $num[10] = 'some text'; $num[20] = $count + 20; echo $num[10];  echo $num[5]; // may produce error  $num[] = -25; // same as $num[21]  A shorthand notation can be used to assign values to an array in a single statement using the array() function:  $elements = array (1,6,'text',-4,0.123,50+$count);             //     0 1   2     3   4      5             // note these are values, not indices  Associative Arrays  PHP Arrays can use either integer or string indices. They can be mixed inside the same array. PHP does not maintain different typed arrays for integer or string indices; there is only one array type.  $num = 10; $elements['test'] = 23; $elements[5] = 'stuff'; $elements[$num] = 'more stuff';  When using the array() function (and several other places in the language), the key/value element pair can be written using the special key => value notation.  $elements = array ( 4 => 'text', 'str' => 23);  Accessing All Elements in an Associative Array  Since an Associative Array can have a mixture of index types, a normal for-loop will not work to access each position in an Associative Array. A special construct foreach exists to simplify this operation:  foreach (array_expression as $value)     statement  foreach (array_expression as $key => $value)     statement  $A1 = array ('x','test',3,-16,'stuff',array(1,2,3)); $A2 = array (10=>20, 'test'=>'data', 'counter'=>12);  foreach ($A1 as $value) echo "$value "; echo "<br><br>\n"; foreach ($A2 as $key => $value)   echo "$key => $value<br>\n";  x test 3 -16 stuff Array  10 => 20 test => data counter => 12  Determining the size of an Array  sizeof(arrayname)   or   count(arrayname)  $A1 = array ('x','test',3,-16,'stuff',array(1,2,3)); $A2 = array (10=>20, 'test'=>'data', 'counter'=>12);  echo sizeof($A1) . ' ' . sizeof($A2); //   6 3 echo count($A1) . ' ' . count($A2); //   6 3  Common Associative Arrays  $_POST     fields from form tags $_GET     fields from URL arguments $_SERVER     common system-oriented information $_COOKIES     fields from browser cookies $_SESSION     fields for user authentication $GLOBALS     all global variables.
Share on Google Plus

About M

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment