简体   繁体   English

递归获取嵌套 PHP 数组中每个节点的父节点列表

[英]Recursively getting a list of parents for each node in a nested PHP array

In PHP I have a set of nested arrays of data.在 PHP 我有一组嵌套的 arrays 数据。 I want to flatten these, so that for every node and leaf I get a list of its parents.我想把它们展平,这样对于每个节点和叶子,我都会得到一个父节点列表。

Here's an example of the original array - note, each part could be of an unknown depth and length:这是原始数组的一个示例 - 请注意,每个部分的深度和长度都可能未知:

$data = array(
    "name" => "Thing",
    "children" => array(
        array(
            "name" => "Place",
            "rdfs:subClassOf" => "schema:Thing",
            "children" => array(
                array(
                    "name" => "Accomodation",
                    "rdfs:subClassOf" => "schema:Place",
                    "children" => array(
                        array(
                            "name" => "Apartment",
                            "rdfs:subClassOf" => "schema:Accomodation",
                        ),
                        array(
                            "name" => "Hotel",
                            "rdfs:subClassOf" => "schema:Accomodation",
                        ),
                        array(
                            "name" => "House",
                            "rdfs:subClassOf" => "schema:Accomodation",
                        )
                    )
                )
            )
        ),
        array(
            "name" => "CreativeWork",
            "rdfs:subClassOf" => "schema:Thing",
            "children" => array(
                array(
                    "name" => "Article",
                    "rdfs:subClassOf" => "schema:CreativeWork",
                    "children" => array(
                        array(
                            "name" => "NewsArticle",
                            "rdfs:subClassOf" => "schema:Article",
                        ),
                        array(
                            "name" => "OpinionArticle",
                            "rdfs:subClassOf" => "schema:Article",
                        ),
                    )
                )
            )
        )
    )
);

(The data I'm actually parsing is this Schema.org JSON file - the above is a minimal example of it.) (我实际解析的数据是这个 Schema.org JSON 文件——上面是它的一个最小示例。)

And here's what I'd like to end up with:这就是我想要的结果:

$results = array(
    "Place"          => array("Thing"),
    "Accomodation"   => array("Thing", "Place"),
    "Apartment"      => array("Thing", "Place", "Accomodation"),
    "Hotel"          => array("Thing", "Place", "Accomodation"),
    "House"          => array("Thing", "Place", "Accomodation"),
    "CreativeWork"   => array("Thing"),
    "Article"        => array("Thing", "CreativeWork"),
    "NewsArticle"    => array("Thing", "CreativeWork", "Article"),
    "OpinionArticle" => array("Thing", "CreativeWork", "Article"),
);

I'm assuming I need to recursively call a function to build the array but so far I'm not having much luck.我假设我需要递归调用 function 来构建数组,但到目前为止我运气不佳。 In case this makes it harder, this is happening in a static method.如果这使它变得更难,这发生在 static 方法中。

Something quick to get you started:快速入门:

class Parser {
public static function parse($input,$prefix = []) {
    $return = $prefix ? [$input['name']=>$prefix] : [];
    if (isset($input['children'])) {
        $prefix[] = $input['name'];
        foreach ($input['children'] as $child) {
            $return += self::parse($child,$prefix);
        }
    }
    return $return;
}
}
var_dump(Parser::parse($data));

You probably need to add a few checks and comments to make it more readable.您可能需要添加一些检查和注释以使其更具可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM