Hudzilla.org - the homepage of Paul Hudson
Contents > Wish List | Report Bug | About Me ]

5     Arrays

This is NOT the latest copy of this book; click here for the latest version.

So far we've looked at the basic variables types such as strings and integers, as well as a variety of functions you can use to manipulate these data types. Beyond the basic data types are arrays and objects, which take a little bit more work in order for you to take advantage of them properly. More importantly, arrays take a solid understanding of functions before you try to use them, which is why they are separated here! As mentioned, objects are covered exclusively in their own chapter - this chapter is dedicated to the topic of arrays.

In order to model our surroundings in a programming environment accurately, it is important to recognise that some types of data naturally group together - colours, for example, naturally clump together into one group. Rather than having hundreds of separate variables - one for each colour - the reality is that it makes more sense to have one variable that holds a list, or array of colours.

Topics covered in this chapter are:

  • Reading arrays

  • Manipulating arrays

  • Multidimensional arrays (arrays of arrays)

  • Saving arrays


Chapter contents

5.1. First steps: array(), count(), print_r(), var_dump(), and var_export()
5.2. Associative arrays
5.3. The three ways of iterating through arrays: list(), each(), and foreach loops
5.4. The array operator
5.5. Returning arrays from functions
5.6. Array-specific functions
5.6.1. Chopping and changing arrays: array_diff(), array_intersect(), and array_merge()
5.6.2. Stripping out duplicate values: array_unique()
5.6.3. Filtering your array through a function: array_filter()
5.6.4. Converting an array to individual variables: extract()
5.6.5. Checking whether an element exists: in_array()
5.6.6. Using an array as a double-ended queue: array_shift(), array_unshift(), array_push(), array_pop()
5.6.7. Swapping keys and values: array_flip()
5.6.8. Sorting arrays: asort(), ksort(), arsort(), and krsort()
5.6.9. Grabbing keys and values: array_keys() and array_values()
5.6.10. Randomising your array: shuffle() and array_rand()
5.6.11. Creating an array of numbers: range()
5.7. Multidimensional arrays
5.8. The array cursor: reset(), end(), next(), and prev()
5.9. Holes in arrays
5.10. Arrays in strings
5.11. Saving arrays: serialize(), unserialize(), urlencode(), and urldecode()
5.12. Summary
5.13. Exercises
5.14. Further reading
5.15. Next chapter



<< 4.27 Next chapter   5.1 First steps: array(), count(), print_r(), var_dump(), and var_export() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
GamingG@firefoxowns.com - 04 Jul 2008

I believe Gayatri was writing the array in PHP syntax, but intended for the information to be retrieved from the database. I was just working on something similar, so here's my code, in case it helps:

$forumq = $g_mysql->query("SELECT * FROM `forums` WHERE `is_category`=0 ORDER BY `forder`");

$allforuma = array();
while($a = $g_mysql->fetch_array($forumq))
{
if(!array_key_exists($a['parent_id'], $allforuma))
{
$allforuma[$a['parent_id']] = array();
}

$allforuma[$a['parent_id']][] = $a;
}

A PHP User - 04 Jul 2008

Check out http://www.php.net/manual/en/function.ksort.php or http://www.php.net/manual/en/function.sort.php.

popey - 04 Jul 2008

To use Gayatri’s array would be to complicated for php in imagine the list having 200 sets of data. The way around this using php would be to use mysql and simply make query

popey - 04 Jul 2008

To use Gayatri’s array would be to complicated for php in imagine the list having 200 sets of data. The way around this using php would be to use mysql and simply make query

A PHP User - 04 Jul 2008

test

A PHP User - 04 Jul 2008

test

A PHP User - 04 Jul 2008

I am not sure if this is the right place to ask for help...I didn't find an exclusive link to do do so....

I have an array like this:
$arr[0][FirstName] = "Jack";
$arr[0][Email] = "jack@yahoo.com";
$arr[0][city] = "Australia";

$arr[1][FirstName] = "Adam";
$arr[1][Email] = "adam@yahoo.com";
$arr[1][city] = "South Africa";

$arr[2][FirstName] = "Eve";
$arr[2][Email] = "eve@yahoo.com";
$arr[2][city] = "Japan";

I would like to sort this array based on FirstName or Email or City...is there are built in function in PHP to do so?

Please help!

Regards Gayatri

chrizPadilay - 04 Jul 2008

Dude in English pls?

chrizPadilay - 04 Jul 2008

Dude in English pls?

A PHP User - 04 Jul 2008

umop apisdn asuasuou se swaas

A PHP User - 04 Jul 2008

iuouoiuoiu oi uoiupo u12

A PHP User - 04 Jul 2008

iuouoiuoiu oi uoiupo u12



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is zero plus five?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow