Storing multiple values in Arrays

Source: Internet
Author: User

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 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.

Like the JS array, you can access and modify the variables stored in the array in the form of $ myarray [Index, you can also add the variables stored in (both indexed array and 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 (I have been talking about mixing JS objects with PHP's associative array. In fact, they do have many commonalities, but the representation method is different: the key value can be regarded as the object attribute in JS, Which is similar ).

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 doesn't 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 must be placed in curly brackets}

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.