Creating Arrays

PHP provides the array( ) language construct that creates arrays. The following examples show how arrays of integers and strings can be constructed and assigned to variables for later use:
$numbers = array(5, 4, 3, 2, 1);

$words = array("Web", "Database", "Applications");



// Print the third element from the array of integers: 3

print $numbers[2];



// Print the first element from the array of strings: "Web"

print $words[0];

By creating arrays this way, PHP assigns integer keys, or indexes to each element. By default, the index for the first element in an array is 0—this may seem odd but think of the index as an offset from the starting position in an array. The values contained in an array can be retrieved and modified using the bracket [ ] syntax. You can also create an array by assigning elements to a new, unset variable. The following code fragment illustrates the bracket syntax with an array of strings:
$newArray[0] = "Potatoes";

$newArray[1] = "Carrots";

$newArray[2] = "Spinach";



// Replace the third element

$newArray[2] = "Tomatoes";

In this example, PHP automatically treats $newArray as an array without a call to array( ).
An empty array can be created by assigning to a variable the return value of array( ). Values can then be added using the bracket syntax. PHP automatically assigns the next numeric index as the key (the largest integer key value plus one) when a key isn't supplied. The result of the following fragment is an array containing three items.
$shopping = array( );



$shopping[] = "Milk";

$shopping[] = "Coffee";

$shopping[] = "Sugar";

It's also easy to print individual element values themselves:
print $shopping[0];   // prints "Milk"

print $shopping[1];   // prints "Coffee"

print $shopping[2];   // prints "Sugar"
Related Posts:
  • How to Read an RSS Feed With PHP <?php include("../includes/config.php"); include("../includes/dbcon.php"); $tt_res=mysql_query("SELECT * FROM `rss_data`"); $num = mysql_num_rows($tt_res); if($num!=0) { $tt="TRUNCATE TABLE `rss_data`"; mysql_query($t… Read More
  • Aarray And multiple array values Aarray And multiple array values array[key_element1] => array(1, 2,3); array[key_element2] => array(4, 5, 6);  associative arrays array[key1] => array(key => value1, key => value2, key => value3); … Read More
  • PHP Interview Questions with Answers-part1    What is PHP?     PHP is a server side scripting language  used for web development applications.     Php is the powerful tool for making dynamic website.     Ma… Read More
  • Php Session Security-Internet Security Because a session may contain sensitive information, you need to treat  the session as a possible security hole. Session security is necessary to  create and implement a session. If someone is listening in or sno… Read More
  • Top codeigniter interview question and answers codeigniter interview question  What is codeigniter? Codeigniter is open source , web application framework.Its is for building websites using php.Codeigniter is loosely based on MVC pattern.Most simple framework in ph… Read More
  • Mysql Join query Codeigniter Mysql Join query Codeigniter code loads and initializes the database class based on your configuration settings. $query = $this->db->query('SELECT name, title, email FROM my_table'); foreach ($query->result() … Read More
  • PHP Sessions - setcookie mplement a session timeout of your own.  Both options mentioned by others session.gc_maxlifetime  and session.cookie_lifetime are not reliable. session.gc_maxlifetime session.gc_maxlifetime specifies the number o… Read More
  • Rewriting Keyword-Rich URLs rules for your .htaccess file Modify the .htaccessfile in your seophpfolder like this: RewriteEngine On # Rewrite numeric URLs RewriteRule ^Products/C([0-9]*)/P([0-9]*)\.html$ i /product.php?category_id=$1&product_id=$2… Read More
  • PHP Session expire-minutes inactivity PHP Session expire-minutes inactivity session_cache_limiter('public'); session_cache_expire(15); //should expire after 15  minutes inactivity asy way to handle this, is to set a variable to $_SESSION  every time … Read More
  • PHP INTERFACES Class inheritanceenables you to describe a parent-child relationshipbetween classes. For example, you might have a base class  Shapefrom which both Squareand Circlederive. However,  you might often want to add a… Read More
  • PHP isn’t as easy as working with JSON <?php $list = array( "eggs", "bread", "milk", "bananas", "bacon", "cheese" ); $xml = new SimpleXMLElement("<list />"); foreach($list as $item) { $xml->addChild("item", $item); } // for nice output $dom = dom_impo… Read More
  • download a file by php code-PHP download files code Basic example for download a  file by php <?php $file="http://testexample.com/your_test_file.jpg"; // path to your file  header('Content-Type: application/octet-stream'); header('Content-Disposition: attachm… Read More
  • php-best top 20 Open Source Content Management Systems best top 20 php Open Source Content Management Systems php-best top 20 Open Source Content Management Systems All this open source content management system written in PHP/mySQL and well configured themes and modules for … Read More
  • Top PHP Tutorial website PHP - Wikipedia, the free encyclopedia PHP is a server-side scripting language designed for  web development but also used as a general-purpose programming language. As of January 2013, PHP was installed on ... http… Read More
  • PHP-final METHODS-override a final method However, there are times where you might want to make sure that a method cannot be re-implemented in its derived  classes. For this purpose, PHP supports the Java-like final access modifier for methods that declares … Read More