简体   繁体   English

如何将此php函数转换为jquery函数。 它从数组返回值的所有可能组合。

[英]how to convert this php function to jquery function. It returns every possible combination of the values from an array.

I found this PHP code from here : 我从这里找到了这个PHP代码:

function everyCombination($array) {
  $arrayCount      = count($array);
  $maxCombinations = pow($arrayCount, $arrayCount);
  $returnArray     = array();
  $conversionArray = array();
  foreach ($array as $key => $value) {
    $conversionArray[base_convert($key, 10, $arrayCount)] = $value;
  }
  for ($i = 0; $i < $maxCombinations; $i++) {
    $combination = base_convert($i, 10, $arrayCount);
    $combination = str_pad($combination, $arrayCount, "0", STR_PAD_LEFT);
    $returnArray[] = strtr($combination, $conversionArray);
  }
  return $returnArray;
}

print_r(everyCombination(array('a', 'b', 'c', 'd')));

which is what I need. 这就是我所需要的。 But I need it to be in jquery so I tried to convert the code to jquery. 但是我需要它在jquery中,所以我尝试将代码转换为jquery。

This is how I did it. 这就是我做的。

function everyCombination(arr)
{   //https://stackoverflow.com/questions/14022215/php-how-do-you-generate-all-possible-combinations-of-values-in-an-array
      var arrCount      = arr.length;
      var maxCombinations = Math.pow(arrCount, arrCount);
      var returnArr     = [];
      var conversionArr = [];

    $.each(arr, function( key, value )
    {

        conversionArr[base_convert(key, 10, arrCount)] = value;
    }); 
      for (i = 0; i < maxCombinations; i++)
      {
        var combination = base_convert(i, 10, arrCount);
       //var combination = str_pad(combination, arrCount, "0", STR_PAD_LEFT);
        var combination = str_pad(arrCount,"0",combination,true);
        var returnArr = strtr(combination, conversionArr);
      }
      return returnArr;
}

I tried to alert the data returned by everyCombination() function like this: 我试图提醒每个像这样的everyCombination()函数返回的数据:

alert(everyCombination(['a', 'b', 'c', 'd'])); 

but i get only 但我只有

dddd

instead of an array composed of; 而不是由组成的数组

Array ( [0] => aaaa [1] => aaab [2] => aaac [3] => aaad [4] => aaba [5] => aabb [6] => aabc [7] => aabd [8] => aaca [9] => aacb [10] => aacc [11] => aacd [12] => aada [13] => aadb [14] => aadc [15] => aadd [16] => abaa [17] => abab [18] => abac [19] => abad [20] => abba [21] => abbb [22] => abbc [23] => abbd [24] => abca [25] => abcb [26] => abcc [27] => abcd [28] => abda [29] => abdb [30] => abdc [31] => abdd [32] => acaa [33] => acab [34] => acac [35] => acad [36] => acba [37] => acbb [38] => acbc [39] => acbd [40] => acca [41] => accb [42] => accc [43] => accd [44] => acda [45] => acdb [46] => acdc [47] => acdd [48] => adaa [49] => adab [50] => adac [51] => adad [52] => adba [53] => adbb [54] => adbc [55] => adbd [56] => adca [57] => adcb [58] => adcc [59] => adcd [60] => adda [61] => addb [62] => addc [63] => addd [64] => baaa [65] => baab [66] => baac [67] => baad [68] => baba [69] => babb [70] => babc [71] => babd [72] => baca [73] => bacb [74] => bacc [75] => bacd [76] => bada [77] => badb [78] => badc [79] => badd [80] => bbaa [81] => bbab [82] => bbac [83] => bbad [84] => bbba [85] => bbbb [86] => bbbc [87] => bbbd [88] => bbca [89] => bbcb [90] => bbcc [91] => bbcd [92] => bbda [93] => bbdb [94] => bbdc [95] => bbdd [96] => bcaa [97] => bcab [98] => bcac [99] => bcad [100] => bcba [101] => bcbb [102] => bcbc [103] => bcbd [104] => bcca [105] => bccb [106] => bccc [107] => bccd [108] => bcda [109] => bcdb [110] => bcdc [111] => bcdd [112] => bdaa [113] => bdab [114] => bdac [115] => bdad [116] => bdba [117] => bdbb [118] => bdbc [119] => bdbd [120] => bdca [121] => bdcb [122] => bdcc [123] => bdcd [124] => bdda [125] => bddb [126] => bddc [127] => bddd [128] => caaa [129] => caab [130] => caac [131] => caad [132] => caba [133] => cabb [134] => cabc [135] => cabd [136] => caca [137] => cacb [138] => cacc [139] => cacd [140] => cada [141] => cadb [142] => cadc [143] => cadd [144] => cbaa [145] => cbab [146] => cbac [147] => cbad [148] => cbba [149] => cbbb [150] => cbbc [151] => cbbd [152] => cbca [153] => cbcb [154] => cbcc [155] => cbcd [156] => cbda [157] => cbdb [158] => cbdc [159] => cbdd [160] => ccaa [161] => ccab [162] => ccac [163] => ccad [164] => ccba [165] => ccbb [166] => ccbc [167] => ccbd [168] => ccca [169] => cccb [170] => cccc [171] => cccd [172] => ccda [173] => ccdb [174] => ccdc [175] => ccdd [176] => cdaa [177] => cdab [178] => cdac [179] => cdad [180] => cdba [181] => cdbb [182] => cdbc [183] => cdbd [184] => cdca [185] => cdcb [186] => cdcc [187] => cdcd [188] => cdda [189] => cddb [190] => cddc [191] => cddd [192] => daaa [193] => daab [194] => daac [195] => daad [196] => daba [197] => dabb [198] => dabc [199] => dabd [200] => daca [201] => dacb [202] => dacc [203] => dacd [204] => dada [205] => dadb [206] => dadc [207] => dadd [208] => dbaa [209] => dbab [210] => dbac [211] => dbad [212] => dbba [213] => dbbb [214] => dbbc [215] => dbbd [216] => dbca [217] => dbcb [218] => dbcc [219] => dbcd [220] => dbda [221] => dbdb [222] => dbdc [223] => dbdd [224] => dcaa [225] => dcab [226] => dcac [227] => dcad [228] => dcba [229] => dcbb [230] => dcbc [231] => dcbd [232] => dcca [233] => dccb [234] => dccc [235] => dccd [236] => dcda [237] => dcdb [238] => dcdc [239] => dcdd [240] => ddaa [241] => ddab [242] => ddac [243] => ddad [244] => ddba [245] => ddbb [246] => ddbc [247] => ddbd [248] => ddca [249] => ddcb [250] => ddcc [251] => ddcd [252] => ddda [253] => dddb [254] => dddc [255] => dddd )

below are some functions from phpjs that are used by everyCombination(); 下面是从一些功能phpjs由everyCombination()中使用;

 function base_convert(number, frombase, tobase) {
      //  discuss at: http://phpjs.org/functions/base_convert/
      // original by: Philippe Baumann
      // improved by: Rafał Kukawski (http://blog.kukawski.pl)
      //   example 1: base_convert('A37334', 16, 2);
      //   returns 1: '101000110111001100110100'

      return parseInt(number + '', frombase | 0)
        .toString(tobase | 0);
    }


    //str_pad($str,20,".",STR_PAD_LEFT); //in php
    function str_pad(len, pad, str, leftPadded)
    {
        //http://jsperf.com/string-padding-performance
          if (str == undefined) return Array(len - 1).join(pad);
          if (leftPadded) {
            return Array(len + 1 - str.length).join(pad) + str;
          } else {
            return str + Array(len + 1 - str.toString().length).join(pad);
          }
    }


    function strtr(str, from, to)
    {
        //http://phpjs.org/functions/strtr/
      //  discuss at: http://phpjs.org/functions/strtr/
      // original by: Brett Zamir (http://brett-zamir.me)
      //    input by: uestla
      //    input by: Alan C
      //    input by: Taras Bogach
      //    input by: jpfle
      // bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
      // bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
      // bugfixed by: Brett Zamir (http://brett-zamir.me)
      // bugfixed by: Brett Zamir (http://brett-zamir.me)
      //  depends on: krsort
      //  depends on: ini_set
      //   example 1: $trans = {'hello' : 'hi', 'hi' : 'hello'};
      //   example 1: strtr('hi all, I said hello', $trans)
      //   returns 1: 'hello all, I said hi'
      //   example 2: strtr('äaabaåccasdeöoo', 'äåö','aao');
      //   returns 2: 'aaabaaccasdeooo'
      //   example 3: strtr('ääääääää', 'ä', 'a');
      //   returns 3: 'aaaaaaaa'
      //   example 4: strtr('http', 'pthxyz','xyzpth');
      //   returns 4: 'zyyx'
      //   example 5: strtr('zyyx', 'pthxyz','xyzpth');
      //   returns 5: 'http'
      //   example 6: strtr('aa', {'a':1,'aa':2});
      //   returns 6: '2'

      var fr = '',
        i = 0,
        j = 0,
        lenStr = 0,
        lenFrom = 0,
        tmpStrictForIn = false,
        fromTypeStr = '',
        toTypeStr = '',
        istr = '';
      var tmpFrom = [];
      var tmpTo = [];
      var ret = '';
      var match = false;

      // Received replace_pairs?
      // Convert to normal from->to chars
      if (typeof from === 'object') {
        tmpStrictForIn = this.ini_set('phpjs.strictForIn', false); // Not thread-safe; temporarily set to true
        from = this.krsort(from);
        this.ini_set('phpjs.strictForIn', tmpStrictForIn);

        for (fr in from) {
          if (from.hasOwnProperty(fr)) {
            tmpFrom.push(fr);
            tmpTo.push(from[fr]);
          }
        }

        from = tmpFrom;
        to = tmpTo;
      }

      // Walk through subject and replace chars when needed
      lenStr = str.length;
      lenFrom = from.length;
      fromTypeStr = typeof from === 'string';
      toTypeStr = typeof to === 'string';

      for (i = 0; i < lenStr; i++) {
        match = false;
        if (fromTypeStr) {
          istr = str.charAt(i);
          for (j = 0; j < lenFrom; j++) {
            if (istr == from.charAt(j)) {
              match = true;
              break;
            }
          }
        } else {
          for (j = 0; j < lenFrom; j++) {
            if (str.substr(i, from[j].length) == from[j]) {
              match = true;
              // Fast forward
              i = (i + from[j].length) - 1;
              break;
            }
          }
        }
        if (match) {
          ret += toTypeStr ? to.charAt(j) : to[j];
        } else {
          ret += str.charAt(i);
        }
      }

      return ret;
    }


    function ini_set(varname, newvalue) {
      //  discuss at: http://phpjs.org/functions/ini_set/
      // original by: Brett Zamir (http://brett-zamir.me)
      //        note: This will not set a global_value or access level for the ini item
      //   example 1: ini_set('date.timezone', 'Asia/Hong_Kong');
      //   example 1: ini_set('date.timezone', 'America/Chicago');
      //   returns 1: 'Asia/Hong_Kong'

      var oldval = '';
      var self = this;

      try {
        this.php_js = this.php_js || {};
      } catch (e) {
        this.php_js = {};
      }

      this.php_js.ini = this.php_js.ini || {};
      this.php_js.ini[varname] = this.php_js.ini[varname] || {};

      oldval = this.php_js.ini[varname].local_value;

      var _setArr = function(oldval) {
        // Although these are set individually, they are all accumulated
        if (typeof oldval === 'undefined') {
          self.php_js.ini[varname].local_value = [];
        }
        self.php_js.ini[varname].local_value.push(newvalue);
      };

      switch (varname) {
        case 'extension':
          if (typeof this.dl === 'function') {
            // This function is only experimental in php.js
            this.dl(newvalue);
          }
          _setArr(oldval, newvalue);
          break;
        default:
          this.php_js.ini[varname].local_value = newvalue;
          break;
      }

      return oldval;
    }

    function krsort(inputArr, sort_flags) {
      //  discuss at: http://phpjs.org/functions/krsort/
      // original by: GeekFG (http://geekfg.blogspot.com)
      // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
      // improved by: Brett Zamir (http://brett-zamir.me)
      //        note: The examples are correct, this is a new way
      //        note: This function deviates from PHP in returning a copy of the array instead
      //        note: of acting by reference and returning true; this was necessary because
      //        note: IE does not allow deleting and re-adding of properties without caching
      //        note: of property position; you can set the ini of "phpjs.strictForIn" to true to
      //        note: get the PHP behavior, but use this only if you are in an environment
      //        note: such as Firefox extensions where for-in iteration order is fixed and true
      //        note: property deletion is supported. Note that we intend to implement the PHP
      //        note: behavior by default if IE ever does allow it; only gives shallow copy since
      //        note: is by reference in PHP anyways
      //        note: Since JS objects' keys are always strings, and (the
      //        note: default) SORT_REGULAR flag distinguishes by key type,
      //        note: if the content is a numeric string, we treat the
      //        note: "original type" as numeric.
      //  depends on: i18n_loc_get_default
      //   example 1: data = {d: 'lemon', a: 'orange', b: 'banana', c: 'apple'};
      //   example 1: data = krsort(data);
      //   example 1: $result = data
      //   returns 1: {d: 'lemon', c: 'apple', b: 'banana', a: 'orange'}
      //   example 2: ini_set('phpjs.strictForIn', true);
      //   example 2: data = {2: 'van', 3: 'Zonneveld', 1: 'Kevin'};
      //   example 2: krsort(data);
      //   example 2: $result = data
      //   returns 2: {3: 'Kevin', 2: 'van', 1: 'Zonneveld'}

      var tmp_arr = {},
        keys = [],
        sorter, i, k, that = this,
        strictForIn = false,
        populateArr = {};

      switch (sort_flags) {
        case 'SORT_STRING':
          // compare items as strings
          sorter = function(a, b) {
            return that.strnatcmp(b, a);
          };
          break;
        case 'SORT_LOCALE_STRING':
          // compare items as strings, original by the current locale (set with  i18n_loc_set_default() as of PHP6)
          var loc = this.i18n_loc_get_default();
          sorter = this.php_js.i18nLocales[loc].sorting;
          break;
        case 'SORT_NUMERIC':
          // compare items numerically
          sorter = function(a, b) {
            return (b - a);
          };
          break;
        case 'SORT_REGULAR':
          // compare items normally (don't change types)
        default:
          sorter = function(b, a) {
            var aFloat = parseFloat(a),
              bFloat = parseFloat(b),
              aNumeric = aFloat + '' === a,
              bNumeric = bFloat + '' === b;
            if (aNumeric && bNumeric) {
              return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0;
            } else if (aNumeric && !bNumeric) {
              return 1;
            } else if (!aNumeric && bNumeric) {
              return -1;
            }
            return a > b ? 1 : a < b ? -1 : 0;
          };
          break;
      }

      // Make a list of key names
      for (k in inputArr) {
        if (inputArr.hasOwnProperty(k)) {
          keys.push(k);
        }
      }
      keys.sort(sorter);

      // BEGIN REDUNDANT
      this.php_js = this.php_js || {};
      this.php_js.ini = this.php_js.ini || {};
      // END REDUNDANT
      strictForIn = this.php_js.ini['phpjs.strictForIn'] && this.php_js.ini['phpjs.strictForIn'].local_value && this.php_js
        .ini['phpjs.strictForIn'].local_value !== 'off';
      populateArr = strictForIn ? inputArr : populateArr;

      // Rebuild array with sorted key names
      for (i = 0; i < keys.length; i++) {
        k = keys[i];
        tmp_arr[k] = inputArr[k];
        if (strictForIn) {
          delete inputArr[k];
        }
      }
      for (i in tmp_arr) {
        if (tmp_arr.hasOwnProperty(i)) {
          populateArr[i] = tmp_arr[i];
        }
      }

      return strictForIn || populateArr;
    }

Where did I go wrong in the converting this function? 在转换此函数时我哪里出错了? or if you have any alternatives, that will do to. 或者,如果您有其他选择,也可以这样做。

I forgot to declare returnArr as array so it's value is being changed each time the for loop loops. 我忘了将returnArr声明为数组,因此每次for循环循环时,其值都会更改。

I fixed it by declaring it as an array and for each loop, i push the value to the array. 我通过将其声明为数组来修复它,并为每个循环将值推送到数组。

function everyCombination(arr)
{   //http://stackoverflow.com/questions/14022215/php-how-do-you-generate-all-possible-combinations-of-values-in-an-array
      var arrCount      = arr.length;
      var maxCombinations = Math.pow(arrCount, arrCount);
      var returnArr     = [];
      var conversionArr = [];
        var returnArr = [];

    $.each(arr, function( key, value )
    {

        conversionArr[base_convert(key, 10, arrCount)] = value;


    }); 
      for (i = 0; i < maxCombinations; i++)
      {
        var combination = base_convert(i, 10, arrCount);
       //var combination = str_pad(combination, arrCount, "0", STR_PAD_LEFT);
        var combination = str_pad(arrCount,"0",combination,true);
         returnArr.push(strtr(combination, conversionArr));
      }
      return returnArr;
}

It's because you are using a string variable, and the last value is stored. 这是因为您正在使用字符串变量,并且存储了最后一个值。 You need to use array variable to achieve your goals. 您需要使用数组变量来实现您的目标。
Try this (the global functions are put inside the global.js file) : 试试这个(全局函数放在global.js文件中):

<!doctype html>
<html>
  <head>
    <title>http://stackoverflow.com/questions/35282110/jquery-function-which-returns-every-possible-combination-from-array-convert-php</title>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="global.js"></script>
  </head>
  <body>
    <script>
        function everyCombination(arr)
        {   //http://stackoverflow.com/questions/14022215/php-how-do-you-generate-all-possible-combinations-of-values-in-an-array
              var arrCount      = arr.length;
              var maxCombinations = Math.pow(arrCount, arrCount);
              var returnArr     = [];
              var conversionArr = [];

            $.each(arr, function( key, value )
            {

                conversionArr[base_convert(key, 10, arrCount)] = value;
            }); 
            var returnArr = [];
            for (i = 0; i < maxCombinations; i++)
            {
              var combination = base_convert(i, 10, arrCount);
             //var combination = str_pad(combination, arrCount, "0", STR_PAD_LEFT);
              var combination = str_pad(arrCount,"0",combination,true);
              returnArr.push(strtr(combination, conversionArr));
            }
            return returnArr;
        }
      (function(){
        alert(everyCombination(['a', 'b', 'c', 'd']));
      })();

    </script>
  </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数组发送到其他函数。(jQuery) - How to send array to other function.(Jquery) 如何在数组中的每个 Object 中添加 Function 作为键值返回 - How to Add Function Returns as Key-Values in Every Object in an Array 箭头函数不是函数。 是 Array 的一个实例。 反应原生钩子 - Arrow function is not a function. is an instance of Array. React-native hooks 我如何从此Firebase集合查询功能中获取“ retArr”数组。 我想将数组值用于其他功能 - How do i get the “retArr” array out from this firebase collection query function. I want the array values to used for another function 退出匿名功能。 jQuery的 - Exit from anonymous function. jQuery 从JQuery函数返回html。 - Returning html from a JQuery function. 使用适当的数组响应HTTP请求时遇到问题。 使用ExpressJS从函数内部发送结果的可能方法? - Having issues responding to an HTTP request with the proper array. Possible way to send results from within function with expressjs? 将数组和其他参数传递给函数。 怎么样? - Pass an array and further arguments into a function. How? 您如何编写一个从数组返回值的函数? - How do your write a function that returns values from an array? 如何将innerHTML转换为可以使用.size()函数的数组。 等等 - How to convert innerHTML into an array where I can use .size() function. etc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM