![]() |
Part 1: Getting Started with Array Functions
Arrays can be extremely useful. It's just when someone asks in what instance would you use an array, I end up being quite stumped to give a decent example. The truth is that arrays are used everywhere, from returning information from a database table, or storing models for your code. They come in a few forms: basic arrays or numeric arrays, multidimensional arrays and associate arrays.
In its most basic form, an array can be thought of as a collection of related (or sometimes unrelated) values. For example, if I had a collection of fruit I could store them in an array like this: PHP Code:
PHP Code:
There are an abundance of array functions that PHP has straight out of the box. Although there are too many to mention in one article, a few key array functions you may wish to familiarise yourself with:
Let me mention a few of these functions, the ones you are likely to use if you are a beginner to PHP. Push PHP Code:
Unshift PHP Code:
Pop PHP Code:
Shift PHP Code:
|
Nice, simple, to the point. =)
You have "part 1". This this a series about arrays or php in general? |
Was intended just for arrays, I'm afraid :( At the time of writing I couldn't be bothered to fit it into 1 article :) !
|
Okay, Well I'd just like to say I've found one more function particularly useful when dealing with arrays:
array_map A handy way to run a function on each element of an array. It saves running a loop, running the function on the element then saving it back to the array. PHP Code:
|
To further the last post, above, there is also the
array_walk function which behaves similarly (but not the same!) as array_map.array_walk (PHP manual page) This does the same thing as the array_map example above. PHP Code:
|
Cool, I actually didn't know that! That would probably be the better choice then :)
|
So there will be a part 2 about arrays? ^^
|
| All times are GMT. The time now is 05:23 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0