TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Part 1: Getting Started with Array Functions (http://www.talkphp.com/absolute-beginners/1213-part-1-getting-started-array-functions.html)

Wildhoney 09-24-2007 04:23 PM

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:

$aFruits = array('Apples''Pears''Oranges'); 

Then I could access those 3 items fairly easy. Arrays, by default, begin at index 0. That is, to echo out Apples I could reference it like so:

PHP Code:

echo $aFruits[0]; 

PHP then counts up from 0 and goes on indefinitely. We have 3 elements in our arrays: 0, 1 and 2, storing Apples, Pears and Oranges, respectively. This is all pretty basic stuff.

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:
  • array_key_exists() : Checks to see if the key specified exists in the specified array. In our array above, 2 is a valid key, but 3 is not.
  • array_merge() : Combines one array with another array. For example, we could merge our fruit array with a member array. This would create one larg array containing both fruits and members.
  • array_push() : Push an element onto the end of our array.
  • array_pop() : Removes the element key from our array. So in our example, oranges would disappear from our array and leave us with 2 remaining elements, Pears and Apples.
  • array_shift() : Removes the first element from our array. So in our example, apples would disappear from our array, leaving us left with Pears and Oranges.
  • array_unique() : Removes all duplicate elements from an array.
  • array_unshift() : Push an element onto the beginning of our array.

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:

$aFruits = array('Apples''Pears''Oranges');
array_push($aFruits'Kiwi'); 

Result: Pushes the value Kiwi onto the end of our array.

Unshift

PHP Code:

$aFruits = array('Apples''Pears''Oranges');
array_unshift($aFruits'Kiwi'); 

Result: Pushes the value Kiwi onto the beginning of our array.

Pop

PHP Code:

$aFruits = array('Apples''Pears''Oranges');
array_pop($aFruits); 

Result: Removes Oranges from our array.

Shift

PHP Code:

$aFruits = array('Apples''Pears''Oranges');
array_shift($aFruits); 

Result: Removes Apples from our array.

jordie 09-29-2007 03:52 AM

Nice, simple, to the point. =)

You have "part 1". This this a series about arrays or php in general?

Wildhoney 09-29-2007 11:30 AM

Was intended just for arrays, I'm afraid :( At the time of writing I couldn't be bothered to fit it into 1 article :) !

jordie 09-30-2007 05:43 PM

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:

$MyInputArray array_map("htmlspecialchars",$MyInputArray); 

Result: Each element in the array will have the htmlspecialchars() function run on it. You can use your own custom functions as well, just pass the name in as a string without the brackets. Read more @ php.net

Salathe 09-30-2007 08:47 PM

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:

array_walk($MyInputArray'htmlspecialchars'); 

The important difference being that array_walk acts on the array itself, whereas array_map acts on a copy of it (sort of) by default.

jordie 10-01-2007 01:53 AM

Cool, I actually didn't know that! That would probably be the better choice then :)

Tanax 10-01-2007 10:53 AM

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