Multidimensional Arrays Nested to Unlimited Depth - php

I have a multidimensional array nested to an unknown/unlimited depth.
I'd like to be able to loop through every element.
I don't want to use, foreach(){foreach(){foreach(){}}} as I don't know the depth.
I'm eventually looking for all nested arrays called "xyz". Has anyone got any suggestions?

I'm eventually looking for all nested arrays called "xyz". Has anyone got any suggestions?
Sure. Building on the suggestions to use some iterators, you can do:
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($array),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $key => $item) {
if (is_array($item) && $key === 'xyz') {
echo "Found xyz: ";
var_dump($item);
}
}
The important difference between the other answers and this being that the RecursiveIteratorIterator::SELF_FIRST flag is being employed to make the non-leaf (i.e. parent) items (i.e. arrays) visible when iterating.
You could also make use of a ParentIterator around the array iterator, rather than checking for arrays within the loop, to make the latter a little tidier.

Recursion.
Write a function that walks one array; for each element that is also an array, it calls itself; otherwise, when it finds the target string, it returns.

There is a vast difference between unknown and unlimited. However, you can make use of the SPL Iterators instead of using multiple nested foreach loops.
Example:
$array_obj = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($array_obj as $key => $value) {
echo $value;
}

Take a look to the RecursiveIteratorIterator interface.
$interface = new RecursiveIteratorIterator( new RecursiveArrayIterator($your_array) );
foreach($interface as $k=>$v) { /* your function*/ }

Using the comments above, I've found the answer:
function findXyz($array){
foreach($array as $foo=>$bar){
if (is_array($bar)){
if ($bar["xyz"]){
echo "<br />The array of xyz has now been found";
print_r($bar['xyz']);
}else{
findXyz($bar);
}
}
}
}
findXyz($myarray);
This loops through all nested arrays and looks for any element who has a sub-array of xyz, as per my original request. array_walk_array and RecursiveIteratorIterator were unable to achieve this.

Have you thought about using array_walk_recursive for this?
Another (slower) approach would be to flatten the array first before performing a search, ie:
$myarray = array('a','b',array(array(array('x'),'y','z')),array(array('p')));
function array_flatten($array,$return)
{
for($x = 0; $x <= count($array); $x++)
{
if(is_array($array[$x]))
{
$return = array_flatten($array[$x],$return);
}
else
{
if($array[$x])
{
$return[] = $array[$x];
}
}
}
return $return;
}
$res = array_flatten($myarray,array());
Or, for a recursive search, see here for an example:
function arrayRecursiveSearch($needle, $haystack, $path=""){
if(!is_array($haystack)){
die("second argument is not array");
}
global $matches;
foreach($haystack as $key=>$value)
{
if(preg_match("/$needle/i", $key)){
$matches[] = array($path . "$key/", "KEY: $key");
}
if(is_array($value)){
$path .= "$key/";
arrayRecursiveSearch($needle, $value, $path);
unset($path);
}else{
if(preg_match("/$needle/i", $value)){
$matches[] = array($path . "$key/", "VALUE: $value");
}
}
}
return $matches;
}
$arr = array("Asia"=>array('rambutan','duku'),
"Australia"=>array('pear','kiwi'),
"Arab"=>array('kurma'));
print_r(arrayRecursiveSearch("ra",$arr));

Related

Folders first from RecursiveDirectoryIterator

I wrote this code a long time ago to get files from a folder structure given in $dir.
$recursiveIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST);
$ritit = new RegexIterator($recursiveIterator, $filter);
foreach ($ritit as $splFileInfo) {
if(($splFileInfo->getFileName() != ".") && ($splFileInfo->getFileName() != "..")) {
$path = $splFileInfo->isDir()
? array($splFileInfo->getFilename() => array())
: array($splFileInfo->getFilename());
for ($depth = $ritit->getDepth() - 1; $depth >= 0; $depth--) {
$path = array($ritit->getSubIterator($depth)->current()->getFilename() => $path);
}
$return = array_merge_recursive($return, $path);
}
}
And as the title suggests, I want the $return array to have the folders first. I first attempted to correct this with a foreach after the loop, and sort into $folders and $files array, however this wouldnt change the contents inside the folders, if there were mutliple children inside children.
Is there a way to modify the above loop so that all folders appear first in the array and files after? Including children and children's children?
I don't think that you can modify the loop to get the output array the way you want it. Instead, I'd rather use recursive sorting function to sort the array after the loop.
First, create function that defines the logic for sorting elements. In your case, you want the array-type elements to be the first elements in a tier, so the sorting function could look like this:
function dirFirstSorting($a, $b)
{
if (is_array($a) && is_string($b)) {
return -1;
} elseif (is_string($a) && is_array($b)) {
return 1;
} else {
return 0;
}
}
Then, create a function that recursively sorts elements in array:
function sortFilesListing(&$array)
{
if (is_array($array)) {
uasort($array, "dirFirstSorting");
array_walk($array, "sortFilesListing");
}
}
All you need to do now, is to call sortFilesListing function with $return array provided:
sortFilesListing($return);
The $return array elements should be now sorted accordingly.

Alter PHP Array

I'm need an array of all the folders in a directory, im using laravel to get an array of folders in the directory using the below code.
Storage::allDirectories('/public');
this returns an array like below.
$directories = [
"Gallery",
"Images",
"Images/Proof",
"Images/Proof/Another",
];
I need a way to remove duplicate parent directories, so for example Images will be remove if Images/Proof/Another exists or something like that, i just need to build an array of a directory and its subfolders as one array, if that makes sense.
so for example:
$directories = [
["Gallery"],
["Images/Proof/Another"],
];
Or something like this, I can't think of a way to get this to work.
I'm building a custom media manager component if anyone was wondering, some exist but i have no control over them so i built my own.
all help is appreciated.
Can you post the code you have tried so far
For 1 i can't get my head around the logic, thats why im asking what i've done "Which i dont see how it would help at all"
foreach (Storage::allDirectories('/public') as $folder)
{
$all_folders[] = $exploded('/',$folder);
}
Im trying to separate the array into smaller arrays and check each exploded bit against another. But this has me running in circles.
Do like below (Easiest way):-
$final_array = [];
foreach($directories as $dir){
$final_array[explode('/',$dir)[0]] = $dir;
}
$final_array =array_values($final_array);
Output:- https://eval.in/912417
Or:-
$final_array = [];
foreach($directories as $dir){
$final_array[explode('/',$dir)[0]][0] = $dir;
}
$final_array =array_values($final_array);
Output:-https://eval.in/912418
If you rsort (descending) it and foreach it you can then use preg_grep to see if it exsists in your new array.
If not add it.
$directories = [
"Gallery",
"Images",
"Images/Proof",
"Images/Proof/Another",
];
Rsort($directories);
$new=[];
Foreach($directories as $val){
If(!preg_grep("~" . $val. ".*~",$new)){
$new[] = $val;
}
}
Var_dump($new);
https://3v4l.org/0EnZ3
Preg_grep will look for the pattern and see if it exsists in the array.
Since I loop descending it will first look at Images/Proof/Another then add it to the list because it's not there.
Next iteration will look at Images/Proof/ and since preg_grep has pattern Images/Proof/.* it will be true thus not add it in the list.
Then the same with images.
You can just filter array using array_filter function.
<?php
$directories = [
"Gallery",
"Images",
"Images/Proof",
"Images/Proof/Another",
];
$filtered = array_filter($directories, function($v) use ($directories) {
foreach($directories as $dir) {
if(strpos($dir, $v . "/") === 0) {
return false;
}
}
return true;
});
var_dump($filtered);
Not so elegant, but clear to read.
Quick and dirty, but tested and works
function removeRedundantParents($directories)
{
foreach ($directories as $key => &$val)
{
foreach ($directories as $i => &$v)
{
if ($val !== $v && substr($v, 0, strlen($val)) == $val)
{
unset($directories[$key]);
continue;
}
}
}
return $directories;
}
I achieved this by removing the row if it satisfies the logic.
Mutative, but satisfies the requirement correctly.
Tested, works properly.
check out https://eval.in/912439 for the snippet and output
Code
$directories = array_flip($directories);
foreach ($directories as $dir => $key) {
$parent_dir = preg_match('/(.*)\//', $dir, $match);
if (isset($match[1])) {
unset($directories[$match[1]]);
}
}
$directories = array_keys($directories);

iterate through array, number of keys is variable, the first value being processed differently

Hi I have a PHP array with a variable number of keys (keys are 0,1,2,3,4.. etc)
I want to process the first value differently, and then the rest of the values the same.
What's the best way to do this?
$first = array_shift($array);
// do something with $first
foreach ($array as $key => $value) {
// do something with $key and $value
}
I would do this:
$firstDone = FALSE;
foreach ($array as $value) {
if (!$firstDone) {
// Process first value here
$firstDone = TRUE;
} else {
// Process other values here
}
}
...but whether that is the best way is debatable. I would use foreach over any other method, because then it does not matter what the keys are.
Here is one way:
$first = true;
foreach($array as $key => $value) {
if ($first) {
// something different
$first = false;
}
else {
// regular logic
}
}
$i = 0;
foreach($ur_array as $key => $val) {
if($i == 0) {
//first index
}
else {
//do something else
}
$i++;
}
I would do it like this if you're sure the array contains at least one entry:
processFirst($myArray[0]);
for ($i=1; $i<count($myArray); $1++)
{
processRest($myArray[$i]);
}
Otherwise you'll need to test this before processing the first element
I've made you a function!
function arrayCallback(&$array) {
$callbacks = func_get_args(); // get all arguments
array_shift($callbacks); // remove first element, we only want the callbacks
$callbackindex = 0;
foreach($array as $value) {
// call callback
$callbacks[$callbackindex]($value);
// make sure it keeps using last callback in case the array is bigger than the amount of callbacks
if(count($callbacks) > $callbackindex + 1) {
$callbackindex++;
}
}
}
If you call this function, it accepts an array and infinite callback arguments. When the array is bigger than the amount of supplied functions, it stays at the last function.
You can simply call it like this:
arrayCallback($array, function($value) {
print 'callback one: ' . $value;
}, function($value) {
print 'callback two: ' . $value;
});
EDIT
If you wish to avoid using a function like this, feel free to pick any of the other correct answers. It's just what you prefer really. If you're repeatedly are planning to loop through one or multiple arrays with different callbacks I suggest to use a function to re-use code. (I'm an optimisation freak)

php. walk up multidimensional array?

suppose I have a multidimensional array structure:
array(parent)
array
array
array(parent)
array(parent)
...some key I'm looking for....
array
array
array
array
array
array
array
I iterate over it recursively to find an array that contains some key I'm looking for - this is no problem.
But then I need to walk up the tree and add additional key to all parents (marked by parent). I can't wrap my head around it. I can easily walk down the tree recursively but I can't figure out how to walk up. Can someone point me to the right direction?
This is an example that I've just wrote just to get the idea.
NOTE that this will break execution on the first occurrence of the matched value.
codepad link
$arr = array(
array('a','b','c','d'),
array(61,62,63,64,65),
array('e','f','g','h'),
array(6,7,8,9),
array(1,2,array(1,2,3,4,5,6,7,8),4,5,6,7,8),
array('i','j','k','l','m'),
);
function array_walkup( $desired_value, array $array, array $keys=array() ) {
if (in_array($desired_value, $array)) {
array_push($keys, array_search($desired_value, $array));
return $keys;
}
foreach($array as $key => $value) {
if (is_array($value)) {
$k = $keys;
$k[] = $key;
if ($find = array_walkup( $desired_value, $value, $k )) {
return $find;
}
}
}
return false;
}
$keys = array_walkup(3, $arr);
echo "3 has been found in \$arr[".implode('][', $keys)."]";
You can use something along these lines in a class:
private $valueFound;
public function recurse($item)
{
if ($item['special_field'] === 'value you are looking for')
{
$this->valueFound = $item['other_field'];
return;
}
if (isset($item['child'])
{
recurse($item['child']);
}
// Use tail-recursion.
// You are at this point past all recursion and are heading back up the tree.
$item['field_to_set'] = $this->valueFound;
}

Knowning at which point we are in a foreach

I have an array:
$array=array('key1'=>'value1','key2'=>'value2','value3');
and a foreach:
foreach($array as $v){
//do something
}
Is there a way to know in the foreach which element we are parsing?
(without doing something like:)
$count=0;
foreach($array as $v){
$count++;
// do something
}
thanks
EDIT1:
Sorry maybe I was not clear:
I don't want know the key, but I need to know how many elements are left in the foreach. (that's why I did the example with $count)
You could use a class that extends ArrayIterator:
class FooArrayIterator extends ArrayIterator {
private $offset = 0;
public function next() {
$this->offset++;
return parent::next();
}
public function rewind() {
$this->offset = 0;
parent::rewind();
}
public function seek($offset) {
if ($offset >= 0 && $offset < $this->count()) {
$this->offset = $offset;
}
parent::seek($offset);
}
public function offset() {
return $this->offset;
}
}
Example:
$array = array('value1','value2','value3');
$iter = new FooArrayIterator($array);
foreach ($iter as $v) {
echo ($iter->count() - $iter->offset() - 1).' more';
}
You can get the actual index:
foreach ($array as $index => $value) {
}
If you are working with an associative array there is no way to tell the current position of the internal array pointer. There is only an indirect way: you search for the current key in the keys of the array with:
foreach ($array as $index => $value) {
$position = array_search ($index, array_keys ($array));
echo ($position);
}
... but I guess count++ is a much more resource-friendly way.
You can:
$count = count($array);
foreach($array as $key => $value) {
$count--;
//$count elements are left
}
Yes, sort of.
foreach($array as $key=>$value)
// code
}
$key will be the array key, although if you want an actual integer count of iterations, and the keys are not numbered sequentially, or are strings, you will have to use a counter like in your original post.
Edit: to handle the last element without implementing a counter, you can use (if keys are int)
$foo = ($key == count($array)-1) ? "last element" : "any other element";
(janked from the manual comments - http://php.net/manual/en/control-structures.foreach.php)
Edit: if your keys are not integers, you can create a counter like you have in your code above, and substitute $key with your counter variable.
You're being a bit too picky :)
By the way the trick is to transform an associative array to an indexed array:
foreach ( array_values($array) as $key=>$value ){
echo $key; //yes, it will be an INT
echo ( count($array) - $key );
}
Without some pre-processing, such as your count() version, there isn't any way to know where you are in an associative array. At most you can check if you're at the end with end(), but there's no guarantee as to what order foreach() will retrieve the individual elements. Generally it's the same order they got added to the array, but not guarantees.
Another pre-processing option would be
$keys = array_keys($array);
$cnt = count($keys)
for ($i = 0; $i < $cnt; $i++) {
$element = $array[$keys[$i]];
}
and $i would tell you exactly how far through you've gone.

Categories