Skip to content

Instantly share code, notes, and snippets.

@prodeveloper
Created October 10, 2013 18:45
Show Gist options
  • Save prodeveloper/6923382 to your computer and use it in GitHub Desktop.
Save prodeveloper/6923382 to your computer and use it in GitHub Desktop.
Snapshot showing use of array_map and foreach in a project that capitalizes strings.
<?php
/**
* Loops array using array walk/ map
*/
class LoopMap extends Loops {
function __construct($array) {
$this -> array = $array;
}
function capitalize_names(){
return array_map(array($this,'_capitalize_name'),$this->array);
}
}
/**
* Loops test array using the conventional for loop
*/
class LoopFor extends Loops {
function __construct($array) {
$this -> array = $array;
}
function capitalize_names(){
foreach ($this->array as &$name) {
$name=$this->_capitalize_name($name);
}
return $this->array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment