排序基于一个数组多维数组" PARENT_ID" [英] Sorting an array into a multidimensional array based on "parent_id"
问题描述
我被每个专辑(除非它是在顶层,在这种情况下,它不具有parent_album_id)。
分配parent_album_id存储在MySQL的无限嵌套的目录结构我第一次抓住从数据库中的所有专辑的数组,并改变每个相册关键它的身份证(自动增量ID)。
接下来,我想专辑的阵列重组到阵列期多维,在'儿童'对每个相册存储子专辑。我已经取得了一些成功。下面code工作正常,如果我只需要往下走阵列中的一个级别,但如果我走下来一个以上的水平就失去阵列的全部结构。这是因为当我递归调用array_search_key我不及格的全阵列,只是我想寻找一个新的水平。
我如何递归遍历数组搜索,但返回专辑的整个多维数组?
的foreach($相册作为&放大器; $专辑){
如果($专辑['parent_album_id']){//将相簿,如果它有一个父
$ insert_album = $专辑;
未设置($专辑[$专辑['身份证']); //从数组删除相册,因为我们将其插入到它的父
$结果= array_search_key($专辑['parent_album_id'],$相册,$ insert_album,$专辑);
如果($结果){
$专辑= $结果;
}
}
}功能array_search_key($ needle_key,$数组$ insert_album){
的foreach($数组$关键=>&安培; $值){
如果($键== $ needle_key){
$阵列[$关键] ['孩子'] [$ insert_album ['身份证'] = $ insert_album;
返回$阵列;
}
如果(is_array($值)及和放大器; is_array($值['孩子'])){
如果(($结果= array_search_key($ needle_key,$值['孩子'],$ insert_album))!= = FALSE)
返回$结果;
}
}
返回false;
}
//你已经设法让数组这种形式
$ =专辑阵列(
1 =>阵列('ID'=大于1,'标题'=>'相册1','parentId的'=>空)
2 =>阵列('ID'=大于2,'标题'=>'相册2','parentId的'=>空)
3 =>阵列('ID'= GT; 3,'标题'=>'相册1.1','parentId的'=> 1)
4 =>阵列('ID'=> 4,'标题'=>'相册1.1.1','parentId的'=> 3)
5 =>阵列('ID'=> 5,'标题'=>'相册2.1','parentId的'=> 2),
6 =>阵列('ID'=> 6,'标题'=>'相册1.1.2','parentId的'=> 3)
7 =>阵列('ID'=大于7,'标题'=>'相册1.1.3','parentId的'=> 3)
);的print_r(富($专辑));
函数foo($专辑){
$ RV =阵列();
的foreach($相册作为&放大器; $专辑){
如果(is_null($专辑['parentId的'])){
//没有parentId的 - >根数组中的条目
$ RV [] =&放大器; $专辑;
}
其他{
$ PID = $专辑['parentId的'];
如果(!使用isset($专辑[$ PID)){
回声专辑orphant:',$专辑['身份证'],\\ n;
}
其他{
如果(!使用isset($专辑[$ PID] ['孩子'])){
$专辑[$ PID] ['孩子'] =阵列();
}
$专辑[$ PID] ['孩子'] [] =&放大器; $专辑;
}
}
}
返回$ RV;
}
打印
阵列
(
[0] =>排列
(
[ID] => 1
[标题] =>相册1
[parentId的] =>
[儿童] =>排列
(
[0] =>排列
(
[ID] => 3
[标题] =>相册1.1
[parentId的] => 1
[儿童] =>排列
(
[0] =>排列
(
[ID] => 4
[标题] =>专辑1.1.1
[parentId的] => 3
) [1] =>排列
(
[ID] => 6
[标题] =>专辑1.1.2
[parentId的] => 3
) [2] =>排列
(
[ID] => 7
[标题] =>专辑1.1.3
[parentId的] => 3
) ) ) ) ) [1] =>排列
(
[ID] => 2
[标题] =>相册2
[parentId的] =>
[儿童] =>排列
(
[0] =>排列
(
[ID] =>五
[标题] =>相册2.1
[parentId的] => 2
) ) ))
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;
}
// 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
)
)
)
)
这篇关于排序基于一个数组多维数组" PARENT_ID"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!