Multidimensional Arrays Nested to Unlimited Depth

Multidimensional Arrays Nested to Unlimited Depth

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.

How to find the depth of an unlimited depthed array

Try this man

                function array_depth($array, $n = 0) {
$max_depth = 1;
foreach ($array as $value) {
if (isset($value['subcategories'][0])) {
$depth = $this -> array_depth($value['subcategories']) + 1;
if ($depth > $max_depth) {
$max_depth = $depth;
}
}
}
return $max_depth;
}

Delete duplicates of values of two nested multidimensional arrays

You are right, you need array_udiff.

Try this:

$res = array_udiff($array1, $array2, function ($a, $b) {
if ($a['id'] < $b['id']) {
return -1;
} elseif ($a['id'] > $b['id']) {
return 1;
} else {
return 0;
}
});

Is there a way to loop through a multidimensional array without knowing it's depth?

Yes, you can use recursion. Here's an example where you output all the elements in an array:

function printAll($a) {
if (!is_array($a)) {
echo $a, ' ';
return;
}

foreach($a as $v) {
printAll($v);
}
}

$array = array('hello',
array('world',
'!',
array('whats'),
'up'),
array('?'));
printAll($array);

What you should always remember when doing recursion is that you need a base case where you won't go any deeper.

I like to check for the base case before continuing the function. That's a common idiom, but is not strictly necessary. You can just as well check in the foreach loop if you should output or do a recursive call, but I often find the code to be harder to maintain that way.

The "distance" between your current input and the base case is called a variant and is an integer. The variant should be strictly decreasing in every recursive call. The variant in the previous example is the depth of $a. If you don't think about the variant you risk ending up with infinite recursions and eventually the script will die due to a stack overflow. It's not uncommon to document exactly what the variant is in a comment before recursive functions.

How to flatten nested array in javascript?

This is an alternative to recursion (see jsfiddle here) and should accept any level of depth which avoids stack overflow.

var array = [[0, 1], [2, 3], [4, 5, [6, 7, [8, [9, 10]]]]];console.log(flatten(array), array); // does not mutate arrayconsole.log(flatten(array, true), array); // array is now empty
// This is done in a linear time O(n) without recursion// memory complexity is O(1) or O(n) if mutable param is set to falsefunction flatten(array, mutable) { var toString = Object.prototype.toString; var arrayTypeStr = '[object Array]'; var result = []; var nodes = (mutable && array) || array.slice(); var node;
if (!array.length) { return result; }
node = nodes.pop(); do { if (toString.call(node) === arrayTypeStr) { nodes.push.apply(nodes, node); } else { result.push(node); } } while (nodes.length && (node = nodes.pop()) !== undefined);
result.reverse(); // we reverse result to restore the original order return result;}

Is there a way to find out how deep a PHP array is?

This should do it:

<?php

function array_depth(array $array) {
$max_depth = 1;

foreach ($array as $value) {
if (is_array($value)) {
$depth = array_depth($value) + 1;

if ($depth > $max_depth) {
$max_depth = $depth;
}
}
}

return $max_depth;
}

?>

Edit: Tested it very quickly and it appears to work.



Related Topics



Leave a reply



Submit