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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况