note 100608 modified in function.array-walk-recursive by danbrown
| From: | danbrown@php.net | Date: | Tue, 17 May 2011 16:58:53 +0000 |
| Subject: | note 100608 modified in function.array-walk-recursive by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-179822@lists.php.net to get a copy of this message | ||
If you don't really particularly care about the keys of an array, you can capture all values
quite simply:
<?php
$sample = array('dog' => 'woof', 'cat' =>
array('angry' => 'hiss', 'happy' => 'purr'),
'aardvark' => 'kssksskss');
$output = array();
// Push all $val onto $output.
array_walk_recursive($sample, create_function('$val, $key, $obj', 'array_push($obj,
$val);'), &output);
// Printing
echo nl2br(print_r($output, true));
/*
* Array
* (
* [0] => woof
* [1] => hiss
* [2] => purr
* [3] => kssksskss
* )
*/
?>
[EDIT BY danbrown AT php DOT net: In a note added by 'FaustoFilho' on 17-MAY-2011, the
following information was appended to this note.
[If you intend to use this] "function to extract the last key value of an array, don't
forget to insert a currency sign ($) signal before '$output' at 7th line.
This value must be a variable, and if you forgot to assign this signal, your code won't work,
displaying an error like this:
Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in
/path/to/script.php on line 7."]
--was--
If you don't really particularly care about the keys of an array, you can capture all values
quite simply:
<?php
$sample = array('dog' => 'woof', 'cat' =>
array('angry' => 'hiss', 'happy' => 'purr'),
'aardvark' => 'kssksskss');
$output = array();
// Push all $val onto $output.
array_walk_recursive($sample, create_function('$val, $key, $obj', 'array_push($obj,
$val);'), &output);
// Printing
echo nl2br(print_r($output, true));
/*
* Array
* (
* [0] => woof
* [1] => hiss
* [2] => purr
* [3] => kssksskss
* )
*/
?>
http://php.net/manual/en/function.array-walk-recursive.php