Jump to content


- - - - -

P8: Arrays


  • Please log in to reply
No replies to this topic

#1 DraxxTV



  • SuperGeek
  • PipPip
  • 14 posts

Posted 06 July 2014 - 06:43 PM

Normally when handling multiple types of information, you would have each piece stored in as a different variable.  For example, if you had items on a shopping list, you would have them stored like this:

$item1='tooth brush';
$item2='tooth paste';
$item4='orange juice';

Creating Arrays

This seems simple enough but when it comes to using this information, it can be quite awkward. There is a better way of dealing with this. You can put it in to an array. You can create an array with these variables like follows:

$shoppingList= array($item1, $item2, $item3, $item4);

You could actually add values to an array with the value instead of a variable like this:

$shoppingList= array('tooth brush','tooth paste','bananas','orange juice');

This would also create an array.

Adding Items to Arrays

Once you have an array, you may need to add to it at a later time. When it comes to doing this, you wouldnt really want to create a new one so the way you would add an item to this array is as follows:


This would then add pasta to our shopping list.

Displaying Arrays

Now that you have your array, you may get to the point where you want to view it. You cant try and echo it because all this would say is 'Array', so what do you do? Well its simple.


When displaying this in the browser it should look like this

Array ( [0] => tooth brush [1] => tooth paste [2] => bananas [3] => orange juice [4] => pasta )

However, to make proper sense of this, it is a good idea to view the source of a webpage. It should then be displayed as

    [0] => tooth brush
    [1] => tooth paste
    [2] => bananas
    [3] => orange juice
    [4] => pasta

This makes a lot more sense.  

Accessing part of arrays

The next thing you may want to do is to display a certain item in an array. The way you would do this is to find the id of the array and print that out. For this we want to print the second item in the array we would do the following

echo $shoppingList[1];

This would then show 'tooth paste'

If you wanted to go through each item in the array programatically and for instance, display them as check box's, you could do so like follows:

echo "Items in my Shopping List:<br>";
foreach ($shoppingList as $item){
    echo '<input type="checkbox" name="shoppingList" value="'.$item.'">'.$item.'<br>';

The way this works is it goes to the array and sets each value as $item. It then generates the HTML to make a check box. This should then output the following:

Items in my Shopping List: ([ ] = Check box)
[ ] tooth brush
[ ] tooth paste
[ ] bananas
[ ] orange juice
[ ] pasta

To find out more about how to use different types of loops, check out my next tutorial

Splitting Strings in to Array

The next thing I'm going to show you is how you can take a string and spit it in to an array. The way this would be useful is if for example you had a string like the following:

$shoppingListString='fish fingers,carrots,soup,coke,apples,orange';

As you can see, that is just a string full of different items which I would like to have in a shopping list. The way you would then convert this string is to split it every ',' as that is the character which is used to split each item. To split it in to an array, you would explode it. You can do so like this

$shoppingList = explode(',',$shoppingListString);

If you then printed this new array, you should see the following:

    [0] => fish fingers
    [1] => carrots
    [2] => soup
    [3] => coke
    [4] => apples
    [5] => orange


The last thing I will be talking about is multidimensional arrays. This is basically an array but with multiple levels. To explain this, I will be using the same type of array we are currently using but to make it multidimensional I will be adding the cost of each item.  As I will be talking about prices etc I'm going to make it a little but more computer related.

$computerParts = array(
    'SandDisk 120GB SSD' => '82.99',
    'Seagate 2TB HDD' => '69.99',
    'AMD FX-6 6100' => '85.00',
    'Intel Core I5' => '158.99'

If you wanted to then go and access information with this using a foreach loop, you could do the following

foreach($computerParts as $part => $price){
    echo $part." Costs &pound;" .$price . "<br>";

This would then output

SandDisk 120GB SSD Costs £82.99
Seagate 2TB HDD Costs £69.99
AMD FX-6 6100 Costs £85.00
Intel Core I5 Costs £158.99


Removing Items in Array

If you would like to remove an item from an array, you can do so like this:


This would then remove the fish fingers from the array.

Removing an Array

To completely remove an array, you would do this:


Resetting an Array

If you would like to remove everything from an array you would do the following:


Finding Length of Arrays

If you wanted to find out how many items you have on your shopping list then you can do so using the count function

echo count($shoppingList);

This would then tell you how many items there are in the array.

Also tagged with one or more of these keywords: PHP4Beginners