构建搜索参数数组

时间:2015-06-23 06:08:37

标签: php arrays

我需要帮助解决一个问题。我有一个充满其他数组的数组。我需要:

  1. 遍历整个数组并构建一个名为public String getEMPData() { String[] columns = new String[] { EMP_ID, EMP_NAME, EMP_DOB, EMP_ADDR, EMP_PHONE }; Cursor c = ourDatabase.query(EMPINFO_TABLE_NAME, columns, null, null, null, null, null); String result = ""; int iRow = c.getColumnIndex(EMP_ID); int iName = c.getColumnIndex(EMP_NAME); int iDOB = c.getColumnIndex(EMP_DOB); int iADDR = c.getColumnIndex(EMP_ADDR); int iPHONE = c.getColumnIndex(EMP_PHONE); for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iDOB) + " " + c.getString(iADDR)+ " " + c.getString(iPHONE) + "\n"; }
  2. 的新数组
  3. 循环的每次迭代都将采用新的finalOptions并应用其他参数
  4. SearchIndex

    THEN:

    最终数组应包含此类数据

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

    基本上,我正在创建一个新数组并将其发送到另一个方法,该方法将执行对API的调用并返回结果,然后再次执行,直到我的数组选项完成。

    这可能不是我要去的方法,我正在寻找一种方法的建议/伪代码。以下是我到目前为止的基本知识:

    从此代码开始

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

    修改

    数组将包含更多值。即$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); } 将有1 - 10.其他人也会有更多的价值观。

1 个答案:

答案 0 :(得分:1)

从给定的数组中,它将产生54种可能的组合,如你所描述的那样。

此外,您需要确保将$allOptions['BrowseNode']中的数组编入索引为$allOptions['SearchIndex']的每个值。否则会产生错误。

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