Array Key Shuffling
Posted: Fri Apr 03, 2009 9:41 pm
I need a function that will turn this array:
$old_array
[text]Array
(
[images] => Array
(
[name] => Array
(
[0] => sheet-v2.png
[1] => 05logo.png
)
[type] => Array
(
[0] => image/png
[1] => image/png
)
[tmp_name] => Array
(
[0] => C:\xampp\tmp\php474F.tmp
[1] => C:\xampp\tmp\php4750.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 81537
[1] => 1143
)
)
)[/text]
into this array:
$new_array
[text]Array
(
[images] => Array
(
[0] => Array
(
[name] => sheet-v2.png
[type] => image/png
[tmp_name] => C:\xampp\tmp\php474F.tmp
[error] => 0
[size] => 81537
)
[1] => Array
(
[name] => 05logo.png
[type] => image/png
[tmp_name] => C:\xampp\tmp\php4750.tmp
[error] => 0
[size] => 1143
)
)
)[/text]
I don't want a custom function that works only with this array. (I already wrote one.) I want a generalized function that works with an unknown number of sub-arrays. Some limitations are that the innermost arrays all have to have the same keys and be at the same depth.
The function call might look something like this:
The second argument is an array that tells the function how to order the hierarchy.
Has anyone tackled this challenge before? The recursion is turning my brain to mush.
I think I am half-way to a solution, though. I made a function that flattens the array keys:
It turns $old_array into this:
$old_array_keys
[text]Array
(
[0] => Array
(
[0] => images
)
[1] => Array
(
[0] => name
[1] => type
[2] => tmp_name
[3] => error
[4] => size
)
[2] => Array
(
[0] => 0
[1] => 1
)
)[/text]
Sorry for the long post. (I thought I would make it just a little bit longer by putting this sentence in here.)
Edit: This post was recovered from search engine cache.
$old_array
[text]Array
(
[images] => Array
(
[name] => Array
(
[0] => sheet-v2.png
[1] => 05logo.png
)
[type] => Array
(
[0] => image/png
[1] => image/png
)
[tmp_name] => Array
(
[0] => C:\xampp\tmp\php474F.tmp
[1] => C:\xampp\tmp\php4750.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 81537
[1] => 1143
)
)
)[/text]
into this array:
$new_array
[text]Array
(
[images] => Array
(
[0] => Array
(
[name] => sheet-v2.png
[type] => image/png
[tmp_name] => C:\xampp\tmp\php474F.tmp
[error] => 0
[size] => 81537
)
[1] => Array
(
[name] => 05logo.png
[type] => image/png
[tmp_name] => C:\xampp\tmp\php4750.tmp
[error] => 0
[size] => 1143
)
)
)[/text]
I don't want a custom function that works only with this array. (I already wrote one.) I want a generalized function that works with an unknown number of sub-arrays. Some limitations are that the innermost arrays all have to have the same keys and be at the same depth.
The function call might look something like this:
Code: Select all
<?php
$new_array = array_key_shuffle($old_array, array(0, 2, 1));
?>Has anyone tackled this challenge before? The recursion is turning my brain to mush.
I think I am half-way to a solution, though. I made a function that flattens the array keys:
Code: Select all
<?php
/**
* Lists array keys recursively, stops at the first non-array
*
* @param array $a - the input array
* @param integer $i - (for recursive calls only)
* @param array $k - (for recursive calls only)
* @return array
*/
function array_keys_r ($a, $i = 0, $k = array())
{
if (is_array($a))
{
$k[$i] = array_keys($a);
return array_keys_r($a[$k[$i][0]], $i + 1, $k);
}
else
return $k;
}
?>$old_array_keys
[text]Array
(
[0] => Array
(
[0] => images
)
[1] => Array
(
[0] => name
[1] => type
[2] => tmp_name
[3] => error
[4] => size
)
[2] => Array
(
[0] => 0
[1] => 1
)
)[/text]
Sorry for the long post. (I thought I would make it just a little bit longer by putting this sentence in here.)
Edit: This post was recovered from search engine cache.