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!
With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements. CSS gradients are another step in that direction. Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...
CSS and JavaScript: the lines seemingly get blurred by each browser release. They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely. We have our .js files and our .css, but...
CSS filters aren't yet widely supported but they are indeed impressive and a modern need for web imagery. CSS filters allow you to modify the display of images in a variety of ways, one of those ways being displaying images as grayscale.
Doing so requires the...
We all know that each browser vendor takes the liberty of implementing their own CSS and JavaScript features, and I'm thankful for that. Mozilla and WebKit have come out with some interesting proprietary CSS properties, and since we all know that cementing standards...
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!