我正在尝试制作这样的 CSV 文件:
id,value
1,type
2,type.type A
3,type.type A.subtype 2000
4,type.type A.subtype 2000.subsubtype
5,type.type A.subtype 2000.subsubtype.subsubsubtype
6,type.type B
这基本上是我们有父母和孩子的递归。到目前为止,我将它们存储在数据库中,我可以将它们提取为二维数组。当我尝试为 CSV 文件中的行创建字符串时,问题就来了。到目前为止,我有这个:
private function _criteriaRec(&$result, $parentId, &$str) {
$relations = parent::getAll('objects_relations', 'id, object_id_2', ['object_id', $parentId, 'attr', 'criteria']); // here I'm extracting the objects from DB
if(!empty($relations)){
foreach ($relations as $relation) {
$object = parent::get('objects', 'id, title', '', $relation['object_id_2']);
$str .= ".$object[title]"; // I'm trying to make the string
$result[] = array(
'parent' => $parentId,
'child' => $relation['object_id_2'],
'title' => $object['title'],
'str' => $str
);
$this->_criteriaRec($result, $relation['object_id_2'], $str);
$str = ''; // I'm clearing the string at some point and I've moved this almost everywhere
}
}
return $result;
}
结果是如下所示的数组:
[0] => Array
(
[parent] => 17
[child] => 33
[title] => type
[str] => .type
)
[1] => Array
(
[parent] => 33
[child] => 34
[title] => subtype B
[str] => .type.subtype B
)
[2] => Array
(
[parent] => 33
[child] => 35
[title] => subtype A
[str] => .subtype A
)
[3] => Array
(
[parent] => 35
[child] => 36
[title] => subsubtype
[str] => .subtype A.subsubtype
)
.......
我对 csv 文件的制作没有问题,但对字符串的嵌套连接没有问题。该字符串将构建一个 D3 树图,这是首选格式,因为我无法控制它。我错过了一些东西,但在这一点上,我不确定是什么。
慕妹3242003