Subscribe to RSS Feed

PHP Arrays

November 9, 2008 by Shaun


No Gravatar

So my last post was about Authorize.NET and PHP. It received quite a bit of attention. For that reason, I’m focusing again on PHP and I’m planning to elaborate on the use of arrays. Arrays are useful for many purposes. I’ve used them to store session data, pass values into functions, as well as a streamlined method of managing and validating data.

Arrays, from my understanding, are more easily parsed by the php engine which improves performance. They also allow for you to pass an undefined number of variables into a function without having to declare them ahead of time.

It’s easy to set an array too. The syntax can be as simple as:

$mypets = array("Fluffy,"Spike");

In the above example, two values are being added to the array: Fluffy and Spike. Since arrays store data in key=>value pairs (which will be explained in a moment), we have simply added 2 values to the array.

key=>value pairs refers to the ability to store data with an identifier and a value. In the preceeding example, we added two names, but did not specify a key (identifier). If no key is specified, arrays use numbers, starting from 0. So in the above example, Fluffy would be in position 0, and Spike would be in position 1. We’ll dive into how to access these values in a couple minutes.

You can also specify the key for “fluffy” and “spike” to help keep the data easier to recall. Since Fluffy is a cat and Spike is a dog, we’ll use those as the keys:

$mypets=array("cat"=>"Fluffy", "dog"=>"Spike");

As you can see, rather than using the default numerical keys, we have just set them to identifiers that are easier to identify. In addition, it doesn’t matter whether Fluffy or Spike are set first. You can always count on Fluffy being a cat and Spike being a dog.

To pull the values from the array depends on how you added them; although the process is very similar. If you provided a customized key as in dog and cat; you can access the values as follows:

$mycat = $mypets['cat']; // returns Fluffy
$mydog = $mypets['dog']; // returns Spike

If you don’t provide a customized key, then you must use the numerical key. Always remember that the values are accesible in the order they are added, so if you add Fluffy first, her key is 0.

$mycat = $mypets['0']; // returns Fluffy
$mydog = $mypets['1']; // returns Spike

The above examples explain how to set and retrieve values. There are a bunch of tools you can use with arrays, which I might dive into deeper within another post. Here are a few I encourage you to lookup and see if you can figure them out. Again, if you have any questions, feel free to comment here and we’ll do our best to help out.

Common Array Functions/Utilities:
in_array()
foreach()
array_keys()
array_push()
explode()
implode()
count()

Tags: , , , ,

2 Responses to “ PHP Arrays ”

  1. - Are You Riled Up? - » Blog Archive » Php Arrays - Olebox - Shaun Oleson
    November 10, 2008 at 2:43 am

    [...] In the above example, two values are being added to the array: Fluffy and Spike. Since arrays store data in key=>value pairs (which will be explained in a moment), we have simply added 2 values to the array. …[Continue Reading] [...]

  2. Gladis DemuzioNo Gravatar
    March 18, 2010 at 4:40 pm

    Great guide,
    array functions have always been hard to understand properly.

Leave a Reply

This site is using OpenAvatar based on OpenID

Olebox - Shaun Oleson is using WP-Gravatar