1

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.

A.L
  • 10,259
  • 10
  • 67
  • 98
Oktarin
  • 57
  • 2
  • 11
  • What is your own attempt to solve the problem? – Max Zuber Apr 29 '16 at 10:58
  • I was playing with some options (array_reverse, foreach, for...) but honestly I have no idea how to make last element as fixed point and work my way backward from it. – Oktarin Apr 29 '16 at 11:03

1 Answers1

1

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?

Jimmmy
  • 579
  • 12
  • 26
  • Thank you Jimmmy. However "ancestors" is being named through entire array multiple times. Can I use values "Books" or "Subject" instead? – Oktarin Apr 29 '16 at 11:13
  • 1
    i used 'ancestors' **because** its in entire array EXCEPT ending element, which is a way to find end of the arrays :) if you want to use 'Books' instead, then perform the check like this: if($category["name"]=="Books") and then accordingly alter the recursion function to not look for parents.. – Jimmmy Apr 29 '16 at 11:22
  • ahh right! now I get it. isItEnd will return true when it hits the last element that doesn't have "ancestor". – Oktarin Apr 29 '16 at 11:24
  • exactly, i added explanation into my answer, wasn't really clear before – Jimmmy Apr 29 '16 at 11:28
  • Jimmmy, I was playing with script, however when I test it with your array it works as expected. When I test it with my array I get "2". You can see example here: https://3v4l.org/n05eK. – Oktarin Apr 29 '16 at 12:50
  • i think you need to initiate foreach cycle to loop all "BrowseNode" elements. Everyone of them send to 'getThirdFromEnd' function.. – Jimmmy Apr 29 '16 at 13:14
  • I thought I did that.. didn't I? Or do you mean I should do this: `$category=$response['Items']['Item']['BrowseNodes'];` – Oktarin Apr 29 '16 at 13:54
  • Tried that - got only 1 result since it is all 1 array. Result == 2. – Oktarin Apr 29 '16 at 13:57
  • I have tried this: `$category=$response['Items']['Item']['BrowseNodes']['BrowseNode']; foreach ($category as $key => $value) { foreach ($value as $key => $cat) { $response_test=getThirdFromEnd($cat); echo "
    "; echo "
    " . print_r($response_test, 1) . "
    "; echo "
    "; } }` And then I get one result `Warning: array_key_exists() expects parameter 2 to be array, string given in /amazon/amazon-grab.php on line 71` and other two results are "2".
    – Oktarin Apr 29 '16 at 14:02
  • Jimmmy, thank you it works. Found typo because you searched for 'ancestors' as in your example code, and it really is 'Ancestors'. – Oktarin May 02 '16 at 06:47