Get Array Values Recursively with PHP
I've been helping to write a WordPress plugin (I'm not ready to share it yet) and one of the tasks required is validating an array of user-selected values against a list of known valid values. The known valid array is actually a key=>value
array so unfortunately array_values
wont help get the simple list I'd like.
Instead a more advanced custom function was needed:
// http://php.net/manual/en/function.array-values.php
function array_values_recursive($array) {
$flat = array();
foreach($array as $value) {
if (is_array($value)) {
$flat = array_merge($flat, array_values_recursive($value));
}
else {
$flat[] = $value;
}
}
return $flat;
}
This recursive function dives into arrays, even key=>value
arrays, to retrieve the final list of values. Thank you PHP.net!
Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS. What we end up doing is repeating the same properties multiple times with each browser prefix. Yuck. Another thing we...
My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...
Another reason that I love Twitter so much is that I'm able to check out what fellow developers think is interesting. Chris Coyier posted about a flashlight effect he found built with jQuery. While I agree with Chris that it's a little corny, it...
Image reflection is a great way to subtly spice up an image. The first method of creating these reflections was baking them right into the images themselves. Within the past few years, we've introduced JavaScript strategies and CANVAS alternatives to achieve image reflections without...
No need to create a new array, just use iterators. There is an interface for iterators with values that can be iterators called RecursiveIterator. To wrap a nested array there is RecursiveArrayIterator. And to flatten an RecursiveIterator you can use RecursiveIteratorIterator, which by default only returns the leaves.
If you want to convert it to an array use iterator_to_array():
Although if you’re going to call
iterator_to_array()
I think you’ve defeated your stated purpose for using iterators.I just love this blog, I always get to know something new. I didn’t know about this. Very useful!