• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

PHP array_walk(): Run an array through a function

By Akash Mehta | on Apr 22, 2008 | 0 Comment
PHP Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

If you’ve been developing with PHP for a while, you’ve probably come across this situation in the past:

<?php
foreach ($somearray as &$element) {
    $element = some_function($element);
}

It’s a common sight: taking an array and running (well, walking) its elements through a particular function. Luckily, PHP provides a simple yet powerful function to overcome this: array_walk().

Usage
Using array walk is simple. It takes two arguments, an array of data and a callback function to pass the array to. It examines the array and calls the callback function with each element of the array, allowing you to run the entire array through the function without extracting the array yourself. Consider this:

<?php
function some_function(&$element, $key) {
    return $element + 1;
}

// This:
foreach ($somearray as $key=>&$element) $element = some_function($element, $key);

// becomes this:
array_walk($somearray, "some_function");

It’s cleaner, faster and more effective. It also gives your callback function more information than you might usually provide. The callback parameter can be any : “function_name”, array(‘Class_name’, ‘method_name’) or even array($object, ‘method_name’) (where $object can be $this as needed).

Syntax
The syntax is very simple:

array_walk (array &$array, callback $callback[, mixed $userdata]);

The first parameter is the array that you want to run through the function. The second is the callback – for a function, class method or object method. The third allows you to pass a third parameter to the callback function from within the current context. This can be anything at all, and along with the array element and its key/index, will be passed directly to the function without modification.

For more details, see array_walk() in the PHP manual.

Share this story:
  • tweet

Tags: php arraysPHP Tutorials

Author Description

No Responses to “PHP array_walk(): Run an array through a function”

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 0Followers 496Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    June 2013
    M T W T F S S
    « Oct    
     12
    3456789
    10111213141516
    17181920212223
    24252627282930

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.