UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

198 lines (161 loc) 7.1 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-04-22 18:54:39 <br /> @Shingo: Thanks for the report! Fixed in Git (with some other clean-up): http://github.com/kvz/phpjs/raw/master/functions/array/array_count_values.js <hr /> <strong> Shingo </strong> on 2010-04-21 17:29:43 <br /> Thanks but there is one problem. If my test array is like <pre><code> arr=[&quot;constructor&quot;]; <pre><code> It does not work. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-04-03 12:36:30 <br /> @ J-R: I'm running the test code with no problems: $ ./phpjstest.php array_count_values array/array_count_values.js returns#1 OKAY returns#2 OKAY returns#3 OKAY Could you provide the exact code that fails? What did you expect, and what did it return instead? That would help us fix the problem! Thx <hr /> <strong> <a href="none" rel="nofollow">J-R</a> </strong> on 2009-03-25 01:39:57 <br /> Hi, I am on firefox on a mac. This code doesn't seem to be working. I tried debugging it by putting alerts in the function. It looks ok except it doesn't return what I expect it to. I am using your example to try to produce the same result to no avail. <pre><code> &lt;script type='text/javascript'&gt; function array_count_values( array ) { // http://kevin.vanzonneveld.net // + original by: Ates Goral (http://magnetiq.com) // + namespaced by: Michael White (http://getsprink.com) // + input by: sankai // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: array_count_values([ 3, 5, 3, &quot;foo&quot;, &quot;bar&quot;, &quot;foo&quot; ]); // * returns 1: {3:2, 5:1, &quot;foo&quot;:2, &quot;bar&quot;:1} // * example 2: array_count_values({ p1: 3, p2: 5, p3: 3, p4: &quot;foo&quot;, p5: &quot;bar&quot;, p6: &quot;foo&quot; }); // * returns 2: {3:2, 5:1, &quot;foo&quot;:2, &quot;bar&quot;:1} // * example 3: array_count_values([ true, 4.2, 42, &quot;fubar&quot; ]); // * returns 3: {42:1, &quot;fubar&quot;:1} var tmp_arr = {}, key = '', t = ''; var __getType = function(obj) { // Objects are php associative arrays. var t = typeof obj; t = t.toLowerCase(); if (t == &quot;object&quot;) { t = &quot;array&quot;; } return t; } var __countValue = function (value) { switch (typeof(value)) { case &quot;number&quot;: if (Math.floor(value) != value) { return; } case &quot;string&quot;: if (value in this) { ++this[value]; } else { this[value] = 1; } } }; t = __getType(array); if (t == 'array') { for ( key in array ) { __countValue.call(tmp_arr, array[key]); } } return tmp_arr; } function formValidator(){ var tmpArray= []; tmpArray = array_count_values([ 3, 5, 3, &quot;foo&quot;, &quot;bar&quot;, &quot;foo&quot; ]); document.write(tmpArray[0] + tmpArray[1]+ tmpArray[2]); document.write(tmpArray[3] + tmpArray[4]+ tmpArray[5]); // document.write(tmpArray[0] + &quot; &quot; + tmpArray[1]+ &quot; &quot; + tmpArray[2] + &quot; &quot; + tmpArray[3]+ &quot; &quot; + tmpArray[4]+ &quot; &quot; + tmpArray[5]); } &lt;/script&gt; &lt;form id=&quot;frmchoixronde1&quot; name=&quot;frmchoixronde1&quot; method=&quot;POST&quot; enctype=&quot;application/x-www-form-urlencoded&quot; onsubmit='return formValidator()'&gt; &lt;html&gt;&lt;head&gt;&lt;title&gt;Page des poolers&lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;b&gt;Entrez vos choix pour la premiere ronde&lt;/b&gt; &lt;/br&gt;&lt;fieldset&gt; &lt;b&gt;&lt;label for=&quot;player_1&quot; style=&quot;width:2em&quot;&gt;1&lt;/label&gt;&lt;/b&gt;&lt;input name=&quot;player_1&quot; id=&quot;player_1&quot; type=&quot;text&quot; size=&quot;30&quot;&gt;&lt;/br&gt; &lt;b&gt;&lt;label for=&quot;player_2&quot; style=&quot;width:2em&quot;&gt;2&lt;/label&gt;&lt;/b&gt;&lt;input name=&quot;player_2&quot; id=&quot;player_2&quot; type=&quot;text&quot; size=&quot;30&quot;&gt;&lt;/br&gt; &lt;b&gt;&lt;label for=&quot;player_3&quot; style=&quot;width:2em&quot;&gt;3&lt;/label&gt;&lt;/b&gt;&lt;input name=&quot;player_3&quot; id=&quot;player_3&quot; type=&quot;text&quot; size=&quot;30&quot;&gt;&lt;/br&gt; &lt;b&gt;&lt;label for=&quot;player_4&quot; style=&quot;width:2em&quot;&gt;4&lt;/label&gt;&lt;/b&gt;&lt;input name=&quot;player_4&quot; id=&quot;player_4&quot; type=&quot;text&quot; size=&quot;30&quot;&gt;&lt;/br&gt; &lt;b&gt;&lt;label for=&quot;player_5&quot; style=&quot;width:2em&quot;&gt;5&lt;/label&gt;&lt;/b&gt;&lt;input name=&quot;player_5&quot; id=&quot;player_5&quot; type=&quot;text&quot; size=&quot;30&quot;&gt;&lt;/br&gt; &lt;b&gt;&lt;label for=&quot;player_6&quot; style=&quot;width:2em&quot;&gt;6&lt;/label&gt;&lt;/b&gt;&lt;input name=&quot;player_6&quot; id=&quot;player_6&quot; type=&quot;text&quot; size=&quot;30&quot;&gt;&lt;/br&gt; &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Soumettre vos choix&quot;&gt; &lt;/form&gt; &lt;/br&gt; &lt;/body&gt;&lt;/html&gt; </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-08-27 19:26:37 <br /> @ sankai: Thanks for pointing that out sankai! I've replaced the mozilla-only: forEach with a regular for loop. It should work fine now! <hr /> <strong> sankai </strong> on 2008-08-25 11:59:27 <br /> It seems because IE broswer don't support the mothod array.forEach().I search some solution from the google web. http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Global_Objects:Array:forEach I try add the code before array_count_values() <pre><code> if (!Array.prototype.forEach) { Array.prototype.forEach = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != &amp;quot;function&amp;quot;) throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i &amp;lt; len; i++) { if (i in this) fun.call(thisp, this[i], i, this); } }; } </code></pre> lol...It still can't work!!! Orz..do you have any idea,sir? <hr /> <strong> sankai </strong> on 2008-08-25 09:42:32 <br /> It can't work well in IE! The debug infomation is &amp;quot;Object don't support the attribute or method&amp;quot; in the code as <pre><code> if (array instanceof Array) { array.forEach(countValue, tmp_ar); } else if (array instanceof Object) { for ( key in array ) { countValue.call(tmp_ar, array[key]); } } </code></pre> å›§rz..but,It's working very well in Firefox!! <hr /> <strong> vikas </strong> on 2008-05-06 13:38:09 <br /> it very nice <hr />