UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

167 lines (133 loc) 5.05 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-03-26 21:12:18 <br /> @josh: And, btw, if you are only dealing with simple numeric arrays, to do that in simple JS style, just do <pre><code>var arr3 = arr1.concat(arr2);</code></pre> See also https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/concat <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-03-26 21:09:06 <br /> @josh: Thanks for pointing out the bug. Fixed in git: http://github.com/kvz/phpjs/raw/master/functions/array/array_merge.js . The code had problems when all arrays were input, but also with numeric renumbering. Sorry for the trouble on that one, but it should be working now. <hr /> <strong> josh </strong> on 2010-03-26 19:19:09 <br /> this doesn't return the expected values... arr1 = Array('zero','one','two','three'); arr2 = Array(4,5,6,7); arr3 = array_merge(arr1,arr2); php does this: arr3: Array ( [0] =&gt; zero [1] =&gt; one [2] =&gt; two [3] =&gt; three [4] =&gt; 4 [5] =&gt; 5 [6] =&gt; 6 [7] =&gt; 7 ) while js function does this: 0: 0: zero 1: one 2: two 3: three 1: 0: 4 1: 5 2: 6 3: 7 i can put two arrays into a bigger array in regular javascript... 9_9 <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-10-08 21:03:45 <br /> @ Subhasis: But don't we already have an array_merge_recursive function? <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-09-27 01:20:46 <br /> @Matheus : That probably just means that the function is returning an object and you are trying to view it as a string (like in an alert). Why does the function return an object? In order to allow for associative arrays, we must use objects in such cases (though this function tries to return a bona fide array if possible). <hr /> <strong> Matheus </strong> on 2009-09-26 15:03:01 <br /> This funcion don't work. It's returning [object Object] <hr /> <strong> <a href="http://weread.com" rel="nofollow">Subhasis</a> </strong> on 2008-12-01 00:15:40 <br /> @Kevin, actually its not the &amp;quot;array_merge&amp;quot; function but rather the &amp;quot;array_merge_recursive&amp;quot; function.. it can accept nested array/object which it will merge recursively... one point to walk over is it gives preference to array object.. so it wont overwrite array/objects if the master array has it... e.g 'cc':[6,8], 'cc':8; o/p: [6,8].. ------------------------------------------ Subhasis http://weread.com <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-11-25 17:07:03 <br /> @ Nate: Thanks dude! @ Subhasis Deb: Could you please tell us how your function would be the better implementation? <hr /> <strong> <a href="http://weread.com" rel="nofollow">Subhasis Deb</a> </strong> on 2008-11-25 09:08:09 <br /> Following is my version of array_merge (compact): /*** * Simulate php array_merge function * * @param {Object/Array} arr1 * @param {Object/Array} arr2 * var a1 = {'aa':100, 'bb':2, 'cc':[6,7], 'dd':[12,13], 'ee':{'15':15,'16':16}}; * var b1 = {'xx':101, 'bb':5, 'cc':8, 'dd':[14,15], 'ee':{'17':17,'18':18}}; * var c = array_merge(a1, b1); * console.log(c) [in firebug] * Output: {'aa':100, 'bb': 5, 'cc':[6,7], 'dd':[12,13,14,15], 'ee':{'15':15,'16':16,'17':17,'18':18}, 'xx':101} */ array_merge : function(arr1, arr2){ if((arr1 &amp;amp;&amp;amp; (arr1 instanceof Array)) &amp;amp;&amp;amp; (arr2 &amp;amp;&amp;amp; (arr2 instanceof Array))){ for (var idx in arr2) { arr1.push(arr2[idx]); } }else if((arr1 &amp;amp;&amp;amp; (arr1 instanceof Object)) &amp;amp;&amp;amp; (arr2 &amp;amp;&amp;amp; (arr2 instanceof Object))){ for(var idx in arr2){ if(idx in arr1){ if (typeof arr1[idx] == 'object' &amp;amp;&amp;amp; typeof arr2 == 'object') { arr1[idx] = array_merge(arr1[idx], arr2[idx]); }else{ arr1[idx] = arr2[idx]; } }else{ arr1[idx] = arr2[idx]; } } } return arr1; }, <hr /> <strong> Nate </strong> on 2008-11-19 06:14:33 <br /> Neither &amp;quot;ct&amp;quot; nor &amp;quot;retArr&amp;quot; are declared with &amp;quot;val&amp;quot;, and therefore create unnecessary global variables. I suggest that &amp;quot;retArr&amp;quot; be declared at the top and &amp;quot;ct&amp;quot; be declared after the code: <pre><code> if (retArr) { return args; } </code></pre> The reason for this is because the function sometimes finishes before ever reaching the part that uses &amp;quot;ct&amp;quot;, and therefore, it could waste a slight amount of time declaring a variable that it didn't end up using. The same is also true for &amp;quot;retObj&amp;quot;, &amp;quot;k&amp;quot;, and &amp;quot;j&amp;quot;. But it's just a suggestion. <hr />