我有如下的大数组
Array
(
[Prosess 1_35] => Array
(
[AF Decom_582] => Array
(
[documents] => Array
(
)
)
[documents] => Array
(
[0] => stdClass Object
(
[title] =>
[doc_id] => 726
)
[1] => stdClass Object
(
[title] => Brunvollquizen
[doc_id] => 878
)
[2] => stdClass Object
(
[title] => Nytt dokument
[doc_id] => 1200
)
[3] => stdClass Object
(
[title] =>
[doc_id] => 1215
)
[4] => stdClass Object
(
[title] => Procedure AXC-12
[doc_id] => 1216
)
)
)
[Prosess 2_2] => Array
(
[documents] => Array
(
[0] => stdClass Object
(
[title] =>
[doc_id] => 1178
)
)
)
[Prosess 3_3] => Array
(
[Delprosess 3-1_10] => Array
(
[test1_340] => Array
(
[documents] => Array
(
)
)
[documents] => Array
(
)
)
[documents] => Array
(
)
)
)
我需要创建一个简单的数组,如
流程1_35有4个文档,一个子文件夹和子文件夹没有文档 过程2_2有1个文档过程3_3没有文档但是有一个子文件夹' 并且需要在每个人文件夹
之后添加一个空数组 array(
array('Prosess 1_35',NULl,NUL...)
array('NULL','doc_name,'doc_id')
array('NULL','doc_name,'doc_id')
array('NULL','doc_name,'doc_id')
array('NULL','doc_name,'doc_id')
array(' AF Decom_582',NULL,NULL)
array('NULL','NULL',''NULL)
array('Prosess 2_2',NULl,NUL...)
array('NULL','doc_name,'doc_id')
array('Prosess 3_3',NULl,NUL...)
array(' Delprosess 3-1_10',NULl,NUL...)
array(' test1_340',NULl,NUL...)
)
我试过下面的代码,但不幸的是,我无法将其作为简单的数组
private static function _createArray1($requirementdocs, $level = 0)
{
$indent = str_repeat(" ", $level * 2);*/
foreach($requirementdocs as $key => $sreq)
{
$rowarr = array();
$key = explode("_",$subkey);
$rowarr[0] = $indent.$key[1];
if ($key[0] == "documents" && !empty($sreq)) continue;
foreach($sreq as $subkey => $req)
{
$rowarr[1] = $req->doc_id;
$rowarr[2] = $req->title;
array_push($res , $rowarr);
}
self::_createArray($sreq, $level+1);
}
}
有人可以帮助我吗?
答案 0 :(得分:1)
这是你想要完成的吗?
$arr = [
[
[
1
],
[
1
],
[
1
],
],
[
1
],
[
[
1
],
[
[
[
1
]
]
],
]
];
echo '<pre>'; print_r($arr); echo '</pre>';
$new = flatten($arr);
echo '<pre>'; print_r($new); echo '</pre>';
和递归方法:
function flatten($array, $depth = INF)
{
$result = [];
foreach ($array as $item) {
$item = $item instanceof Collection ? $item->all() : $item;
if (is_array($item)) {
if ($depth === 1) {
$result = array_merge($result, $item);
continue;
}
$result = array_merge($result, flatten($item, $depth - 1));
continue;
}
$result[] = $item;
}
return $result;
}
顺便说一下,这是Laravel助手array_flatten方法代码。