I am accessing product details through Amazon API and get this in my array of data:
["BrowseNodes"]=>
array(1) {
["BrowseNode"]=>
array(3) {
[0]=>
array(3) {
["BrowseNodeId"]=>
string(10) "7421468011"
["Name"]=>
string(24) "Educational & Nonfiction"
["Ancestors"]=>
array(1) {
["BrowseNode"]=>
array(3) {
["BrowseNodeId"]=>
string(4) "4390"
["Name"]=>
string(14) "Graphic Novels"
["Ancestors"]=>
array(1) {
["BrowseNode"]=>
array(3) {
["BrowseNodeId"]=>
string(4) "4366"
["Name"]=>
string(23) "Comics & Graphic Novels"
["Ancestors"]=>
array(1) {
["BrowseNode"]=>
array(4) {
["BrowseNodeId"]=>
string(4) "1000"
["Name"]=>
string(8) "Subjects"
["IsCategoryRoot"]=>
bool(true)
["Ancestors"]=>
array(1) {
["BrowseNode"]=>
array(2) {
["BrowseNodeId"]=>
string(6) "283155"
["Name"]=>
string(5) "Books"
}
}
}
}
}
}
}
}
}
[1]=>
array(3) {
["BrowseNodeId"]=>
string(5) "13871"
["Name"]=>
string(20) "History & Philosophy"
["Ancestors"]=>
array(1) {
["BrowseNode"]=>
array(3) {
["BrowseNodeId"]=>
string(2) "75"
["Name"]=>
string(14) "Science & Math"
["Ancestors"]=>
array(1) {
["BrowseNode"]=>
array(4) {
["BrowseNodeId"]=>
string(4) "1000"
["Name"]=>
string(8) "Subjects"
["IsCategoryRoot"]=>
bool(true)
["Ancestors"]=>
array(1) {
["BrowseNode"]=>
array(2) {
["BrowseNodeId"]=>
string(6) "283155"
["Name"]=>
string(5) "Books"
}
}
}
}
}
}
}
[2]=>
array(3) {
["BrowseNodeId"]=>
string(5) "11256"
["Name"]=>
string(20) "Folklore & Mythology"
["Ancestors"]=>
array(1) {
["BrowseNode"]=>
array(3) {
["BrowseNodeId"]=>
string(5) "11232"
["Name"]=>
string(15) "Social Sciences"
["Ancestors"]=>
array(1) {
["BrowseNode"]=>
array(3) {
["BrowseNodeId"]=>
string(10) "3377866011"
["Name"]=>
string(26) "Politics & Social Sciences"
["Ancestors"]=>
array(1) {
["BrowseNode"]=>
array(4) {
["BrowseNodeId"]=>
string(4) "1000"
["Name"]=>
string(8) "Subjects"
["IsCategoryRoot"]=>
bool(true)
["Ancestors"]=>
array(1) {
["BrowseNode"]=>
array(2) {
["BrowseNodeId"]=>
string(6) "283155"
["Name"]=>
string(5) "Books"
}
}
}
}
}
}
}
}
}
</pre>
The point is I need to extract Value "Politics & Social Sciences" in this example and depth is not always the same. However every array starts the same from the end - Books -> Subjects -> Politics & Social Sciences
Since I am running hundreds of books through my script I need automated way of getting the 3rd level from the bottom. It is always 3rd level from the end of array.
i made these functions you can use to find third 'subject' from the end. but i think there is better apporach than using recursion (like me):
function isItEnd($array){
return !array_key_exists('Ancestors', $array);
}
function getThirdFromEnd($category){
if(!isItEnd($category)){
$away_from_parent = getThirdFromEnd($category['ancestors']['BrowseNode']);
if($away_from_parent == 0){
$away_from_parent = $category['ancestors']['BrowseNode'];
} else {
$away_from_parent--;
}
} else {
$away_from_parent = 2;
}
return $away_from_parent;
}
it recursively takes categories and goes deeper until isItEnd function return true (end of arrays reached), then by numeric count it returns third parent from the end
You can use it like this:
$thirds = array();
foreach($browseNodes['BrowseNodes']['BrowseNode'] as $category){
$thirds[] = getThirdFromEnd($category);
}
print_r($thirds);
working demo: https://3v4l.org/SeKvM
hope this helps, but wouln't be better to firstly parse the array into two dimensional (with stored 'parents') and then look for specific id or name?