Home arrow PHP arrow Page 5 - Array Manipulation With PHP4

Slice And Dice - PHP

This may come as a bit of a shock to you, but PHP's arrayfunctions can do a lot more than just count the elements of an array oriterate through key-value pairs. This article takes an in-depth look atPHP's less well-known array manipulation tools, illustrating how they canbe used to write tighter, more efficient code...and have some fun in thebargain as well!

TABLE OF CONTENTS:
  1. Array Manipulation With PHP4
  2. Having Your Cake
  3. When Size Does Matter...
  4. Push And Pull
  5. Slice And Dice
  6. Where Am I?
  7. Sorting Things Out
  8. Flipping Out
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 8
November 09, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
PHP allows you to extract a subsection of the array with the array_slice() function, in much the same way that the substr() function allows you to extract a section of a string. Here's what it looks like:

array_slice(array, start, length)
where "array" is an array variable, "start" is the index to begin slicing at, and "length" is the number of elements to return from "start".

Here's an example:

<? // create array $colours = array("red", "green", "blue", "yellow"); // returns the array ("green", "blue") $slice = array_slice($colours, 1, 2); ?>
You can also use a negative index for the "start" position, to force PHP to begin counting from the right instead of the left.

<? // create array $colours = array("red", "green", "blue", "yellow"); // returns the array ("green", "blue") $slice = array_slice($colours, -3, 2); ?>
The array_splice() function allows you to splice one or more values into an existing array. Here's what it looks like:

array_splice(array, start, length, replacement-values)
where "array" is an array variable, "start" is the index to begin slicing at, "length" is the number of elements to return from "start", and "replacement-values" are the values to splice in.

Here's an example:

<? // create array $trio = array("huey", "dewey", "louie"); // section to be spliced in $splice = array("larry", "curly", "moe"); // $trio now contains ("huey", "larry", "curly", "moe") array_splice($trio, 1, 2, $splice); ?>


 
 
>>> More PHP Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: