构建搜索参数数组

构建搜索参数数组

问题描述:

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.

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