我想将以下多维数组转换为单数组。子数组的 仅 parent_id
列应与父数组的category_id
相同。而且,sort_order_number
子数组应该等于它们在 children 数组中的位置。父数组的sort_order_number
应该等于它们在 parent 数组中的位置:
当前的多维数组 :
array:2 [▼
0 => array:12 [▼
"text_az" => "title1"
"text_en" => "title1.1"
"text_ru" => "title1.2"
"slug" => "sfsf"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 1
"sort_order_number" => 1
"parent_id" => null
"created_at" => null
"updated_at" => null
"children" => array:1 [▼
0 => array:12 [▼
"text_az" => "title3"
"text_en" => "title3.1"
"text_ru" => "title3.2"
"slug" => "sdf"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 3
"sort_order_number" => 1
"parent_id" => null
"created_at" => null
"updated_at" => null
"children" => array:1 [▼
0 => array:11 [▼
"text_az" => "title2"
"text_en" => "title2.1"
"text_ru" => "title2.2"
"slug" => "fsdfsf"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 2
"sort_order_number" => 2
"parent_id" => null
"created_at" => null
"updated_at" => null
]
]
]
]
]
1 => array:11 [▼
"text_az" => "title4"
"text_en" => "title4.1"
"text_ru" => "title4.2"
"slug" => "treteterter"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 4
"sort_order_number" => 2
"parent_id" => null
"created_at" => null
"updated_at" => null
]
]
转换后我想拥有的单维数组:
array:3 [▼
0 => array:12 [▼
"text_az" => "title1"
"text_en" => "title1.1"
"text_ru" => "title1.2"
"slug" => "sfsf"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 1
"sort_order_number" => 1
"parent_id" => null
"created_at" => null
"updated_at" => null
]
1 => array:12 [▼
"text_az" => "title3"
"text_en" => "title3.1"
"text_ru" => "title3.2"
"slug" => "sdf"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 3
"sort_order_number" => 1
"parent_id" => 1
"created_at" => null
"updated_at" => null
]
2 => array:12 [▼
"text_az" => "title2"
"text_en" => "title2.1"
"text_ru" => "title2.2"
"slug" => "fsdfsf"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 2
"sort_order_number" => 2
"parent_id" => 3
"created_at" => null
"updated_at" => null
]
3 => array:12 [▼
"text_az" => "title4"
"text_en" => "title4.1"
"text_ru" => "title4.2"
"slug" => "treteterter"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 4
"sort_order_number" => 2
"parent_id" => null
"created_at" => null
"updated_at" => null
]
]
答案 0 :(得分:2)
由于数组是固定结构,您可以这样做, simple demo
$result = [];
$count = 1;
$temp = [];
array_walk_recursive($array, function($v, $k) use(&$result, &$count, &$temp) {
$temp[$k] = $v;
if($count++ % 11 == 0) {
$temp["sort_order_number"] = intval($count / 11);
$result[] = $temp;
$temp = [];
}
});