1

I am doing a request to an API and for each request I get an array with the BrowseNodes back. Inside this array I get this result:

array:1 [
  "BrowseNode" => array:3 [
    "BrowseNodeId" => "2502033031"
    "Name" => "OBD-II Diagnosewerkzeuge"
    "Ancestors" => array:1 [
      "BrowseNode" => array:3 [
        "BrowseNodeId" => "5142250031"
        "Name" => "Motorwerkzeuge & Zubehör"
        "Ancestors" => array:1 [
          "BrowseNode" => array:3 [
            "BrowseNodeId" => "2502064031"
            "Name" => "Werkzeuge"
            "Ancestors" => array:1 [
              "BrowseNode" => array:4 [
                "BrowseNodeId" => "79899031"
                "Name" => "Kategorien"
                "IsCategoryRoot" => "1"
                "Ancestors" => array:1 [
                  "BrowseNode" => array:2 [
                    "BrowseNodeId" => "78191031"
                    "Name" => "Auto & Motorrad"
                  ]
                ]
              ]
            ]
          ]
        ]
      ]
    ]
  ]
]

If this would be a fixed array which always had this length it would be easy for me to get the last BrowseNode Array and the value with the key "Name". In this case it would be the this one "Name" => "Auto & Motorrad".

However, because a category can have one or more nested arrays, it is not static...

That means I have to find a solution to always get the most nested array and from this one the name to get the category. Because it is dynamic data, I don't know how to solve that. I just know I always have to get the BrowseNode and then inside the BrowseNode array. I have to get the Ancestors and inside this Ancestors again. I have to get the BrowseNode array until I arrived at the last BrowseNode array and the get the name.

So, I have to iterate through my array until the BrowseNode array doesn't have an Ancestors array anymore and the get the name of this BrowseNode array.

Do you guys have any idea on how to do this?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Jan
  • 1,180
  • 3
  • 23
  • 60
  • Your question would be clearer (better understood about task requirements) if you included sample data that has a fork in it. Then you could explain better about the "depth" factor. – mickmackusa Nov 06 '18 at 12:33
  • @Jan Did my post answer you or I miss-understood the question? – dWinder Nov 08 '18 at 17:10
  • Yeah, it did. Sorry, forgot to upvote it! :) @DavidWinder – Jan Nov 08 '18 at 19:26

2 Answers2

1

I guess you can use recursion to achieve that.

On each object, search for Ancestors, if not found return the name, else do so again on the Ancestors.

Consider the following code:

$arr = array("BrowseNode" => array("BrowseNodeId" => "1", "Ancestors" => array("BrowseNode" => array("BrowseNodeId" => "2", "Ancestors" => array("BrowseNode" => array("BrowseNodeId" => "3"))))));

function getLastName($elem) {
    $elem = $elem["BrowseNode"];
    if (!array_key_exists("Ancestors", $elem))
         return $elem['BrowseNodeId'];
    else return getLastName($elem["Ancestors"]);
}

echo getLastName($arr);

Notice that this code return id instead of your Name - small modification I leave to you...

dWinder
  • 11,597
  • 3
  • 24
  • 39
0

There might be a more "correct" way of doing this but since I'm writing this on my phone on a plane, you'll have to forgive my not being able to proof check my answer. But you could do a recursive function, like...

function recurseArray($browseNode) 
{
    if(isset($browseNode["Ancestors"])) {
        recurseArray($browseNode["Ancestors"]["browseNode"])
    } else {
        // Do whatever you want with the last iteration
    }
}

Hope that makes sense!