vote up 0 vote down star

Pointless Dribble


Okay This is another weird one from me, i want to thank OIS for helping me out on my last question... which deals with this same kind of funky array manipulation... i studied that code in depth and i feel it has helped me become better with recursive array manipulative functions. However, once again i find my self in another tough spot

Actual Problem

I am trying to write a recursive manipulative function such as this. Except for getting the depth of each array element. It will add incremental count to each array element with a certain depth. The easiest way to see what I'm trying to do is to view the "sample array" and "Desired result Array"... i feel like I'm getting better at understanding these kind of recursive functions. but this one is giving me hell, thanks in advance for any kind of help you can give me with this. Please disregard the [depth] result of the sample array i already have a function that adds this. Thanks again, -- YouDontMeanMuch

Sample Array


			array (
			  52 => 
			  array (
			    'title' => 'Website Navigation',
			    'path' => '',
			    'type' => '115',
			    'pid' => 0,
			    'hasChildren' => 1,
			    'children' => 
			    array (
			      53 => 
			      array (
			        'title' => 'Home',
			        'path' => '',
			        'type' => '118',
			        'pid' => 52,
			        'hasChildren' => 0,
			      ),
			      54 => 
			      array (
			        'title' => 'Features',
			        'path' => 'features',
			        'type' => '374',
			        'pid' => 52,
			        'hasChildren' => 1,
			        'children' => 
			        array (
			          59 => 
			          array (
			            'title' => 'artistic',
			            'path' => 'features/artistic',
			            'type' => '374',
			            'pid' => 54,
			            'hasChildren' => 1,
			            'children' => 
			            array (
			              63 => 
			              array (
			                'title' => 'galleries',
			                'path' => 'features/artistic/galleries',
			                'type' => '374',
			                'pid' => 59,
			                'hasChildren' => 1,
			                'children' => 
			                array (
			                  65 => 
			                  array (
			                    'title' => 'graphics',
			                    'path' => 'features/artistic/galleries/graphics',
			                    'type' => '118',
			                    'pid' => 63,
			                    'hasChildren' => 0,
			                  ),
			                  67 => 
			                  array (
			                    'title' => 'mixed medium',
			                    'path' => 'features/artistic/galleries/mixed-medium',
			                    'type' => '118',
			                    'pid' => 63,
			                    'hasChildren' => 0,
			                  ),
			                  64 => 
			                  array (
			                    'title' => 'overview',
			                    'path' => 'features/artistic/galleries',
			                    'type' => '118',
			                    'pid' => 63,
			                    'hasChildren' => 0,
			                  ),
			                  68 => 
			                  array (
			                    'title' => 'photography',
			                    'path' => 'features/artistic/galleries/photography',
			                    'type' => '118',
			                    'pid' => 63,
			                    'hasCh
            
flag

2 Answers

vote up 2 vote down check

I think this should work... I wasn't able to test on your example array but it seems to work on a smaller array I made.

Edit: Changed the function now that you've removed the 'depth' keys from your example array. Now it finds the depth on its own. I've also added my test code and output:

<?php

function array_depth_count(&$array, $count=array(), $depth=1) {
    foreach ($array as &$value) {
        if (is_array($value)) {
            $value['count'] = ++$count[$depth];
            array_depth_count($value, $count, $depth + 1);
        }
    }
}

$a = array(array(array(array(0),array(0),array(),array()),0,array()));

echo "Before\n";
print_r($a);
array_depth_count($a);
echo "\n\nAfter\n";
print_r($a);

?>

Output:

Before
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => 0
                        )

                    [1] => Array
                        (
                            [0] => 0
                        )

                    [2] => Array
                        (
                        )

                    [3] => Array
                        (
                        )

                )

            [1] => 0
            [2] => Array
                (
                )

        )

)

After
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => 0
                            [count] => 1
                        )

                    [1] => Array
                        (
                            [0] => 0
                            [count] => 2
                        )

                    [2] => Array
                        (
                            [count] => 3
                        )

                    [3] => Array
                        (
                            [count] => 4
                        )

                    [count] => 1
                )

            [1] => 0
            [2] => Array
                (
                    [count] => 2
                )

            [count] => 1
        )

)
link|flag
Couldn't seem to get this to work at all on the sample array, i var_exported the sample array now (edit on the question). – youdontmeanmuch Feb 7 '09 at 10:34
My function was using the 'depth' keys in the original array but now they're gone. I edited the function to find the depth on its own and it seems to work on your sample array now. – jeremy Ruten Feb 7 '09 at 21:15
Awesome, it makes sense... and it works :D thank you very much! – youdontmeanmuch Feb 8 '09 at 5:56
vote up 0 vote down

I really want to say this will work

function deep(&$layer)
{
    $count = 1;
    $keys = array_keys($layer);
    foreach($keys as $key)
    	if(is_array($layer[$key]))
    		deep($layer[$key]);
    $layer['depth'] = $count++;
}

(Tested and works fine for me) (Another case of me misunderstanding the question. This should be what you want)

link|flag
Yea i think you mis understood the question (sorry i didn't make this clear) I already have a function to set the depth, i need something to set the [count] on the sample array. – youdontmeanmuch Feb 7 '09 at 10:37

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.