UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

421 lines (326 loc) 14.5 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://alsidewindowsz.com" rel="nofollow">vinyl </a> </strong> on 2012-05-15 23:55:04 <br /> I love using syntax its such a great system. I love learning new code it expands my brain capacity by 30%. <hr /> <strong> <a href="http://an3m1.com/" rel="nofollow"> ????? ????? ????</a> </strong> on 2012-04-23 14:48:15 <br /> This is a very informative article. I was looking for these things and here I found it. I am doing a project and this information is very useful me. Some things in here I have not thought about before <hr /> <strong> <a href="vaseto.eu" rel="nofollow">Vasil Vasilev</a> </strong> on 2012-01-25 16:19:26 <br /> Here is another implementation of the array_diff I am using in a project. I prefer to use for (;;) arrays to make sure I am iterating only over values, and will skip prototyped array members etc. <pre><code> var array_diff = function(arr1) { var retArr = [], arr1length, i, j, z, keyFound, argl = arguments.length, arr = []; pArr: for (i = 0, arr1length = arr1.length; i &lt; arr1length; i++) { keyFound = false; for (j = 1; j &lt; argl; j++) { arr = arguments[j]; for (var z = 0, arrLength = arr.length; z &lt; arrLength; z++) { if (arr[z] === arr1[i]) { keyFound = true; continue pArr; } } if (keyFound) { continue; } } if ( ! keyFound) { retArr.push(arr1[i]); } } return retArr; }; </code></pre> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2011-06-13 19:23:58 <br /> @George Wilde: Great, thanks for sharing! <hr /> <strong> George Wilde </strong> on 2011-06-13 12:14:55 <br /> Hey, Thanks for the code it worked perfectly as described :) I thought I would post my adaptation returning an array instead of an object. In doing this you sacrifice the original array keys but it suited my needs. <pre><code> function array_diff (arr1) { // http://kevin.vanzonneveld.net // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Sanjoy Roy // + revised by: Brett Zamir (http://brett-zamir.me) // * example 1: array_diff(['Kevin', 'van', 'Zonneveld'], ['van', 'Zonneveld']); // * returns 1: [0 =&gt; 'Kevin'] var retArr = [], argl = arguments.length, k1 = '', i = 1, k = '', arr = {}; arr1keys: for (k1 in arr1) { for (i = 1; i &lt; argl; i++) { arr = arguments[i]; for (k in arr) { if (arr[k] === arr1[k1]) { // If it reaches here, it was found in at least one array, so try next value continue arr1keys; } } retArr.push(arr1[k1]); } } return retArr; } </code></pre> <hr /> <strong> CoR </strong> on 2010-11-18 00:19:52 <br /> @Brett Zamir: Yes, I see what you mean... I was too fast to point array problem without reading faq. My bad ;) Anyway, in my case array_diff should work only with JS arrays and it MUST return fully functional JS array. [] and .push() works great :) Another 'solution' is making array_diffo that will use json objects as key:value array. But it's poor substitute for PHP's associative arrays. No length, no indexing... It's nice that this site exist. You have tons of useful code! <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-11-17 12:23:11 <br /> @CoR: Please see item 1 of the FAQ: http://wiki.github.com/kvz/phpjs/faq . The PHP way is to preserve keys which as you suggest is problematic with regular arrays (not to mention non-numeric keys). <hr /> <strong> CoR </strong> on 2010-11-16 04:39:09 <br /> <pre><code> var ar1 =[0,1,2,3,4]; var ar2 =[0,1,2]; var a = array_diff(ar1,ar2); console.log(a.length); // 5 </code></pre> p.s. a.length will fail because of var retArr = {}; After [] fix it will produce seemingly false .length of 5. a === [undefined, undefined, undefined,3,4]; <hr /> <strong> CoR </strong> on 2010-11-16 02:33:08 <br /> You have pretty nasty bug in code. JavaScript doesn't have real array. Length isn't real number of array items! .length = lastIndex + 1; Consider this: <pre><code> var ar = [0,1,2]; console.log(ar.length); // 3, and that's ok ar[900] = 3; console.log(ar.length); // 901 and that's horribly wrong! </code></pre> With ar[900] I just created array of 901 elements. First three are 0,1,2. Last element is 3. And in the middle I have 897 undefined elements. Pretty bad... Fix: var retArr = {}; // not good var retArr = []; // good retArr[k1] = arr1[k1]; // bug retArr.push(arr1[k1]); <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-11-08 16:40:26 <br /> @ Brett Zamir: Yes we have a FAQ! : ) Let's just take a moment to enjoy that fact :D Ok, good. I've changed the navigation a bit according to your ideas. As far as linking/making wiki pages for individual functions; I'm not too fond of that idea as there already is a lot of information about functions in the comments, and I think I'd like to keep it centralized that way. And we can't move all function comments to the wiki cause that would probably raise the bar for people to leave any comment at all. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-11-08 03:50:02 <br /> @Kevin: also, btw, thanks for adding the link! <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-11-08 03:49:09 <br /> @Kevin: Now that we actually have a FAQ on the wiki (at least the beginnings of one), do you want to link directly to that instead of the wiki? (or maybe call it &quot;FAQ/wiki&quot; to indicate that both are available there) I think it's important to indicate that we have FAQ answered there. Also, what do you think of making automated links on the function pages to the wiki for letting users contribute their own non-PHP variations, tips on use, etc.? <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-11-07 18:29:31 <br /> Link is on it's way! <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-10-26 00:50:33 <br /> It seems the comments problem is fixed now. And, sure, go ahead and add a link to the wiki; that'd be great... <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-10-25 13:25:53 <br /> @ Brett Zamir: The comments, no I'm not having those issues. Could you by any chance supply the errors? btw I removed the social javascript. maybe that fixes some issues as well. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-10-25 13:24:39 <br /> @ Brett Zamir: Thanks for baring with me Brett. I must say I'll sleep a little bit better knowing there's 1 less thing I need to maintain &amp; think of. I'll add a link to the github wiki and we'll see how it goes OK? <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-10-18 18:29:08 <br /> @Kevin: Also, by the way, do you know why phpjs.org hangs seemingly indefinitely after posting a comment here? I seem to always have to refresh the page... (there are also some red warnings displayed in the interim) <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-10-18 18:25:42 <br /> @Kevin: I guess using the GitHub wiki would be ok, but it really gets under my skin when these sites, instead of using the powerful and well-known Mediawiki, add frustratingly deficient wikis of their own (and I'm already unhappy with Git itself, or rather, the current lack of GUI tools to work with it as well as with Subversion). But to be fair to you (and it is not a big deal for the limited documentation we need), the GitHub wiki should work fine for this. <hr /> <strong> <a href="http://www.itsacon.net/" rel="nofollow">Itsacon</a> </strong> on 2009-10-09 12:47:34 <br /> I understand, and have by now run into the problem myself (array_diff with un-indexed arrays gives forced numeric indexes to the resulting array). If you choose to use generic Objects for arrays throughout, the implode() function should reflect this as well. I'll append a comment there. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-10-09 12:14:24 <br /> @ Brett Zamir: We could probably start one in the wiki. What do you think about hosting the wiki here: http://wiki.github.com/kvz/phpjs ? this way i wouldn't have to worry about updating media wiki, accountmanagement, and backups ? <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-10-09 05:06:36 <br /> @Itsacon: We are really kind of forced in most cases to treat arrays as objects in order to preserve PHP-like behavior of preserving keys (such as array_diff), including numeric keys. For example, to take Example #1 at http://php.net/array_diff , note that the key returned is '1' (with the value 'blue'). To do this with a regular array, we would either need to force the array to be of size 2, i.e.: &quot;[undefined, 'blue']&quot; or lose the key=1 information, i.e., &quot;['blue']&quot;. I also see that I hadn't updated our array_diff example to show an object being returned, so it was showing an array being returned instead, but that is now fixed in Git. If you really want to be able to return genuine arrays and don't mind losing key information (or don't mind getting an array which has &quot;undefined&quot; place-holders for empty numeric keys causing the size of the array to be inflated), then we could add some code to allow you to do something like: <pre><code>ini_set('phpjs.objectsAsArrays', 0);</code></pre> ...to indicate you want array inputs to be returned as arrays. But again, there are disadvantages to this, and we'd have to choose one of the two approaches (undefined items or no key information). @Kevin: If you ever decide to add a FAQ, I think this should be the #1 Question! :) <hr /> <strong> <a href="http://www.itsacon.net/" rel="nofollow">Itsacon</a> </strong> on 2009-10-08 10:35:49 <br /> There's a compatibility problem with this function and functions that take Array inputs (and check for it), like implode(). This is caused by this line: <pre><code>var arr1 = arguments[0], retArr = {};</code></pre> The retArr variable gets instantiated as a generic Object, not an Array. Because of this, the return value for this function is not an Array. I changed this line to <pre><code>var arr1 = arguments[0], retArr = new Array;</code></pre> This solved my problems. (Note that this also applies to several other array_ functions that (should) return Arrays) <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-03-13 15:51:53 <br /> @ Sanjoy Roy: Thank you, added! <hr /> <strong> Re-Ordering the Index </strong> on 2008-03-13 03:50:23 <br /> /////////////////////////////////////////////////////////// /* Sanjoy Roy's comments: ------------------------------- I have added a counter variable (cntr) and return array returns ordered array. This will help to get rid of 'undefined' values in the list when we use them in our program. */ ////////////////////////////////////////////////////////// <pre><code> function array_diff (array) { // http://kevin.vanzonneveld.net // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: array_diff(['Kevin', 'van', 'Zonneveld'], ['van', 'Zonneveld']); // * returns 1: ['Kevin'] var arr_dif = [], i = 1, argc = arguments.length, argv = arguments, key, key_c, found=false, cntr=0; // loop through 1st array for ( key in array ){ // loop over other arrays for (i = 1; i&amp;lt; argc; i++){ // find in the compare array found = false; for (key_c in argv[i]) { if (argv[i][key_c] == array[key]) { found = true; break; } } if(!found){ //arr_dif[key] = array[key]; arr_dif[cntr] = array[key]; cntr++; } } } return arr_dif; }; </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-01-22 09:00:40 <br /> @ Ates Goral: All together superb contributions. Greatly formatted and very consistent. I also like that you've included the example-return pairs in the comments. Thanks a LOT! <hr /> <strong> Ates Goral </strong> on 2008-01-22 05:37:46 <br /> Here's array_diff_key(): <pre><code> function array_diff_key(object) { // * example 1: array_diff_key({red: 1, green: 2, blue: 3, white: 4}); // * returns 1: {&amp;quot;red&amp;quot;:1, &amp;quot;green&amp;quot;:2, &amp;quot;blue&amp;quot;:3, &amp;quot;white&amp;quot;:4} // * example 2: array_diff_key({red: 1, green: 2, blue: 3, white: 4}, {red: 5}); // * returns 2: {&amp;quot;green&amp;quot;:2, &amp;quot;blue&amp;quot;:3, &amp;quot;white&amp;quot;:4} // * example 3: array_diff_key({red: 1, green: 2, blue: 3, white: 4}, {red: 5}, {green: 6, blue: 7}); // * returns 3: {&amp;quot;white&amp;quot;:4} // * example 4: array_diff_key({red: 1, green: 2, blue: 3, white: 4}, {red: 5}, {red: 5}); // * returns 4: {&amp;quot;green&amp;quot;:2, &amp;quot;blue&amp;quot;:3, &amp;quot;white&amp;quot;:4} var ret = new Object(); for (var key in object) { ret[key] = object[key]; } for (var argidx = 1; argidx &amp;lt; arguments.length; ++argidx) { var other = arguments[argidx]; if (other instanceof Object) { for (var key in other) { delete ret[key]; } } } return ret; } </code></pre> <hr />