问题
I'm storing an infinitely nested directory structure in mysql by assigning a "parent_album_id" to each "album" (unless it's at the top level, in which case it does not have a parent_album_id).
I first grab an array of all the albums from the database and change each albums key to it's "id" (autoincrement id).
Next, I want to reorganize the array of albums into a multidemensional array, storing child albums in 'children' for each album. I've had some success. The following code works fine if I only need to go down one level in the array, but if I go down more than one level it loses the full structure of the array. This is because when I recursively call array_search_key I don't pass the full array, just the next level that I want to search.
How can I recursively search through the array, but return the entire multidimensional array of albums?
foreach ($albums as &$album){
if($album['parent_album_id']){ // Move album if it has a parent
$insert_album = $album;
unset($albums[$album['id']]); // Remove album from the array, since we are going to insert it into its parent
$results = array_search_key($album['parent_album_id'],$albums,$insert_album, $albums);
if($results){
$albums = $results;
}
}
}
function array_search_key( $needle_key, $array , $insert_album) {
foreach($array AS $key=>&$value){
if($key == $needle_key) {
$array[$key]['children'][$insert_album['id']] = $insert_album;
return $array;
}
if(is_array($value) && is_array($value['children'])){
if( ($result = array_search_key($needle_key, $value['children'], $insert_album)) !== false)
return $result;
}
}
return false;
}
回答1:
// you already managed to get the array into this form
$albums = array(
1 => array('id'=>1, 'title'=>'Album 1', 'parentId'=>null),
2 => array('id'=>2, 'title'=>'Album 2', 'parentId'=>null),
3 => array('id'=>3, 'title'=>'Album 1.1', 'parentId'=>1),
4 => array('id'=>4, 'title'=>'Album 1.1.1', 'parentId'=>3),
5 => array('id'=>5, 'title'=>'Album 2.1', 'parentId'=>2),
6 => array('id'=>6, 'title'=>'Album 1.1.2', 'parentId'=>3),
7 => array('id'=>7, 'title'=>'Album 1.1.3', 'parentId'=>3)
);
print_r(foo($albums));
function foo($albums) {
$rv = array();
foreach( $albums as &$album) {
if ( is_null($album['parentId']) ) {
// no parentId -> entry in the root array
$rv[] = &$album;
}
else {
$pid = $album['parentId'];
if ( !isset($albums[$pid]) ) {
echo 'orphant album: ', $album['id'], "\n";
}
else {
if ( !isset($albums[$pid]['children']) ) {
$albums[$pid]['children'] = array();
}
$albums[$pid]['children'][] = &$album;
}
}
}
return $rv;
}
prints
Array
(
[0] => Array
(
[id] => 1
[title] => Album 1
[parentId] =>
[children] => Array
(
[0] => Array
(
[id] => 3
[title] => Album 1.1
[parentId] => 1
[children] => Array
(
[0] => Array
(
[id] => 4
[title] => Album 1.1.1
[parentId] => 3
)
[1] => Array
(
[id] => 6
[title] => Album 1.1.2
[parentId] => 3
)
[2] => Array
(
[id] => 7
[title] => Album 1.1.3
[parentId] => 3
)
)
)
)
)
[1] => Array
(
[id] => 2
[title] => Album 2
[parentId] =>
[children] => Array
(
[0] => Array
(
[id] => 5
[title] => Album 2.1
[parentId] => 2
)
)
)
)
来源:https://stackoverflow.com/questions/2580248/sorting-an-array-into-a-multidimensional-array-based-on-parent-id