Arrays in PHP

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);
?>
时间: 2024-08-28 08:16:55

Arrays in PHP的相关文章

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

Arrays工具类

Arraysd的静态方法能够方便的对数组进行操作,每个方法也加了注释 : 程序: import java.util.*;public class Array{        public static void main(String[] args){                int[]  arr={1,3,4,2};                System.out.println("排序前:");                printArray(arr);//打印原数组

350.求两个数组的交集 Intersection of Two Arrays II

Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any ord

ArrayList&amp;LinkedList&amp;Map&amp;Arrays

Java集合框架 1:集合接口 1.1:Collection接口 Collection接口是构造集合框架的基础.它声明所有类集合都将拥有的核心方法 Boolean add(Object obj) 将obj加入到调用类集合中,加入返回true 否则 返回 false Boolean addAll(Collection c) 将c中所有元素都加入到类集合中,都加入返回true否则 false Void clean() 从调用类集合中删除所有元素 Boolean contains(Object obj

LeetCode OJ 4. Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

leetcode----------------Median of Two Sorted Arrays

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 分析 这是一道非常经典的题.这题更通用的形式是,给定两个已经排序好的数组,找到两者所有元素中第 k 大的元素.O(m + n) 的解法比较直观,直接merge两个数组,然后

4. Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

Arrays类

import java.util.Arrays; public class Person {    public static void main(String[] args) {        int[] intArray = new int[9];        for (int i=0; i<intArray.length; i++) {            intArray[i] = i;        }        intArray[5] = 100;        print(

[leetcode-349-Intersection of Two Arrays]

Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the result must be unique. The result can be in any order. 参考思路: 找两个数组相同的部分,难度不算大,我们可以用个set把nums1都

【集合框架】JDK1.8源码分析之Collections &amp;&amp; Arrays(十)

一.前言 整个集合框架的常用类我们已经分析完成了,但是还有两个工具类我们还没有进行分析.可以说,这两个工具类对于我们操作集合时相当有用,下面进行分析. 二.Collections源码分析 2.1 类的属性   2.2 构造函数 private Collections() { } 说明:私有构造函数,在类外无法调用. 2.3 方法分析 下面是Collections的所有方法. 可以看到,Collections的方法包含了各种各样的操作.下面分析最常用的方法. 1. sort函数 该函数有两个重载函