83214

Flatten Array: Keep index, value equal to position in array

Question:

I've been having a little trouble trying to flatten arrays in a specific way.

Here is a print_r view of the array I want to flatten:

Array ( [1] => Array ( [8] => 1 [9] => 2 [10] => Array ( [15] => Array ( [22] => 1 ) [21] => 2 ) [11] => Array ( [16] => Array ( [23] => 1 ) ) ) [2] => Array ( [12] => 1 ) [3] => Array ( [13] => 1 ) [4] => Array ( [14] => 1 ) [5] => 5 [6] => 6 [7] => 7 )

What I'm attempting to create is an array which keeps the above indexes, but the value is equal to it's position in the array, much like the original index (starting from zero).

Here is the desired result:

Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 1 [9] => 2 [10] => 3 [11] => 4 [12] => 1 [13] => 1 [14] => 1 [15] => 1 [16] => 1 [21] => 2 [22] => 2 [23] => 1 )

Knowingly, 17 to 20 are missing.

My function is as follows:

function array_flatten ($array) { $result = array (); $count = 1; while ($index = current($array)) { $result[key($array)] = $count; if (is_array($index)) { $result = array_merge(array_flatten($index), $result); } next($array); $count++; } return $result; }

The line $result = array_merge(array_flatten($index), $result); appears to be the problems. It returns:

Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 )

However, if I run var_dump(array_flatten($index)); on the same line, it returns all the arrays I wish to merge to the $result variable.

array 22 => int 1 array 15 => int 1 21 => int 2 array 23 => int 1 array 16 => int 1 array 8 => int 1 9 => int 2 10 => int 3 11 => int 4 array 12 => int 1 array 13 => int 1 array 14 => int 1

It seems that that array_merge doesn't actually merge these arrays.

Is there something I am doing wrong? Any words of guidance are very much appreciated. Thank you.

<em>Update</em>

Solved!

The function which does the required is as follows:

function array_flatten($array, &$result = array()) { $count = 1; foreach($array as $index => $value) { $result[$index] = $count; if(is_array($value)) { array_flatten($value, $result); } $count++; } return $result; }

Answer1:

function flatten_array($array, &$result) { foreach($array as $key => $value) { if(is_array($value)) { flatten_array($value, $result); } else { $result[$key] = $value; } } }

To use this, check the sample code below:

$flattened = array(); $test = array( 1 => 1 , 3 => 2 , array(2 => 4, 4 => 6) , 5 => 3 , array(7 => 9, 8 => 7, 9 => 5) ); flatten_array($test, $flattened); // Now $flattened contains the flattened array

Answer2:

After you have clarified your question, I was a bit surprised that you accepted an answer that does not return the data you expected. (I've now seen you added <em>your</em> solution to your question.)

What I did: I took the function from @Arjan as a base, and run it on the questions input data and compared it with the questions expected data. Then I worked on it a bit. This is what I came up with (as a result):

# COMP EXPECTED . ACTUAL #00: == Array . Array #01: == ( . ( #02: == [1] => 1 . [1] => 1 #03: == [2] => 2 . [2] => 2 #04: == [3] => 3 . [3] => 3 #05: == [4] => 4 . [4] => 4 #06: == [5] => 5 . [5] => 5 #07: == [6] => 6 . [6] => 6 #08: == [7] => 7 . [7] => 7 #09: == [8] => 1 . [8] => 1 #10: == [9] => 2 . [9] => 2 #11: == [10] => 3 . [10] => 3 #12: == [11] => 4 . [11] => 4 #13: == [12] => 1 . [12] => 1 #14: == [13] => 1 . [13] => 1 #15: == [14] => 1 . [14] => 1 #16: == [15] => 1 . [15] => 1 #17: == [16] => 1 . [16] => 1 #18: == [21] => 2 . [21] => 2 #19: != [22] => 2 . [22] => 1 #20: == [23] => 1 . [23] => 1 #21: == ) . ) #22: == .

It looks like that your expected data has a mistake for position 22.

This is the modified function (<a href="http://codepad.org/DinolOik" rel="nofollow"><strong>Demo</strong></a>):

function flatten_array($array, &$result = null) { $r = null === $result; $i = 0; foreach($array as $key => $value) { $i++; if(is_array($value)) { $result[$key] = $i; flatten_array($value, $result); } else { $result[$key] = $value; } } if ($r) { ksort($result); return $result; } } $actual = flatten_array($input);

Recommend

  • Performance slowdown when using Julia's built-in functions
  • Parse text file and store fields in hash table in Perl
  • Google script to remove duplicate rows in spreadsheet and keep the most recent entry based on timest
  • lazy load plugin to load images as user scrolls?
  • how to request user permissions using facebook api in js?
  • Python — confused by numpy's piecewise function
  • Return an average of last or first two rows from a different group (indicated by a variable)
  • PHP Need to recursively reverse an array
  • Search_array in nested arrays
  • array_replace_recursive does not replace an array with some empty array
  • Unset inside array_walk_recursive not working
  • Strip down any parameter from URL which is not whitelisted
  • merging two arrays and keep duplicate values
  • Remove items from multidimensional array in PHP
  • turn two nested associative arrays into one flat array?
  • Using Sax parsing to edit and write XML in VB6
  • How does this usort cmp function actually work?
  • Django: Count of Group Elements
  • Why value captured by reference in lambda is broken? [duplicate]
  • Sending data from AppleScript to FileMaker records
  • MySQL WHERE-condition in procedure ignored
  • Build own AppleScript numerical error handling
  • PHP: When would you need the self:: keyword?
  • Acquiring multiple attributes from .xml file in c#
  • Why joiner is not used after Sequence generator or Update statergy
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • File not found error Google Drive API
  • Is it possible to post an object from jquery to bottle.py?
  • Django query for large number of relationships
  • Recursive/Hierarchical Query Using Postgres
  • reshape alternating columns in less time and using less memory
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?