如何创建多维数组无限深度数组,父子来自单个 table
How to create multidimensional array unlimited depth array with parent and child came from single table
我正在尝试创建无限深度的多维数组。
我 select 来自数据库的数据并检查它是否字段 'isArray' 为真,这意味着该列是父列然后我尝试创建一个循环以使其查找其子项 'parent_id' => $row->id
.
我期待这样的输出。
array(
[0]=> array(
'id' => '29',
'section' => '',
'sorting' =>'',
'title' => 'POWERS OF THE BANKO SENTRAL',
'pdf_file' => '',
'content' => '',
'parent_id' => '0',
'isArray' => array(
[0] => array(
'id' => '30',
'section' => '001',
'sorting' => '',
'title' => 'Examination by the Bangko Sentral',
'pdf_file' => 'NRBSL CHRISTMAS PARTY RAFFLE WINNERS.pdf',
'parent_id' => '29',
'isArray' => 0,
),
[1] => array(
'id' => '31',
'section' => '002',
'sorting' => '',
'title' => 'Supervisory Enforcement',
'pdf_file' => 'APL-Form1.pdf'
'parent_id' => '29',
'isArray' => 0
)
),
),
[1]=> array(
[0] => array(
'id' => '32',
'section' => '',
'sorting' => '',
'title' => 'A. Risk Management',
'pdf_file' => '',
'content' => '',
'parent_id' => '0',
'isArray' => array(
[0] => array(
'id' => '33',
'section' => '911',
'sorting' => '',
'title' => 'RISK MANAGEMENT',
'pdf_file' => '',
'content' => '',
'parent_id' => '32',
'isArray' => array(
[0] => array(
'id' => '34',
'section' => 'ASDF',
'sorting' => '',
'title' => 'ASDFSDF',
'pdf_file' => '',
'content' => '',
'parent_id' => '33',
'isArray' = array()
)
)
)
)
)
)
)
而我从数据库中得到的数据是这样的
我想出了这个代码:
public function findParentsParent($result) {
global $subs;
foreach ($result as $row) {
$isArray = filter_var($row->isArray, FILTER_VALIDATE_BOOLEAN);
if ($isArray) {
$subs[][$row->parent_id] = $row;
$where = ['parent_id' => $row->id];
$result = $this->my_model->select_where('tbl_policies', $where, 'sorting, section');
if(!empty($result))
$this->findParentsParent($result);
//return array_reverse($subs);
} else {
$subs[] = $row;
}
}
return array_reverse($subs);
}
但我最终得到了这个数组:
array(6) {
[0]=>
object(stdClass)#44 (11) {
["id"]=>
string(2) "30"
["section"]=>
string(3) "001"
["sorting"]=>
string(0) ""
["title"]=>
string(33) "Examination by the Bangko Sentral"
["pdf_file"]=>
string(41) "NRBSL CHRISTMAS PARTY RAFFLE WINNERS1.pdf"
["content"]=>
string(0) ""
["parent_id"]=>
string(2) "29"
["isArray"]=>
string(1) "0"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-03 11:46:06"
["updated_at"]=>
string(19) "2022-03-03 11:46:06"
}
[1]=>
object(stdClass)#43 (11) {
["id"]=>
string(2) "31"
["section"]=>
string(3) "002"
["sorting"]=>
NULL
["title"]=>
string(30) "Supervisory Enforcement Policy"
["pdf_file"]=>
string(13) "APL-Form1.pdf"
["content"]=>
string(0) ""
["parent_id"]=>
string(2) "29"
["isArray"]=>
string(1) "0"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-03 13:19:27"
["updated_at"]=>
string(19) "2022-03-03 13:19:27"
}
[2]=>
array(1) {
[0]=>
object(stdClass)#40 (11) {
["id"]=>
string(2) "29"
["section"]=>
string(0) ""
["sorting"]=>
string(0) ""
["title"]=>
string(28) "POWERS OF THE BANGKO SENTRAL"
["pdf_file"]=>
string(0) ""
["content"]=>
string(0) ""
["parent_id"]=>
string(1) "0"
["isArray"]=>
string(1) "1"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-03 11:45:25"
["updated_at"]=>
string(19) "2022-03-03 11:45:25"
}
}
[3]=>
array(1) {
[33]=>
object(stdClass)#42 (11) {
["id"]=>
string(2) "34"
["section"]=>
string(4) "ASDF"
["sorting"]=>
NULL
["title"]=>
string(7) "ASDFSDF"
["pdf_file"]=>
string(0) ""
["content"]=>
string(0) ""
["parent_id"]=>
string(2) "33"
["isArray"]=>
string(1) "1"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-04 09:29:37"
["updated_at"]=>
string(19) "2022-03-04 09:29:37"
}
}
[4]=>
array(1) {
[32]=>
object(stdClass)#41 (11) {
["id"]=>
string(2) "33"
["section"]=>
string(3) "911"
["sorting"]=>
NULL
["title"]=>
string(15) "RISK MANAGEMENT"
["pdf_file"]=>
string(0) ""
["content"]=>
string(0) ""
["parent_id"]=>
string(2) "32"
["isArray"]=>
string(1) "1"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-04 09:29:18"
["updated_at"]=>
string(19) "2022-03-04 09:29:18"
}
}
[5]=>
array(1) {
[0]=>
object(stdClass)#39 (11) {
["id"]=>
string(2) "32"
["section"]=>
NULL
["sorting"]=>
NULL
["title"]=>
string(18) "A. Risk Management"
["pdf_file"]=>
string(0) ""
["content"]=>
string(0) ""
["parent_id"]=>
string(1) "0"
["isArray"]=>
string(1) "1"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-04 09:28:41"
["updated_at"]=>
string(19) "2022-03-04 09:28:41"
}
}
}
感谢Recursive function to generate multidimensional array from database result
我用这段代码解决了我的问题
public function buildTree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = $this->buildTree($elements, $element['id']);
$isArray = filter_var($element['isArray'], FILTER_VALIDATE_BOOLEAN);
if ($isArray) {
$element['isArray'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}
我正在尝试创建无限深度的多维数组。
我 select 来自数据库的数据并检查它是否字段 'isArray' 为真,这意味着该列是父列然后我尝试创建一个循环以使其查找其子项 'parent_id' => $row->id
.
我期待这样的输出。
array(
[0]=> array(
'id' => '29',
'section' => '',
'sorting' =>'',
'title' => 'POWERS OF THE BANKO SENTRAL',
'pdf_file' => '',
'content' => '',
'parent_id' => '0',
'isArray' => array(
[0] => array(
'id' => '30',
'section' => '001',
'sorting' => '',
'title' => 'Examination by the Bangko Sentral',
'pdf_file' => 'NRBSL CHRISTMAS PARTY RAFFLE WINNERS.pdf',
'parent_id' => '29',
'isArray' => 0,
),
[1] => array(
'id' => '31',
'section' => '002',
'sorting' => '',
'title' => 'Supervisory Enforcement',
'pdf_file' => 'APL-Form1.pdf'
'parent_id' => '29',
'isArray' => 0
)
),
),
[1]=> array(
[0] => array(
'id' => '32',
'section' => '',
'sorting' => '',
'title' => 'A. Risk Management',
'pdf_file' => '',
'content' => '',
'parent_id' => '0',
'isArray' => array(
[0] => array(
'id' => '33',
'section' => '911',
'sorting' => '',
'title' => 'RISK MANAGEMENT',
'pdf_file' => '',
'content' => '',
'parent_id' => '32',
'isArray' => array(
[0] => array(
'id' => '34',
'section' => 'ASDF',
'sorting' => '',
'title' => 'ASDFSDF',
'pdf_file' => '',
'content' => '',
'parent_id' => '33',
'isArray' = array()
)
)
)
)
)
)
)
而我从数据库中得到的数据是这样的
我想出了这个代码:
public function findParentsParent($result) {
global $subs;
foreach ($result as $row) {
$isArray = filter_var($row->isArray, FILTER_VALIDATE_BOOLEAN);
if ($isArray) {
$subs[][$row->parent_id] = $row;
$where = ['parent_id' => $row->id];
$result = $this->my_model->select_where('tbl_policies', $where, 'sorting, section');
if(!empty($result))
$this->findParentsParent($result);
//return array_reverse($subs);
} else {
$subs[] = $row;
}
}
return array_reverse($subs);
}
但我最终得到了这个数组:
array(6) {
[0]=>
object(stdClass)#44 (11) {
["id"]=>
string(2) "30"
["section"]=>
string(3) "001"
["sorting"]=>
string(0) ""
["title"]=>
string(33) "Examination by the Bangko Sentral"
["pdf_file"]=>
string(41) "NRBSL CHRISTMAS PARTY RAFFLE WINNERS1.pdf"
["content"]=>
string(0) ""
["parent_id"]=>
string(2) "29"
["isArray"]=>
string(1) "0"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-03 11:46:06"
["updated_at"]=>
string(19) "2022-03-03 11:46:06"
}
[1]=>
object(stdClass)#43 (11) {
["id"]=>
string(2) "31"
["section"]=>
string(3) "002"
["sorting"]=>
NULL
["title"]=>
string(30) "Supervisory Enforcement Policy"
["pdf_file"]=>
string(13) "APL-Form1.pdf"
["content"]=>
string(0) ""
["parent_id"]=>
string(2) "29"
["isArray"]=>
string(1) "0"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-03 13:19:27"
["updated_at"]=>
string(19) "2022-03-03 13:19:27"
}
[2]=>
array(1) {
[0]=>
object(stdClass)#40 (11) {
["id"]=>
string(2) "29"
["section"]=>
string(0) ""
["sorting"]=>
string(0) ""
["title"]=>
string(28) "POWERS OF THE BANGKO SENTRAL"
["pdf_file"]=>
string(0) ""
["content"]=>
string(0) ""
["parent_id"]=>
string(1) "0"
["isArray"]=>
string(1) "1"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-03 11:45:25"
["updated_at"]=>
string(19) "2022-03-03 11:45:25"
}
}
[3]=>
array(1) {
[33]=>
object(stdClass)#42 (11) {
["id"]=>
string(2) "34"
["section"]=>
string(4) "ASDF"
["sorting"]=>
NULL
["title"]=>
string(7) "ASDFSDF"
["pdf_file"]=>
string(0) ""
["content"]=>
string(0) ""
["parent_id"]=>
string(2) "33"
["isArray"]=>
string(1) "1"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-04 09:29:37"
["updated_at"]=>
string(19) "2022-03-04 09:29:37"
}
}
[4]=>
array(1) {
[32]=>
object(stdClass)#41 (11) {
["id"]=>
string(2) "33"
["section"]=>
string(3) "911"
["sorting"]=>
NULL
["title"]=>
string(15) "RISK MANAGEMENT"
["pdf_file"]=>
string(0) ""
["content"]=>
string(0) ""
["parent_id"]=>
string(2) "32"
["isArray"]=>
string(1) "1"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-04 09:29:18"
["updated_at"]=>
string(19) "2022-03-04 09:29:18"
}
}
[5]=>
array(1) {
[0]=>
object(stdClass)#39 (11) {
["id"]=>
string(2) "32"
["section"]=>
NULL
["sorting"]=>
NULL
["title"]=>
string(18) "A. Risk Management"
["pdf_file"]=>
string(0) ""
["content"]=>
string(0) ""
["parent_id"]=>
string(1) "0"
["isArray"]=>
string(1) "1"
["uploaded_by"]=>
string(1) "6"
["created_at"]=>
string(19) "2022-03-04 09:28:41"
["updated_at"]=>
string(19) "2022-03-04 09:28:41"
}
}
}
感谢Recursive function to generate multidimensional array from database result
我用这段代码解决了我的问题
public function buildTree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = $this->buildTree($elements, $element['id']);
$isArray = filter_var($element['isArray'], FILTER_VALIDATE_BOOLEAN);
if ($isArray) {
$element['isArray'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}