How to work with PHP arrays
Description
Chapter: PHP and MySQL Basics
Transcript
Hi, this is Chris Shattuck with BuildaModule dot com, and in this video we're going to be talking about working with arrays in Drupal. So this lesson is geared towards folks that maybe don't have a PHP development background but that might need to get into a Drupal website and modify some code or potentially add some functionality by creating a new module and coding in there. So we're going to start out by talking about what an array actually is and why we would want to use one in Drupal.
Next, we'll look at how to identify an array. So if you have a variable, how do you make sure that that's an array so you can apply these techniques to it? Next, we'll look at some examples of arrays from a Drupal module called blog and these are examples that are very similar to structures that you would use in your own custom module. Next, we'll look at a couple of methods for creating an array and we'll move on to being able to get data from an array and add to or modify an array, probably two of the most useful techniques when working with Drupal because arrays are used so extensively.
We'll move on to a more advanced technique which is looping through an array and performing some action on every element in it. And finally, we'll take a look at some useful PHP functions that allow you to do some pretty advanced stuff with arrays without having to write your own custom code for it. Okay.
Let's go ahead and get started by talking about what an array is and why we would want to use one in Drupal. Probably the first thing to wrap your mind around is that an array is a variable and as a variable its main purpose is to store data. But unlike simple variables like strings or integers, an array can contain multiple pieces of information and each piece of information can have a name.
So an array allows us to map names to values and the way we would typically talk about this when we're talking about arrays are values to keys. So values are the data and keys are the names. Because we can do that, we can then access those bits of information by identifying their name in a particular technique that we'll cover later under getting data from an array.
Because we can put in multiple pieces of information into an array and that sort of structure can be very flexible, for example, we can put an array inside of an array or multiple values inside of an array that are different types of variables, we can structure data around the way that they're naturally occurring inside of our code. So if we want to store data about a user, say, we have some basic information like their email address and their name and their ID, but then we have another piece of information which contains all of their settings that they use for their site, we might want an array inside of that array then to store those settings. So arrays are very flexible and that's why they are used to extensively throughout Drupal.
The reason why you will need to learn how to use arrays is that Drupal provides us with a number of ways to sort of hook into processes before they complete. So these are called Drupal hooks. Typically, in order to hook into one of these processes, Drupal will provide us, by us I mean a custom module, with a set of data that usually comes in the structure of an array.
So if we want to manipulate that data before its output, we need to be able to work with that array. One example is with Drupal forms. So if we want to manipulate a form before it gets displayed to the browser, we would need to be able to manipulate an array that contains settings about the form such as the inputs, what elements are required, for example, and we can edit those as long as we can edit an array, and then pass it back to Drupal to finish handling.
All right. So now that you know what an array is and why we would want to use one in Drupal, let's go ahead and talk about how to identify an array. There are a number of techniques you can use to get the values that are inside of a variable.
Probably the most powerful of which is using a PHP debugger. If you're interested in that, there are some videos on the site that show how to set one of those up. But we're going to go real simple here and use a single PHP function called print_r() to get basically the same effect.
So we're going to pass the print_r() a variable of some kind and what print_r() will do is output some information about the variable including its type and what information is included in it. So in this example I've provided a dummy array and when I output it what I get is the first line saying what the type of variable it is. So here you see it's an array that's followed by a parenthesis and between these parentheses are the values that are included inside of this array.
And you can see here it's showing us the key and the value for each element inside of the array. So value 1 is assigned a 0, value 2 is assigned a 1, and value 3 is assigned a 2. So let me show you a quick example in some code I'm working on here so you can see it in action.
So I'm going to type print_r and then I'm going to output panes. Now, this code helps me with the output of this page on Firefox that you can see that contains the lesson plan basically for this video. When I add print_r() to the code, what it's going to do is output this array so you can see up here that we're assigning some different values to this array and it will output it in a nice clean format that's pretty easy to read.
What I want to do here is view the source code for Firefox so I'm going to go to View and then Page Source, and that's going to bring up a screen that will show us the source code for this page. What it will also do is refresh the page. So now we see it with that print_r() in action.
So here we have at the very top of the screen a word that says "Array" that indicates that this variable, the panes variable is an array, and then we have a parenthesis which indicates that this is the start of the values inside of that array. And you can see here the key inside of the brackets. There's a number in this one but it's a string.
So this is an indication of the flexibility of arrays that we can use not just numbers for the keys but strings. And then the value itself is a variable. It's an array.
So we see that with a capitalized Array here. And then the start of a parenthesis which indicates that the following is content inside of this array here, and then it shows us the inside of the content key there is this data. And so this is the value for this key and it just contains some HTML code.
So if we continue down here, you can see that this is completely exploded and you can just follow the depth of the array by looking at the indentation before a variable. Let's go ahead and move on to looking at some real life examples from some Drupal code. These three examples from the blog module provide a demonstration of how flexible arrays can be and some different ways of working with them and following this we'll take a look at basically the same structures but in a simpler format.
But I just want you to see how these are used in action. So all three of these functions are hooks that are supplied by Drupal to provide particular functionality to plug in to processes before they complete. So this first one is hook_perm() which allows us to add permissions from our custom module.
And what's happening in here is that it's returning a value which is creating a variable by using the array function and then passing the array function several values. So you can see these are all because they're strings they have parentheses around them, and then they're separated by commas. And so you can see this array here would have one, two, three, four, five elements in it.
so real simple way to create an array. We're not assigning any keys to it explicitly by numerical keys get added to it implicitly and we'll show you how that works in a couple of steps down this lesson. Next is the blog_node_info() ufnction and you can see here it's doing a very similar thing except I wanted to demonstrate how you can use an array inside of an array.
So we have the array function and we're taking a key called blog and we're assigning it an array. So this array is a variable inside of an array. Really simple and then inside of here we have three elements.
So that's just another way to define an array. This time an array inside of an array. In this third example, we're using kind of a different syntax for both creating our array and adding items to it.
So in our previous examples we added the items to the array all at once. In this example, what we're doing is adding items to the array one at a time. So this first line here we're not even creating an array explicitly by using the array function but by using a variable with brackets we can assign a key to a value.
And this tells PHP, "Hey, this is an array that we're working with. " So we have the variable which is the array and we're assigning a key here, in this case, blog a value which is another array. So this is actually a nested array even though you only see one array function.
So it's a very similar structure ultimately to what you see up here under blog_node_info(). This item is added and you can see that the adding is ended with a semicolon at the end of the line, and then a new item is added the same way and several items are added below that. And then finally, the function returns that full array.
Okay. Now, let's take a look at a couple of simpler examples. The simplest way to create an array is to use the array function and not even pass it any values.
There might be some cases where we'd want to do that. For example, if we scroll up to our previous example in the blog module and we look at the blog_menu() function, we can see here that there's a variable being used that hasn't been defined yet. We're basically defining the variable as an array by assigning it a key using this bracket syntax here.
But this kind of thing can throw up a PHP error on a server depending on the server configuration. To avoid this we can explicitly assign this items variable to an array by using the array function. Let's go back to the examples here.
So if want to create a simple array, we'll use the same syntax except we'll just pass it some values separated by a comma. An associative array is when we're using keys as well as values and the syntax is a little bit different. So here we have the variable that we're going to assign the array.
We are using the array function and then we're using a set of key value pairs separated by this construct which is an equal sign and a right facing caret and this will assign these keys to these values all the way through and we can continue to add it. Note that I'm adding a comma at the very end but this isn't necessary. This just makes it easier if we're going to be editing the code later.
So it's typical convention in Drupal coding to leave that comma there at the end even though it doesn't really have a purpose. We can also use nested or mult ...
When you have an active membership, you will be able to see your progress here.
Skill level: Beginner - Advanced
This is the original BuildAModule series on Drupal 6 development along with coverage of some additional tools for developers. Weighing in at nearly 9 hours, with over 30 videos, this is a great way to get started with Drupal 6 development.
Some of the key points we'll be covering include:
- How to work with the most important Drupal 6 APIs
- How to build, validate and process forms with the Form API
- The basics of working with jQuery for building dynamic interfaces
- The basics of using MySQL to grab data, insert or update from the database
Who this collection is for
This collection is intended mostly for PHP developers who want to learn how to code for Drupal.
Prerequisites
Having a solid foundation in working with Drupal on the front end will help you understand why we need to approach certain tasks in the way we do. Also, a basic understanding of PHP will be useful.
Chapters
- 212:29Essential Concepts
- 3:30How to install and uninstall a module
- 14:03How to build your first Drupal module
- 13:47Introduction to hooks
- 8:21How to add permissions
- 27:13Introduction to the Form API
- 12:44How to make your module customizable
- 21:33How to add and configure blocks
- 10:13How to add JavaScript and CSS
- 15:07How to theme a Drupal module
- 16:18How to create an install script
- 19:00How to create, format and validate a form
- 21:24How to improve form validation and process a form
- 18:47How to create edit and delete forms and alter other forms
- 10:29How to apply for a Drupal CVS account
- 27:05Working with jQuery and Javascript
- 67:03Securing a Module
- 58:49Testing and Debugging a Module
- 96:31PHP and MySQL Basics
- 73:25Using Komodo Edit as an IDE
- 5:23How to create a project in Komodo Edit
- 8:59How to work with projects in Komodo Edit
- 11:03How to work with files in Komodo Edit
- 12:28Understanding the Komodo Edit interface
- 10:19How to create snippets with variables and options
- 11:26Advanced snippet usage: Key binding, tabstops and abbreviations
- 13:47How to use templates in Komodo Edit
Videos

Add to , or
Add to new playlist:
Add to cart: