PHP多维数组反向查找(Amazon产品API)


PHP multidimensional array reverse lookup (Amazon Product API)

我正在通过Amazon API访问产品详细信息,并在我的数据数组中获取这些信息:

["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>

重点是我需要在这个例子中提取价值"政治和社会科学",深度并不总是一样的。然而,每个数组从末尾开始都是一样的-书籍->主题->政治;社会科学

由于我正在通过我的脚本运行数百本书,我需要一种从底部获得第三级的自动化方式。它总是在数组末尾的第三级。

我制作了这些函数,您可以使用这些函数从末尾找到第三个"subject"。但我认为有比使用递归更好的方法(像我一样):

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;
}

它递归地获取类别并深入到isItEnd函数返回true(到达数组的末尾),然后通过数字计数返回末尾的第三个父级

你可以这样使用它:

$thirds = array();
foreach($browseNodes['BrowseNodes']['BrowseNode'] as $category){
     $thirds[] = getThirdFromEnd($category);
}
print_r($thirds);

工作演示:https://3v4l.org/SeKvM

希望这会有所帮助,但首先将数组解析为二维(带有存储的"parents"),然后查找特定的id或名称不是更好吗?