Fix array keys for multidimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

Fix array keys for multidimensional array

Post by dimxasnewfrozen »

I have an interesting dilemma. I have an array that looks like:

Code: Select all

Array
(
    [ArrayName1] => Array
        (
            [name] => ArrayName1
            [folder_path] => 
            [description] => 
            [metadata] => 
            [folders] => Array
                (
                    [ArrayName2] => Array
                        (
                            [name] => ArrayName2
                            [folder_path] => 
                            [description] => 
                            [metadata] => 
                            [folders] => Array
                                (
                                    [ArrayName3] => Array
                                        (
                                            [name] => ArrayName3
                                            [folder_path] =>
                                            [description] => 
                                            [metadata] => 
                                            [folders] => 
                                        )

                                    [ArrayName4] => Array
                                        (
                                            [name] => ArrayName4
                                            [folder_path] =>
                                            [description] => 
                                            [metadata] => 
                                            [folders] => 
                                        )

                                )

                        )

                )

        )
What I'm trying to do is get the array keys for each array object (other than 'folders') to be a numeric key and make it look like:

Code: Select all

Array
(
    [0] => Array
        (
            [name] => ArrayName1
            [folder_path] => 
            [description] => 
            [metadata] => 
            [folders] => Array
                (
                    [0] => Array
                        (
                            [name] => ArrayName2
                            [folder_path] => 
                            [description] => 
                            [metadata] => 
                            [folders] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => ArrayName3
                                            [folder_path] =>
                                            [description] => 
                                            [metadata] => 
                                            [folders] => 
                                        )

                                    [1] => Array
                                        (
                                            [name] => ArrayName4
                                            [folder_path] =>
                                            [description] => 
                                            [metadata] => 
                                            [folders] => 
                                        )

                                )

                        )

                )

        )
This is a dynamic array so it could be larger or smaller. I spent some time messing with the array_values function and making it recursive but I keep getting ALL keys replaced not just the new array object.

Here's what I was toying with:

Code: Select all


	function fix_keys($array) {

	  foreach ($array as $k => $val) 
	  {
	    if(is_array($val))
	    {
	    	$array[$k] = $this->fix_keys($val);
	    }
	  }

	 return array_values($array);
	
But that outputs:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => ArrayValue1
            [1] =>
            [2] => 
            [3] => 
            [4] => Array
                (
                    [0] => Array
                        (
                            [0] => ArrayValue3
                            [1] => 
                            [2] => 
                            [3] => 
                            [4] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => ArrayValue3
                                            [1] =>
                                            [2] => 
                                            [3] => 
                                            [4] => 
                                        )
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Fix array keys for multidimensional array

Post by requinix »

So as you've noticed the easiest solution will be recursion to some degree. The only difference I would suggest to the structure of your code is to take into account how every other level of the array is a nested structure. As in the first level is an array of things, the second level is data, the third level is an array, the fourth is data, and so on.

Assuming you're starting with the top-level array (which is an array of things), you foreach over that and reset keys as you go. You then jump directly to the "folders" subarray and recurse over that specifically.

Code: Select all

$newarray = array();
foreach ($array as $value) {
	$value["folders"] = $this->fix_keys($value["folders"]); // recursion on just the folders
	$newarray[] = $value; // reset the key
}
return $newarray;
(You could avoid $newarray by just modifying the $array in place, using unset() to remove the old item.)
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

Re: Fix array keys for multidimensional array

Post by dimxasnewfrozen »

Oh great thanks. I'll give it a shot and let you know!
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

Re: Fix array keys for multidimensional array

Post by dimxasnewfrozen »

Brilliant! It works. Thanks again!
Post Reply