ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

Back to basics: PHP & Arrays

(Page 2 out of 3)

Commonly used Array functions

The functions below are a set of array functions you will be using a lot in any given script, and are vitally important when using arrays.

print_r()
This function is used to output a whole array structure, and is extremely useful when trying to debug your PHP script. It is used like so:


$myarray = array('One', 'Two', 'Three');
echo '
';
print_r($myarray);
echo '
'
;
?>

For debugging purposes, I highly recommend the show() function by Keith Devens, which can be found at his website. It incorporates the print_r function, but also makes sure all the output is 'safe'.

is_array()
This function does exactly what it says: checks whether a given variable is an array or not. Returns true or false, and is used like this:


if (is_array($myvar) == true) {
        echo "Yessir, it's an array!";
} else {
        echo "'Fraid not sir, no array here";
}
?>

count()
This function does only one thing: it counts how many elements there are in an array, like so:


$myarray = array('One', 'Two', 'Three');
echo count($myarray); // Outputs 3
?>

explode()
This function is used to change a regular variable into an array, by separating it on a certain character. Takes two arguments: the separator (usually a comma) and the variable, and returns the array. Used like this:


$myvar = 'One,Two,Three';
$myarray = explode(',', $myvar);
?>

implode()
This function is the exact opposite of the explode function, and changes an array into a regular variable with a separator, like so:


$myarray = array('One', 'Two', 'Three');
$myvar = implode(',', $myarray);
?>

array_map()
This function is used to apply a callback function to each item of the array. Let's say for example you want to make sure there is no extra white space at the beginning and end of each item. Normally, you'd use the trim() function to remove all white space from a variable, but you can't do this with an array. That's where the array_map() function comes in the picture, and it's used like this:


$myarray = array('  One', 'Two  ', ' Three');
$myarray = array_map('trim', $myarray);
?>

The functions above are the one's you'll be using most, but there are a lot more arrays functions, which can all be found on the Array Functions page in the PHP Manual.

Looping through an array

Another great aspect of arrays is the ability to go through them, and using each item, without even knowing how much items there are in the array. The easiest way to do this is to use a foreach() loop, like so:


$people = array('Dennis', 'Joe', 'Jane');

foreach ($people as $person) {
        echo $person . '';
}
?>

The above code outputs each person, without knowing how much people there are. Another way to go through an array is using a regular for() loop, which looks a bit like above:


$people = array('Dennis', 'Joe', 'Jane');

for ($i = 0; $i < count($people); $i++) {
        echo $people[$i] . '';
}
?>

This code displays the exact same as the above code, and it walks through the array the exact same way, using a for() loop. This also demonstrates another possibility of arrays: you are able to use dynamic keys, like I used the $i variable as key.

You can also go through an array, and get both the key and the value of the item, using a different version of the foreach loop, like this:


$people = array('Dennis' => 'Pallett', 'Joe' => 'Doe', 'Jane' => 'Doe');

foreach ($people as $key => $value) {
        echo $key . ' - ' . $value . '';
}
?>

It is also possible to traverse an array using other ways (e.g. a while() loop or using the list() function), but in all my years of PHP programming I've never had to use it. The for and foreach loops are probably good enough 99% of the time.

Let's look at the last part of using arrays: sorting the items in a certain way.

« Previous: Introduction & Basics
Next: Sorting Functions »



5 Responses to “Back to basics: PHP & Arrays”

  1. OAKA Blog » Blog Archive » Using Arrays in PHP Says:

    […] Site: http://phpit.net/article/back-to-basics-arrays/1/ […]

  2. CubSpeaking » Using Arrays in PHP Says:

    […] Site: http://phpit.net/article/back-to-basics-arrays/1/ […]

  3. Bocse L Filip Says:

    The natural algorythm is not only the human-like ordering algo. If a machine would see strings the way we do the machines would also naturally sort in the same manner as humans…. only difference is the representation of a string.
    Of course we see strings as a set of various characters different by shape, meaning and so on… not the machines though… these only see 1’s and 0’s .It is true these beautifull machines have the ability to represent these 1’s and 0’s in a human understandable representation but it is just like a translation from their language (1’s and 0’s ) to a language which they do not understand… based on a strict dictionary.
    It is sad :) but true… the machines have no imagination. May one teach a machine what imagination is it is very likely our natural algo will seem redundant to whatever these machines may consider “natural algo”.

  4. Secret Black Book of IceFuzion » Back to basics: PHP & Arrays Says:

    […] read more | digg story […]

  5. Ocus Says:

    Playing The Angel

    PHPit - Totally PHP &r…

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Basics
  2. Commonly Used Functions
  3. Sorting Functions
Bookmark Article
Download Article
PDF
Download this article as a PDF file