码迷,mamicode.com
首页 > Web开发 > 详细

Arrays in PHP

时间:2015-10-08 20:08:23      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

Part 1: What‘s an Array?

An array is a list of items, a bit like a shopping list. It allows you to store more than one item in only one variable.

Think of it like this. When writing your shopping list, you could use a separate piece of paper for each item you need to buy (a variable). However this is silly and unneeded—could you imagine how hard it would be to carry all that paper around with you? So, you use one piece of paper for all of your items. This one piece of paper is your array.

In the editor do you see the bit of text that starts with $array =? That is our array. Don‘t worry about all the details just yet, we will explain in more detail later. For now, just see if you can work out what is happening.

<?php
      $array = array("Egg", "Tomato", "Beans");
 ?>  

 

Part 2: Array Syntax

Have you noticed something familiar at the start of our array? That‘s right, it starts in the same way as a variable, with the $ sign, and then a name, followed by =.

However, this is when things start to get different. When declaring an array, we have to use array(). This basically tells PHP that $array is an array and not something else, such as a regular old variable.

By now, I am sure you have noticed the text inside the ( and ). This is just the items in our array. So, currently, our array has the items "Egg," "Tomato," and "Beans" in it. You can add any type of information to an array, and you do it in much the same way as when declaring variables. Use"" when adding strings, and just enter the number when adding integers.

You must always remember, however, that each item in an array must be separated by a comma: ,.

<?php
        // Add your array elements after
        // "Beans" and before the final ")"
        $array = array("Egg", "Tomato", "Beans" );        
 ?>

 

 

Part 3: Access by Offset with []

Each item in an array is numbered starting from 0. For example, when we create an array:

<?php
    $myArray = array("do", "re", "mi");
?>

 

Each item is numbered starting from 0, like this:

+------+------+------+
| "do" | "re" | "mi" |
+------+------+------+
   0      1      2      

The item "do" is in position 0, the item "re" is in position 1, and so on.

Therefore, we can access a particular item of the array using its position, like this:

<?php
$myArray = array("do", "re", "mi");

echo $myArray[0]
// outputs "do"
?>

 

  1. First we create an array named $myArray
  2. Then we use echo to output the first item in $myArray. Since items are numbered starting from 0, "do" is at position 0.

 

Part 4: Access by offset with {}

PHP is a very flexible language. When accessing arrays by offset, you can actually use two different types of syntax: the [] syntax we‘ve covered, or you can use curly braces ({}). You use the curly braces just like you use the square brackets:

<?php
 $myArray = array("do", "re", "mi");
 print $myArray{2};
 // prints "mi";
?>

 

Both forms are equivalent, and using one or the other is totally up to you!

 

Part 5: Modifying Array Elements

An item in an array can be changed by specifying its position and providing a new value, like this:

<?php
$myArray = array("red", "blue", "yellow");

echo $myArray[1];
// outputs "blue"

$myArray[1] = "green";

echo $myArray[1];
// outputs "green"
?>

 

  1. First we create a new array $myArray with a list of colors.
  2. Then we output the item at position 1. Since items are numbered starting from 0, "blue" is at position 1
  3. Next we change the item at position 1 to "green".
  4. Now when we output the item at position 1, we get "green".

 

Part 6: Deleting Array Elements

Finally, you can remove elements using unset:

<?php
  $array = array("red", "blue", "green");
  unset($array[2]);
?>

 

You can even delete the whole array:

<?php
  unset($array);
?>

 

 

 

 

 

 

 

 

Arrays in PHP

标签:

原文地址:http://www.cnblogs.com/elewei/p/4861868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!