TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-24-2007, 04:23 PM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Wink 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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-29-2007, 03:52 AM   #2 (permalink)
The Wanderer
 
Join Date: Sep 2007
Location: Sydney, Australia
Posts: 19
Thanks: 0
jordie is on a distinguished road
Default

Nice, simple, to the point. =)

You have "part 1". This this a series about arrays or php in general?
jordie is offline  
Reply With Quote
Old 09-29-2007, 11:30 AM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Was intended just for arrays, I'm afraid :( At the time of writing I couldn't be bothered to fit it into 1 article :) !
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-30-2007, 05:43 PM   #4 (permalink)
The Wanderer
 
Join Date: Sep 2007
Location: Sydney, Australia
Posts: 19
Thanks: 0
jordie is on a distinguished road
Default

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
jordie is offline  
Reply With Quote
Old 09-30-2007, 08:47 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Old 10-01-2007, 01:53 AM   #6 (permalink)
The Wanderer
 
Join Date: Sep 2007
Location: Sydney, Australia
Posts: 19
Thanks: 0
jordie is on a distinguished road
Default

Cool, I actually didn't know that! That would probably be the better choice then :)
jordie is offline  
Reply With Quote
Old 10-01-2007, 10:53 AM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

So there will be a part 2 about arrays?
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:42 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design