首页 > 代码库 > Storing multiple values in arrays

Storing multiple values in arrays

It‘s often useful to store related values together in a single variable. To do so you create array which is similar to a list. Let‘s indulge my passion for Hitchhikers Guide to the Galaxy and create an array for some of the main characters. You store an array in an ordinary variable. I‘ll advise the assignment operator and array with a pair of parentheses and you put the values that you want to store in the array between the parentheses as a comma separated list. I‘m going to store strings so, in quotes.

Arthur Dent and the commas go outside of the quotes. Lord prefect and Zaphod Beeblebrox. So we now have an array containing three elements all stored in the variable called characters, you can‘t use echo with an array, lets just see what happens if you try.

Save that and view it in the browser And it just tells us it‘s an array, which is not very helpful. The way that you inspect the contents of an array is to use a PHP function called print_r. Let‘s just comment that out. And then print_r. And inside the parentheses the name of the array that you want to inspect.

So if we refresh the browser, here we‘ve got the array elements. And each one has a number which is called its index. So zero is Arthur Dent. One is Ford Prefect. And two is Zaphod Beeblebrox. It‘s important to note that arrays in PHP, and in many other programming languages, begin from the index zero. To access an array element, you add the array index inside a pair of square brackets, after the variable name. So if we want to display the second element in the array. We use echo and the array name and the array index goes inside the square brackets.

So for the second array element, the index is one. Save that and refresh the browser and there we are. It‘s displaying the second array element. To add more elements to an existing array we use an empty para square brackets after the variable name and assign the value directly. So let‘s just add a couple more. Square brackets after characters and I‘ll add marvin.

和JS的数组一样,可以通过$myArray[index]的方式访问和修改存储在数组中的变量,也可以增加存储在里面的变量(indexed array 和 associative array都是如此);

 

And then we‘ll also add the creator of the Norwegian fjords. Who was strangely reluctant to reveal his name to Arthur Dent, I wonder why? Slartibartfast. So, now we‘ve got five elements in the array let‘s inspect it by running Print R again.

Comment that out, save it. And what‘s happened is that the new elements have been added to the array. And assigned the next available numbers. Because each number is known as the index, this type of array is normally known as an indexed array. PHP has another type of array called an associative array. Instead of using a numeric index, each array element is stored as a key value pair(一直讲JS的对象和PHP的associative array 搞混,其实他们确实有许多共同之处,但是表示的方法不一样:key value可以看做是JS中object的属性,有异曲同工之妙).

So lets create an associative array for descriptions from the hitchhiker‘s guide. You start with a variable as before and then the assignment operator array. And the key value pars go between the parenthesis as before. First the key which must be a string. So we‘ll have Earth.

And the assignment operator is an equal sign, followed immediately by a greater than sign. So it looks rather like a double arrow. And then the value. Mostly harmless and then a comma, we‘ll add another one. Very often when I create associative arrays, I use a lot of white space and put them one on top of each other so they‘re much easier to read. Don‘t need to do this but it‘s good for reability.

So we will have the second key Marvin and his description, the paranoid android. You can also add new elements to an associative array after its been created in the same way as for an indexed array except that you put the key between the square brackets. So let‘s have another one descriptions and then we‘ll put the key as a string will be Zaphod.

And then you assign the value in the ordinary way. It‘s important to note that you can‘t use an associative array element directly inside a double quoted string. Let‘s just see what happens if I try to do that. So, echo, Earth is described.

As, and then descriptions, and the key is earth, closing quote and semi colon. You can see that my script editor is signalling that there‘s a syntax error, but I want to show you what will happen. If you try to load this into a browser, I‘ll just save that, refresh the browser and we‘ve got this long and complicated message, error message which tells us that oh, you can‘t do that So, how do you get around this? Well the simple answer is that you wrap the associative array element in a pair of curly braces.{associative必须要放在花括号里面}

Soon as I do that, the syntax error goes away. And let‘s just comment out this printer align here. Save and refresh the browser. And now we know that Earth is described as mostly harmless. Arrays are an important part of PHP and you‘ll use them a lot. In the lesson, you learned how to create both index and associative arrays, and inspect their contents with Print R.

Index arrays use numbers to identify each array element. And it‘s important to remember that the numbers begin at zero. You also learn that you need to enclose associative array elements and curly braces. If you want to include them in a double quoted string.

Storing multiple values in arrays