douni9620 2015-06-23 06:08
浏览 43
已采纳

构建搜索参数数组

I need some help wrapping my head around a problem. I have an array filled with other arrays. I need to:

  1. Loop through the entire array and build a new array called finalOptions
  2. Each iteration of the loop will take a new SearchIndex and apply the other paramenters

i.e

SearchIndex => SportingGoods
MinPercentageOff => 50
MinimumPrice => 1
ItemPage => 1
Sort => salesrank
BrowseNode => 2342470011

THEN:

Final array should contain data like this

SearchIndex => SportingGoods
MinPercentageOff => 60
MinimumPrice => 100
ItemPage => 2
Sort => salesrank
BrowseNode => 3403201

Basically, I'm creating a new array and sending it to another method that will execute a call to an API and return a result, then doing it again until my array options are complete.

This might not be the way to go and I'm looking for suggestions/pseudo code on an approach. Here is the basics of what I have so far:

Starting with this code

$allOptions = array(
    "SearchIndex"           => array("SportingGoods", "Tools"),
    "MinPercentageOff"      => array("50", "60", "70"),
    "MinimumPrice"          => array("1", "100", "1000"),
    "ItemPage"              => array("1", "2"),
    "Sort"                  => array("salesrank")
    "BrowseNode"            => array(
                                    "SportingGoods" => array("2342470011", "3403201"),
                                    "Tools"         => array("511364")
                            )
)

$finalOptions = array();

foreach($allOptions as $options){
    foreach($options["SearchIndex"] as $searchIndex){
        $finalOptions[] = "SearchIndex" => $searchIndex[]
    }


    $this->itemSearch($finalOptions);
}

EDIT

The arrays will contain more values. i.e "ItemPage" => array("1", "2"), will have 1 - 10. The others will have more values as well.

  • 写回答

1条回答 默认 最新

  • douti0687 2015-06-23 06:44
    关注

    From the given array it will produce 54 possible combinations as you described.

    Also you need to make sure you have array in $allOptions['BrowseNode'] indexed as each value of $allOptions['SearchIndex']. Otherwise it will produce error.

    Cartesian function from here.

    $allOptions = [
        "SearchIndex"           => ["SportingGoods", "Tools"],
        "MinPercentageOff"      => ["50", "60", "70"],
        "MinimumPrice"          => ["1", "100", "1000"],
        "ItemPage"              => ["1", "2"],
        "Sort"                  => ["salesrank"],
        "BrowseNode"            => ["SportingGoods" => ["2342470011", "3403201"], "Tools" => ["511364"] ] ];
    
    
    $finalOptions = $allOptions; // copy our initial $allOptions array
    
    unset($finalOptions['BrowseNode']); // deal with BrowseNode key later with custom iterator
    
    $cartesian_product = cartesian($finalOptions); // find cartesian except BrowseNode
    
    foreach($cartesian_product as $cartesian) // each member of cartesian product will iterate here
    {
        foreach($allOptions['BrowseNode'][$cartesian['SearchIndex']] as $possible)
        /*
            We have unset the BrowseNode, so need to refer original $allOptions array for BrowseNode,
            In every cartesian product, we will get $cartesian['SearchIndex'] and it will contain either
            'SportingGoods' or 'Tools' , so in our original array, look for 'BrowseNode' value, having key
            same as $cartesian['SearchIndex'].
    
            $allOptions['BrowseNode'][$cartesian['SearchIndex']] <---- is similar to below two lines
    
            $key = $cartesian['SearchIndex'];
            $allOptions['BrowseNode'][$key];
    
            Finally iterate through $allOptions['BrowseNode'][$cartesian['SearchIndex']] will iterate as many times,
            as many values there are
        */
        {
            $cartesian['BrowseNode'] = $possible; // assign the long waited key here to 'BrowseNode'
            var_dump($cartesian); // here you can do $this->itemSearch($cartesian);
        }
    }
    
    function cartesian($input) {
    
        $input = array_filter($input);
        /*  
            will renove any false values in input array,
            in our array's case, it will do nothing.
        */
        $result = [[]];
    
        foreach ($input as $key => $values) {
            $append = [];
    
            foreach($result as $product) {
                foreach($values as $item) {
    
                    $product [$key] = $item;
                    $append [] = $product;
    
                }
            }
    
            $result = $append;
        }
    
        return $result;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程