phpjs
Version:
1 lines • 1.4 MB
JSON
{"abs":[{"date":"2008-01-30 11:03:04","author":"Karol Kowalski","url":"","comment":"Why not use native JS Math.abs for this. I believe this should be faster than conditional expressions.\r\n\r\n[CODE="Javascript"]\r\nfunction abs( mixed_number ) {\r\n \/\/ http:\/\/kevin.vanzonneveld.net\r\n \/\/ + original by: _argos\r\n \/\/ * example 1: abs(4.2);\r\n \/\/ * returns 1: 4.2\r\n \/\/ * example 2: abs(-4.2);\r\n \/\/ * returns 2: 4.2\r\n \/\/ * example 3: abs(-5);\r\n \/\/ * returns 3: 5\r\n \/\/ * example 4: abs('_argos');\r\n \/\/ * returns 4: 0\r\n\r\n\tvar abs=Math.abs( mixed_number )\r\n\treturn ( !isNaN ( abs) ) ? abs : 0\r\n}\r\n[\/CODE]"},{"date":"2008-01-30 12:48:35","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Karol Kowalski: Using native Javascript for this is probably best so I've updated the function. But I'm not totally certain that this is faster. Doesn't the Math libary have to be loaded or something? Can somebod shed a light on this?\r\n\r\nFor reference, this was the original by _argos:\r\n[CODE="Javascript"]\r\nfunction abs( mixed_number ) {\r\n \/\/ + original by: _argos\r\n return ( ( !isNaN ( mixed_number ) ) ? ( ( mixed_number < 0 ) ? ( mixed_number * -1 ) : mixed_number ) : 0 );\r\n}\r\n[\/CODE]"},{"date":"2008-01-30 15:03:31","author":"Karol Kowalski","url":"","comment":"I run a test and it appeared that my function with Math.abs was around 40% more time consuming than yours. I tried to optimize it and ended up with 20% gain, and the code is still smaller and more readable. I tested it in Firefox, IE7, Safari and Opera with similar results. To see what's causing the overhead I run a function that would just return Math.abs, withoud checking for validity, still it was 10% slower than your function. It seems that Math.abs API is always slower than what can be accomplished with JS hack, a bit sad.\r\n\r\nStill, for code readability I would sugget using the code of abs_math2.\r\n\r\nHere's the test code:\r\n[CODE="Javascript"]\r\nif (!window['console']) {\r\n\r\nconsole={}\r\nconsole.log=alert\r\n}\r\n\r\nvar start;\r\n\r\nfunction abs_math2 ( mixed_number ) {\r\n return ( ( isNaN ( mixed_number ) ) ? 0 : Math.abs ( mixed_number ) );\r\n}\r\n\r\nfunction abs_math( mixed_number ) {\r\n var abs=Math.abs( mixed_number );\r\n return ( !isNaN ( abs ) ) ? abs : 0\r\n}\r\n\r\nfunction abs_cond( mixed_number ) {\r\n return ( ( !isNaN ( mixed_number ) ) ? ( ( mixed_number < 0 ) ? ( mixed_number * -1 ) : mixed_number ) : 0 );\r\n}\r\n\r\n\r\nstart=new Date();\r\n\r\nfor (var i=100000;i;i--) {\r\n\r\nabs_cond(4.2);\r\nabs_cond(-4.2);\r\nabs_cond(-5);\r\nabs_cond('_argos');\r\nabs_cond(-Infinity);\r\n\r\n}\r\n\r\nconsole.log((new Date())-start)\r\n\r\n\r\nstart=new Date();\r\n\r\nfor (var i=100000;i;i--) {\r\n\r\nabs_math(4.2);\r\nabs_math(-4.2);\r\nabs_math(-5);\r\nabs_math('_argos');\r\nabs_math(-Infinity);\r\n\r\n}\r\n\r\nconsole.log((new Date())-start)\r\n\r\n\r\nstart=new Date();\r\n\r\nfor (var i=100000;i;i--) {\r\n\r\nabs_math2(4.2);\r\nabs_math2(-4.2);\r\nabs_math2(-5);\r\nabs_math2('_argos');\r\nabs_math2(-Infinity);\r\n\r\n}\r\n\r\nconsole.log((new Date())-start)\r\n\r\n\r\n\/\/my results\r\n\/\/Firefox 2.0.11\r\n\/\/1000\r\n\/\/1422\r\n\/\/1219\r\n\r\n\r\n[\/CODE]"},{"date":"2008-01-30 15:43:39","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Karol Kowalski: Awesome work Karol! I've updated the function. Thanks for putting in the extra effort. Greatly appreciated!"},{"date":"2008-01-30 22:24:48","author":"_argos","url":"","comment":"@Karol Kowalski : Hi, thanks for take the time to check my port, I make the same tests like you, because I think that native functions always are more slower, so ever I use self hacks :p again thanxs for your time.\r\n\r\nPS: Sorry for my badly English, but i have 4 years without use it :p"},{"date":"2008-01-31 17:33:29","author":"_argos","url":"","comment":"Hi Kevin I'm here again look this ports.\r\n\r\n[CODE="Javascript"]\r\nif ( defined ( 'CONSTANTE' ) ) {\r\n\r\n console.log ( CONSTANTE );\r\n\r\n}\r\n\r\nfunction defined ( constant_name ) {\r\n\r\n\treturn ( ( window [ constant_name ] !== undefined ) ? true : false );\r\n\r\n}\r\n\r\n\r\n\r\n\/\/ -----\r\n\r\n\/\/ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\r\n\r\nconsole.log ( range ( 0, 12 ) );\r\n\r\n\r\n\r\n\/\/ [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]\r\n\r\nconsole.log ( range( 0, 100, 10 ) );\r\n\r\n\r\n\r\n\/\/ ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];\r\n\r\nconsole.log ( range( 'a', 'i' ) );\r\n\r\n\r\n\r\n\/\/ ['c', 'b', 'a'];\r\n\r\nconsole.log ( range ('c', 'a' ) );\r\n\r\n\r\n\r\nfunction range ( low, high, step ) {\r\n\r\n\tvar matrix = [];\r\n\r\n\tvar inival, endval, plus;\r\n\r\n\tvar walker = step || 1;\r\n\r\n\tvar chars = false;\r\n\r\n\t\r\n\r\n\tif ( !isNaN ( low ) && !isNaN ( high ) ) {\r\n\r\n\t\tinival = low;\r\n\r\n\t\tendval = high;\t\t\r\n\r\n\t} else if ( isNaN ( low ) && isNaN ( high ) ) {\r\n\r\n\t\tchars = true;\r\n\r\n\t\tinival = low.charCodeAt ( 0 );\r\n\r\n\t\tendval = high.charCodeAt ( 0 );\r\n\r\n\t} else {\r\n\r\n\t\tinival = ( isNaN ( low ) ? 0 : low );\r\n\r\n\t\tendval = ( isNaN ( high ) ? 0 : high );\r\n\r\n\t}\r\n\r\n\t\r\n\r\n\tplus = ( ( inival > endval ) ? false : true );\r\n\r\n\t\r\n\r\n\tif ( plus ) {\r\n\r\n\t\twhile ( inival <= endval ) {\r\n\r\n\t\t\tmatrix.push ( ( ( chars ) ? String.fromCharCode ( inival ) : inival ) );\r\n\r\n\t\t\tinival += walker;\r\n\r\n\t\t}\r\n\r\n\t} else {\r\n\r\n\t\twhile ( inival >= endval ) {\r\n\r\n\t\t\tmatrix.push ( ( ( chars ) ? String.fromCharCode ( inival ) : inival ) );\r\n\r\n\t\t\tinival -= walker;\r\n\r\n\t\t}\r\n\r\n\t}\r\n\r\n\t\t\r\n\r\n\treturn matrix;\r\n\r\n}\r\n\r\n\r\n\r\n\/\/ -----\r\n\r\nconsole.log ( strcmp ( 'waldo', 'Waldo' ) );\r\n\r\nconsole.log ( strcmp ( 'Waldo', 'waldo' ) );\r\n\r\nconsole.log ( strcmp ( 'waldo', 'waldo' ) );\r\n\r\n\r\n\r\nfunction strcmp ( str1, str2 ) {\r\n\r\n\tvar size1 = str1.charCodeAt ( 0 );\r\n\r\n\tvar size2 = str2.charCodeAt ( 0 );\r\n\r\n\t\r\n\r\n\treturn ( ( size1 == size2 ) ? 0 : ( ( size1 > size2 ) ? 1 : -1 ) );\r\n\r\n}\r\n[\/CODE]"},{"date":"2008-01-31 18:47:22","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ _argos: Nice work! Added!"},{"date":"2008-03-31 19:42:37","author":"Philip","url":"","comment":"Dunno where this belongs, but how about this code for echo()?\r\n\r\n[CODE="Javascript"]\r\nfunction echo()\r\n{\r\n for(i=0;i<echo.arguments.length;i++)\r\n {\r\n if(document.body && document.body.innerHTML) {\r\ndocument.body.innerHTML=document.body.innerHTML+echo.arguments[i];\r\n} else {\r\ndocument.write(echo.arguments[i]);\r\n}\r\n }\r\n}\r\n[\/CODE]"},{"date":"2008-04-02 13:26:06","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"Philip: Good idea, I'll add it, thanks!"},{"date":"2008-04-12 17:33:32","author":"Jonas Raoni","url":"","comment":"A shorter version of this would be:\r\n\r\nreturn Math.abs (n) || 0;"},{"date":"2008-04-12 20:24:25","author":"Philip","url":"","comment":"Hey, just fyi... Philip and Philip Peterson are both me ;-)"},{"date":"2008-04-13 12:29:58","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Jonas Raoni: Thanks I'll update the function.\r\n@ Philip: I'll update your name!"},{"date":"2008-09-08 11:40:18","author":"Reena","url":"nill","comment":"Good"},{"date":"2009-01-09 21:34:02","author":"Nile","url":"unlinkthis.net","comment":"json_encode\r\n[CODE="Javascript"]\r\nvar arr = new Array('Hello');\r\nvar applyEncode = function(varIn){\r\nreturn '{'+varIn+'}';\r\n}\r\nvar json_encode = function(value){\r\n var data = '';\r\n for(i=0,endCount='';i<value.length;++i){\r\n var endCount = (i!=value.length-1) ? ", " : "";\r\n data += '"'+key(value[i])+'" : "'+value[key(value)]+'"'+endCount;\r\n }\r\n return applyEncode(data);\r\n}\r\ndocument.write(json_encode(arr));\r\n[\/CODE]"},{"date":"2009-01-12 23:53:13","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Nile: Hey Nile thanks for your contribution. Unfortunately there are a couple of issues with it:\r\n- depends on an outside function: applyEncode\r\n- does not support associative arrays (in terms of traversing)\r\n- does not support index arrays, numbers, etc (in terms of encoding)\r\n\r\nPlease have a look here:\r\nhttp:\/\/www.json.org\/json2.js\r\n\r\nThe code is public domain, so maybe it's possible to modify it and use it in PHP.JS. We could look into that!"},{"date":"2009-01-15 02:28:06","author":"Paul","url":"","comment":"I downloaded php.namespaced.*.js at 2009-01-14 15:28 HST. There appears to be a space missing which is causing javascript errors. Search string "functionPropagation". I was able to resolve the error in php.namespaced.js and php.namespaced.min.js, but not php.namespaced.packed.js due to the packing operation.\r\n\r\nBTW: This package is incredible!"},{"date":"2009-01-15 11:40:52","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Paul: It was actually an issue in the exit function. Should be fixed now, thanks for letting us know!"},{"date":"2009-03-06 17:02:58","author":"Jay","url":"","comment":"You should really consider putting this project up on GitHub.com, it would be a perfect fit. That way people could modify and improve the code, and then send you a pull request for you to merge their changes. It's a very nice way to develop open source projects."},{"date":"2009-03-22 19:06:57","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Jay: I'm working on 1 github project and a.t.m. I do not find the speed satisfying. I imagine all that SSH traffic causes quite some load but git pull times of > 60 seconds are just not acceptable. Maybe later though! Thanks"},{"date":"2009-09-04 19:14:48","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Jay: \"Later\" has arrived. We're now on GitHub!\nhttp:\/\/github.com\/kvz\/phpjs\n\nSome more info about the change here:\nhttp:\/\/kevin.vanzonneveld.net\/techblog\/article\/svn_to_git\/"},{"date":"2012-04-10 09:49:50","author":"????? ???","url":"http:\/\/an3m1.com\/","comment":"I have a lot to benefit from this article and thank you for this wonderful effort to this article and will continue my many articles you have other "}],"addslashes":[{"date":"2008-01-21 18:35:41","author":"booeyOH","url":"","comment":"preg_quote() function for adding slashes to RegEx\r\n\r\nNot sure if it is out there, but needed something quick, hope its helpful\r\n[CODE="Javascript"]\r\nfunction preg_quote( str ) {\r\n\tvar quote_chars = ["\\\\", ".", "+", "*", "?", "[", "^", "]", "$", "(", ")", "{", "}", "=", "!", "<", ">", "|", ":"];\r\n\tvar return_val = str;\r\n\t\r\n\tfor(var i=0;i<quote_chars.length;i++)\r\n\t\t{\r\n\t\teval("var pattern = \/\\\\"+quote_chars[i]+"\/gi");\r\n\t\treturn_val = return_val.replace(pattern, chr(92)+quote_chars[i]);\r\n\t\t}\r\n\t\r\n\treturn return_val;\r\n}\r\n[\/CODE]"},{"date":"2008-01-22 07:40:04","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ booeyOH: It sure is! Thank you!"},{"date":"2008-01-23 18:14:03","author":"Ates Goral","url":"","comment":"First, just a nitpick:\r\n\r\nA set of characters can be used instead of the ORs:\r\n\r\n[CODE="Javascript"]\r\nreturn str.replace(\/(["'\\\\])\/g, "\\\\$1");\r\n[\/CODE]\r\n\r\nTo add support for NUL:\r\n\r\n[CODE="Javascript"]\r\nreturn str.replace(\/(["'\\\\])\/g, "\\\\$1").replace(\/\\0\/g, "\\\\0");\r\n[\/CODE]"},{"date":"2008-01-23 18:18:10","author":"Ates Goral","url":"","comment":"Additional test case:\r\n\r\n[CODE="Javascript"]\r\n \/\/ * example 2: addslashes("\\"'\\\\\\0");\r\n \/\/ * returns 2: "\\\\\\"\\\\\\'\\\\\\\\\\\\\\0"\r\n[\/CODE]"},{"date":"2008-01-23 20:24:09","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Ates Goral: Processed."},{"date":"2008-02-27 23:47:40","author":"Martin","url":"","comment":"example 1 (the only one) on this page is incorrect, in that it doesn't actually add the slash. hehe."},{"date":"2008-02-28 12:58:55","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Martin: Thanks for noticing. If you look at the source code, you see that the example is correct. But my blog probably filters out the backslash again. I'll look into it!"},{"date":"2008-03-01 17:02:17","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Martin: Fixed!"},{"date":"2008-04-12 17:48:16","author":"Jonas Raoni","url":"","comment":"It's missing the "\\" escape.\r\n\r\nreturn str.replace(\/(["'\\\\])\/g, "\\\\$1").replace(\/\\0\/g, "\\\\0")\t;\r\n\r\nOr\r\n\r\nreturn str.replace(\/(["'\\\\\\0])\/g, function(_, n){\r\n\treturn "\\\\" + (n == "\\0" ? "0" : n);\r\n});"},{"date":"2008-04-13 12:34:05","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Jonas Raoni: I believe your proposal has the same regex, only here it's singlequoted for compatbility with Dean Edwards packer."},{"date":"2008-05-24 01:03:58","author":"Sean Gallagher","url":"","comment":"Here is another quicky but good add slashes function!\r\n\r\nP.S. I could not get your function to work.\r\n\r\n[CODE="Javascript"]\r\nfunction addslashes(str)\r\n{\r\n \/\/ http:\/\/www.atlwebsite.com\r\n \/\/ By Sean Gallagher\r\n \/\/ Example: addslashes('what "ya\\'ll" doing?')\r\n \/\/ Returns: what \\"ya\\'ll\\" doing?\r\n str = str.replace(\/'\/g,"\\\\'");\r\n return str.replace(\/"\/g,'\\\\"');\r\n}\r\n[\/CODE]"},{"date":"2008-05-30 22:57:47","author":"Dudi","url":"","comment":"It doesn't seem like the function works. And the example is wrong again. ;-)"},{"date":"2008-05-31 14:29:19","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Dudi: That's just my blog messing up 'addslashes'. Fixed though."},{"date":"2008-07-22 07:41:52","author":"Nate","url":"","comment":"I couldn't get the function to work at first. I made some changes, and here is what I came up with:\r\n\r\n[CODE="Javascript"]\r\nreturn str.replace(\/([\\\\"'])\/g, "\\\\$1").replace(\/\\0\/g, "\\\\0");\r\n[\/CODE]\r\n\r\nAlso, the example should read,\r\n"kevin\\\\\\'s birthday" because the \\' becomes '. That is why it appears to work in the tester script."},{"date":"2008-08-08 11:49:10","author":"Onno Marsman","url":"","comment":"It's probably also a good idea to convert str to a string to make sure .replace exists. \r\naddslashes(6) does work in PHP but not in this function\r\n\r\n[CODE="Javascript"]\r\nreturn (str+'').replace(\/([\\\\"'])\/g, "\\\\$1").replace(\/\\0\/g, "\\\\0");\r\n[\/CODE]"},{"date":"2008-08-27 18:10:59","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Nate & Onno Marsman: Awesome job guys! I have been fooled by my tester for a long time. Addslashes has ben updated, and you have been credited accordingly. I've also changed the new testsuite to support addslashes behaviour."},{"date":"2008-09-21 15:37:57","author":"Julien Paquit","url":"","comment":"Very useful code ! Because of some scripting needs (and compatibility), I add this portion of code to the original one :\r\n\r\n[CODE="Javascript"]\r\nreturn (str+'').replace(\/(["])\/g, "&quot;").replace(\/([\\\\'])\/g, "\\\\$1").replace(\/\\0\/g, "\\\\0");\r\n[\/CODE]\r\n\r\nNow I am able to pass recursively parameters without errors."},{"date":"2008-09-21 22:01:07","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Julien Paquit: Thank you but I believe PHP does not do that automatically? If not, then we should not either, because we may surprise developers & cause unexpected output."},{"date":"2008-09-23 00:14:19","author":"Julien Paquit","url":"","comment":"Kevin : you are totally right. That was just a tip ;)"},{"date":"2009-06-30 08:40:10","author":"Denny Wardhana","url":"","comment":"Under \"Strict Warnings\" setup, \n[CODE]\n\/\\0\/g\n[\/CODE]\nproduces Warning: non-octal digit in an escape sequence that doesn't match a back-reference.\n\nHow to remove that warning (and the function still working of course)?\n"},{"date":"2009-06-30 11:45:20","author":"Brett Zamir","url":"http:\/\/brett-zamir.me","comment":"@Denny: Thanks for the report. I've fixed it in SVN. (use \\u0000 instead--\"\\u\" indicates a 4-digit hexadecimal Unicode sequence and \\0 was a shortcut for this)"},{"date":"2010-04-11 00:58:56","author":null,"url":"http:\/\/oskar-lh.name","comment":"Hi,\n[code]replace(\/[\\\\\"']\/g, \"\\\\$&\")[\/code]\nmight be faster than\n[code]replace(\/([\\\\\"'])\/g, \"\\\\$1\")[\/code]"},{"date":"2010-04-11 01:00:00","author":null,"url":"http:\/\/oskar-lh.name","comment":"Hi,\nreplace(\/[\\\\\"']\/g, \"\\\\$&\")\nmight be faster than\nreplace(\/([\\\\\"'])\/g, \"\\\\$1\")"},{"date":"2010-04-11 08:58:25","author":"Brett Zamir","url":"http:\/\/brett-zamir.me","comment":"@Oskar: Thanks, good point! Changed in git..."},{"date":"2010-12-25 03:05:13","author":"ball","url":"","comment":"thanks a lot!!!"},{"date":"2011-05-05 10:30:40","author":"Denis","url":"www.divasbydesign.co.za","comment":"very helpful :)"},{"date":"2011-05-05 10:31:29","author":"Denis","url":"http:\/\/www.divasbydesign.co.za","comment":"I found this very helpful in my coding :)"},{"date":"2011-07-28 11:00:22","author":"?","url":"","comment":"'\n"},{"date":"2012-03-30 14:29:59","author":"praveen","url":"ajkds@ff.com","comment":"adsdajdhadehaijdeiojni bondha"},{"date":"2012-03-30 14:31:30","author":"praveen","url":"ajkds@ff.com","comment":"oka mukka ardam kaledu ra yedava"},{"date":"2012-05-06 09:32:18","author":"????? ????????","url":"http:\/\/an3m1.com\/","comment":"Great job here. I really enjoyed what you had to say. Keep going because you definitely bring a new voice to this subject. Not many people would say what you\u2019ve said and still make it interesting \n\n"}],"array":[{"date":"2008-06-19 17:13:13","author":"thinsoldier","url":"","comment":"what about associative arrays?\r\nvar Divs = array(); \/\/ array of divs that I want to sort\r\nDivs['amelie'] = divHtmlElement3;\r\nDivs['randolph'] = divHtmlElement1;\r\nDivs['judy'] = divHtmlElement9;\r\n\r\nasort(Divs);\r\n\r\nshowDivArray(Divs);"},{"date":"2008-06-19 18:29:16","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ thinsoldier: What about them? Judging by your comment I would think you mean that our asort doesn't support them.. But it appears we don't have an asort yet ;)\r\nJavaScript does support them in general. But they're called objects to be strict.\r\nBut I guess I don't fully understand your question."},{"date":"2008-09-09 18:15:57","author":"covings","url":"http:\/\/www.cornicescentre.co.uk","comment":"VERY INTERESTING AND REALLY USEFUL INFORMATIONS! THX SO MUTCH"},{"date":"2008-10-05 10:08:55","author":"thinsoldier","url":"","comment":"what about\r\n\r\narray('first'=>'Kevin', 'last'=>'Zonnevelt')"},{"date":"2008-10-06 12:09:36","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ thinsoldier: Yes that would be very nice, but I don't see how we could implement that unfortunately :( Here we run against the hard wall of language differences."},{"date":"2008-11-29 20:59:46","author":"Sean","url":"","comment":"@Kevin: Could you not do:\r\n\r\n[CODE="Javascript"]\r\narray(" 'first' => 'kevin' ", " 'last' => 'Zonnevelt' " );\r\n[\/CODE]\r\n\r\nIt's not quite the same as php, but at least this way you could create hash arrays"},{"date":"2008-11-30 08:53:16","author":"Onno Marsman","url":"","comment":"@Sean: We already can create associative arrays with JS:\r\n[CODE="Javascript"]\r\n{\r\n 'first': 'kevin',\r\n 'last': 'Zonnevelt' \/\/I don't know why this is with a T ;)\r\n}\r\n[\/CODE]\r\nTechnically this is not an array but an object, but your proposal wouldn't return anything different. So we already have a syntax that is "not quite the same as php" and it does exactly the same.\r\n\r\nAlso think of using variables:\r\n[CODE="Javascript"]\r\n{ a: b, c: d}\r\n[\/CODE]\r\nYour alternative would result in something like this, because proper escaping is needed:\r\n[CODE="Javascript"]\r\narray(" '"+addslashes(a)+"' => '"+addslashes(b)+"' ", " '"+addslashes(c)+"' => '"+addslashes(d)+"' " );\r\n[\/CODE]\r\nI hope I didn't make any typo's, but I think my point is clear."},{"date":"2008-12-01 09:31:43","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Sean & Onno Marsman: I totally agree with Onno. If we can't get it right, and people have to learn a different notation anyway, let's just stick with JavaScript."},{"date":"2008-12-05 06:19:53","author":"Ates Goral","url":"http:\/\/magnetiq.com","comment":"function each(arr) {\r\n \/\/ Return the current key and value pair from an array and advance the array cursor\r\n\r\n \/\/ + original by: Ates Goral (http:\/\/magnetiq.com)\r\n \/\/ * example 1: each([42,43]);\r\n \/\/ * returns 1: {0: 0, 1: 42, key: 0, value: 42}\r\n \/\/ * example 2: each({a:"apple",b:"balloon"});\r\n \/\/ * returns 2: {0:"a",1:"apple",key:"a",value:"apple"}\r\n if (!(arr instanceof Object) || (arr._keys && !arr._keys.length)) {\r\n return false;\r\n }\r\n\r\n if (!arr._keys) {\r\n arr._keys = [];\r\n \r\n for (var k in arr) {\r\n if (k != "_keys") {\r\n arr._keys.push(k);\r\n }\r\n }\r\n }\r\n \r\n var k = arr._keys.shift();\r\n var v = arr[k];\r\n \r\n return {\r\n 0: k,\r\n 1: v,\r\n key: k,\r\n value: v\r\n };\r\n}"},{"date":"2008-12-05 06:22:48","author":"Ates Goral","url":"http:\/\/magnetiq.com","comment":"I apologize for the earlier, unformatted post :)\r\n\r\n[CODE="Javascript"]\r\nfunction each(arr) {\r\n \/\/ Return the current key and value pair from an array and advance the array cursor\r\n\r\n \/\/ + original by: Ates Goral (http:\/\/magnetiq.com)\r\n \/\/ * example 1: each([42,43]);\r\n \/\/ * returns 1: {0: 0, 1: 42, key: 0, value: 42}\r\n \/\/ * example 2: each({a:"apple",b:"balloon"});\r\n \/\/ * returns 2: {0:"a",1:"apple",key:"a",value:"apple"}\r\n if (!(arr instanceof Object) || (arr._keys && !arr._keys.length)) {\r\n return false;\r\n }\r\n\r\n if (!arr._keys) {\r\n arr._keys = [];\r\n \r\n for (var k in arr) {\r\n if (k != "_keys") {\r\n arr._keys.push(k);\r\n }\r\n }\r\n }\r\n \r\n var k = arr._keys.shift();\r\n var v = arr[k];\r\n \r\n return {\r\n 0: k,\r\n 1: v,\r\n key: k,\r\n value: v\r\n };\r\n}\r\n[\/CODE]"},{"date":"2008-12-05 06:27:32","author":"Ates Goral","url":"http:\/\/magnetiq.com","comment":"Perhaps the each() implementation I have below is a hasty one. It won't play nice with reset(), next() and previous(). It can be improved by actually storing the cursor position instead of consuming the key array with the shift(). I'll try to improve it and provide reset\/next\/prev if I find the time."},{"date":"2008-12-10 17:22:44","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Ates Goral: Wow thanks a lot Ates! We'll leave it open for improvement then! Though I don't really like the idea of global variables, I believe the include_once functions also already work like that so we may be able to do that here as well. What do you think?"},{"date":"2010-09-05 16:56:38","author":"baterie s?oneczne","url":"http:\/\/www.actionenergy.pl","comment":"Wow! it seems that someone did really great stuff here! Lovely!"},{"date":"2011-08-27 21:18:20","author":"aaa","url":"","comment":"ewewe"},{"date":"2012-04-17 15:33:14","author":"????? ????","url":"http:\/\/an3m1.com\/","comment":"If I might \u2014perhaps you should consider adding a few images. I don\u2019t mean to disrespect what you\u2019ve said ; its very enlightening, indeed. However, I think would respond to it more positively if they could be something tangible to your ideas \n"}],"array_change_key_case":[{"date":"2008-05-17 16:12:15","author":"d3x","url":"","comment":"Javascript equivalent for the PHP array():\r\n\r\n[CODE="Javascript"]\r\nfunction array(){\r\nreturn Array.prototype.slice.call(arguments);\r\n}\r\n[\/CODE]"},{"date":"2008-05-17 16:41:00","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ d3x: Awesome!!!! Added."},{"date":"2009-01-08 16:44:09","author":"Scot Diddle","url":"www.webtdo.com","comment":"Hi,\r\n\r\nI am building a PHP\/JS page to call, process, and display the results of eache php.js function.\r\n\r\nI'm not sure why I am getting back '[object object]' for array_change_key_case();\r\n\r\n[CODE=\"php\"]\r\n\t\t\r\n\t\/**\r\n\t * \r\n\t * array_change_key_case()\r\n\t * \r\n\t *\/\r\n\t\r\n\t $integer = 42;\r\n\t \r\n\t $simpleArray = '[ 3, 5 ]';\r\n\t \r\n\t $associativeArrray = \"{ FuBaR: 42, Dry: 'Do not repeat repeat yourself' } \";\r\n\t \r\n\t $associativeArrrayForDisplay = \"{ FuBaR: 42, Dry: \\'Do not repeat repeat yourself\\' } \";\r\n[\/CODE]\r\n\r\n [CODE=\"javascript\"]\r\n\r\n\t\tvar answer = confirm(\"Show: array_change_key_case() ?\");\r\n\r\n\t\tif (answer) {\r\n\t\t\t\r\n\t\t\tvar array_change_key_case_query = array_change_key_case(<?php echo $integer ?>); \r\n\t\t\talert('array(<?php echo $integer; ?>) : ' + array_change_key_case_query);\r\n\t\t\t\r\n\t\t\tvar array_change_key_case_query = array_change_key_case(<?php echo $simpleArray ?>); \r\n\t\t\talert('array(<?php echo $simpleArray; ?>) : ' + array_change_key_case_query);\r\n\t\t\t\t\t\t\r\n\t\t\tvar array_change_key_case_query = array_change_key_case(<?php echo $associativeArrray ?>); \r\n\t\t\t\r\n\t\t\tfor (a in array_change_key_case_query) {\r\n\t\t\t\r\n\t\t\t\talert('array(<?php echo $associativeArrrayForDisplay; ?>) : ' + a);\r\n\t\t\t\t\r\n\t\t\t}\r\n\r\n\t\t}\r\n\r\n[\/CODE]\r\n\r\nThe \"For (a in Oject) returns the Index in lower case, but where did the value(s) associated with the new lowercase index go... How do you use the output from this function. ?\r\n\r\nThanks, Scot L. Diddle"},{"date":"2009-01-08 17:11:02","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Scot Diddle: Our testsuite does not produce any unexpected results with the example-based test-cases. Currently it is not clear to me what statement exactly fails.\r\n\r\nIf the question is just how to get a value from an object-element:\r\n[CODE="Javascript"]\r\nfor (a in array_change_key_case_query) {\r\n \/\/ a is the key\r\n val = array_change_key_case_query[a];\r\n \/\/ val is now the value of the element with key: a\r\n}\r\n[\/CODE]"},{"date":"2009-01-08 17:16:13","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Scot Diddle: PS, may I ask to what purpose you are building this page? If it is for testing purposes you may want to look into our testsuite which runs from commandline (mac & linux)."}],"array_chunk":[{"date":"2008-04-12 17:53:36","author":"Jonas Raoni","url":"","comment":"Put a link on the name of Carlos R. L. Rodrigues to our site at http:\/\/jsfromhell.com ^^"},{"date":"2008-04-13 13:02:22","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Jonas Raoni: Done!"},{"date":"2009-01-13 12:42:07","author":"Douwe","url":"","comment":"You use constant_name as a function parameter, but in the function you use constant..."}],"array_combine":[{"date":"2012-05-16 07:42:38","author":"Fred P","url":"","comment":"You can fix line #21 permanently like this:\n\n[CODE]\n var new_array = {},\n keycount = (keys && keys.length) || 0,\n i = 0;\n\nthen this will always work properly, \nsince keycount does not have to be falsy 0, but REALLY zero.\n\n if (keycount !== values.length) {\n return false;\n }\n\n[\/CODE]\n"}],"array_count_values":[{"date":"2008-05-06 13:38:09","author":"vikas","url":"","comment":"it very nice"},{"date":"2008-08-25 09:42:32","author":"sankai","url":"","comment":"It can't work well in IE!\r\nThe debug infomation is "Object don't support the attribute or method" in the code as\r\n[CODE="Javascript"]\r\n if (array instanceof Array) {\r\n array.forEach(countValue, tmp_ar);\r\n } else if (array instanceof Object) {\r\n for ( key in array ) {\r\n countValue.call(tmp_ar, array[key]);\r\n }\r\n }\r\n[\/CODE]\r\n\r\n\u00e5\u203a\u00a7rz..but,It's working very well in Firefox!!"},{"date":"2008-08-25 11:59:27","author":"sankai","url":"","comment":"It seems because IE broswer don't support the mothod array.forEach().I search some solution from the google web.\r\n\r\nhttp:\/\/developer.mozilla.org\/En\/Core_JavaScript_1.5_Reference:Global_Objects:Array:forEach\r\n\r\nI try add the code before array_count_values()\r\n[CODE="Javascript"]\r\nif (!Array.prototype.forEach)\r\n{\r\n Array.prototype.forEach = function(fun \/*, thisp*\/)\r\n {\r\n var len = this.length;\r\n if (typeof fun != "function")\r\n throw new TypeError();\r\n\r\n var thisp = arguments[1];\r\n for (var i = 0; i < len; i++)\r\n {\r\n if (i in this)\r\n fun.call(thisp, this[i], i, this);\r\n }\r\n };\r\n}\r\n[\/CODE]\r\n\r\nlol...It still can't work!!!\r\n\r\nOrz..do you have any idea,sir?"},{"date":"2008-08-27 19:26:37","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ sankai: Thanks for pointing that out sankai! I've replaced the mozilla-only: forEach with a regular for loop. It should work fine now!"},{"date":"2009-03-25 01:39:57","author":"J-R","url":"none","comment":"Hi, \nI 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. \n[code]\n<script type='text\/javascript'>\n\nfunction array_count_values( array ) {\n \/\/ http:\/\/kevin.vanzonneveld.net\n \/\/ + original by: Ates Goral (http:\/\/magnetiq.com)\n \/\/ + namespaced by: Michael White (http:\/\/getsprink.com)\n \/\/ + input by: sankai\n \/\/ + improved by: Kevin van Zonneveld (http:\/\/kevin.vanzonneveld.net)\n \/\/ * example 1: array_count_values([ 3, 5, 3, \"foo\", \"bar\", \"foo\" ]);\n \/\/ * returns 1: {3:2, 5:1, \"foo\":2, \"bar\":1}\n \/\/ * example 2: array_count_values({ p1: 3, p2: 5, p3: 3, p4: \"foo\", p5: \"bar\", p6: \"foo\" });\n \/\/ * returns 2: {3:2, 5:1, \"foo\":2, \"bar\":1}\n \/\/ * example 3: array_count_values([ true, 4.2, 42, \"fubar\" ]);\n \/\/ * returns 3: {42:1, \"fubar\":1}\n \n var tmp_arr = {}, key = '', t = '';\n \n var __getType = function(obj) {\n \/\/ Objects are php associative arrays.\n var t = typeof obj;\n t = t.toLowerCase();\n if (t == \"object\") {\n t = \"array\";\n }\n return t;\n } \n \n var __countValue = function (value) {\n switch (typeof(value)) {\n case \"number\":\n if (Math.floor(value) != value) {\n return;\n }\n case \"string\":\n if (value in this) {\n ++this[value];\n } else {\n this[value] = 1;\n }\n }\n };\n \n t = __getType(array);\n if (t == 'array') {\n for ( key in array ) {\n __countValue.call(tmp_arr, array[key]);\n }\n } \n return tmp_arr;\n}\n\n\nfunction formValidator(){\n\tvar tmpArray= [];\n\ttmpArray = array_count_values([ 3, 5, 3, \"foo\", \"bar\", \"foo\" ]);\n\tdocument.write(tmpArray[0] + tmpArray[1]+ tmpArray[2]);\n\tdocument.write(tmpArray[3] + tmpArray[4]+ tmpArray[5]);\n\/\/\tdocument.write(tmpArray[0] + \" \" + tmpArray[1]+ \" \" + tmpArray[2] + \" \" + tmpArray[3]+ \" \" + tmpArray[4]+ \" \" + tmpArray[5]);\n}\n\n<\/script>\n\n<form id=\"frmchoixronde1\" name=\"frmchoixronde1\" method=\"POST\" \nenctype=\"application\/x-www-form-urlencoded\" onsubmit='return formValidator()'>\n<html><head><title>Page des poolers<\/title><\/head> <body>\n<b>Entrez vos choix pour la premiere ronde<\/b>\n<\/br><fieldset>\n\n<b><label for=\"player_1\" style=\"width:2em\">1<\/label><\/b><input name=\"player_1\" id=\"player_1\" type=\"text\" size=\"30\"><\/br>\n<b><label for=\"player_2\" style=\"width:2em\">2<\/label><\/b><input name=\"player_2\" id=\"player_2\" type=\"text\" size=\"30\"><\/br>\n<b><label for=\"player_3\" style=\"width:2em\">3<\/label><\/b><input name=\"player_3\" id=\"player_3\" type=\"text\" size=\"30\"><\/br>\n\n<b><label for=\"player_4\" style=\"width:2em\">4<\/label><\/b><input name=\"player_4\" id=\"player_4\" type=\"text\" size=\"30\"><\/br>\n<b><label for=\"player_5\" style=\"width:2em\">5<\/label><\/b><input name=\"player_5\" id=\"player_5\" type=\"text\" size=\"30\"><\/br>\n<b><label for=\"player_6\" style=\"width:2em\">6<\/label><\/b><input name=\"player_6\" id=\"player_6\" type=\"text\" size=\"30\"><\/br>\n<input type=\"submit\" name=\"submit\" value=\"Soumettre vos choix\">\n\n<\/form>\n<\/br> \n<\/body><\/html> \n\n[\/code]"},{"date":"2009-04-03 12:36:30","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ J-R: I'm running the test code with no problems:\n$ .\/phpjstest.php array_count_values\narray\/array_count_values.js returns#1 OKAY \n returns#2 OKAY \n returns#3 OKAY \n\nCould 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"},{"date":"2010-04-21 17:29:43","author":"Shingo","url":"","comment":"Thanks but there is one problem. If my test array is like\n[code]\n arr=[\"constructor\"];\n[code]\nIt does not work."},{"date":"2010-04-22 18:54:39","author":"Brett Zamir","url":"http:\/\/brett-zamir.me","comment":"@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"}],"array_diff":[{"date":"2008-01-22 05:37:46","author":"Ates Goral","url":"","comment":"Here's array_diff_key():\r\n\r\n[CODE="Javascript"]\r\nfunction array_diff_key(object) {\r\n \/\/ * example 1: array_diff_key({red: 1, green: 2, blue: 3, white: 4});\r\n \/\/ * returns 1: {"red":1, "green":2, "blue":3, "white":4}\r\n \/\/ * example 2: array_diff_key({red: 1, green: 2, blue: 3, white: 4}, {red: 5});\r\n \/\/ * returns 2: {"green":2, "blue":3, "white":4}\r\n \/\/ * example 3: array_diff_key({red: 1, green: 2, blue: 3, white: 4}, {red: 5}, {green: 6, blue: 7});\r\n \/\/ * returns 3: {"white":4}\r\n \/\/ * example 4: array_diff_key({red: 1, green: 2, blue: 3, white: 4}, {red: 5}, {red: 5});\r\n \/\/ * returns 4: {"green":2, "blue":3, "white":4}\r\n \r\n var ret = new Object();\r\n \r\n for (var key in object) {\r\n ret[key] = object[key];\r\n }\r\n \r\n for (var argidx = 1; argidx < arguments.length; ++argidx) {\r\n var other = arguments[argidx];\r\n \r\n if (other instanceof Object) {\r\n for (var key in other) {\r\n delete ret[key]; \r\n }\r\n }\r\n }\r\n \r\n return ret;\r\n}\r\n[\/CODE]"},{"date":"2008-01-22 09:00:40","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ 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!"},{"date":"2008-03-13 03:50:23","author":"Re-Ordering the Index","url":"","comment":"\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/*\r\nSanjoy Roy's comments:\r\n------------------------------- \r\nI have added a counter variable (cntr) and return array returns ordered array.\r\nThis will help to get rid of 'undefined' values in the list when we use them in our program.\r\n*\/\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\n[CODE="Javascript"]\r\nfunction array_diff (array) {\r\n \/\/ http:\/\/kevin.vanzonneveld.net\r\n \/\/ + original by: Kevin van Zonneveld (http:\/\/kevin.vanzonneveld.net)\r\n \/\/ * example 1: array_diff(['Kevin', 'van', 'Zonneveld'], ['van', 'Zonneveld']);\r\n \/\/ * returns 1: ['Kevin']\r\n \r\n var arr_dif = [], i = 1, argc = arguments.length, argv = arguments, key, key_c, found=false, cntr=0;\r\n \r\n \/\/ loop through 1st array\r\n for ( key in array ){\r\n \/\/ loop over other arrays\r\n for (i = 1; i< argc; i++){\r\n \/\/ find in the compare array\r\n found = false;\r\n for (key_c in argv[i]) {\r\n if (argv[i][key_c] == array[key]) {\r\n found = true;\r\n break;\r\n }\r\n }\r\n \r\n if(!found){\r\n \/\/arr_dif[key] = array[key];\r\n arr_dif[cntr] = array[key];\r\n cntr++;\r\n }\r\n }\r\n }\r\n \r\n return arr_dif;\r\n};\r\n[\/CODE]"},{"date":"2008-03-13 15:51:53","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Sanjoy Roy: Thank you, added!"},{"date":"2009-10-08 10:35:49","author":"Itsacon","url":"http:\/\/www.itsacon.net\/","comment":"There's a compatibility problem with this function and functions that take Array inputs (and check for it), like implode().\r\nThis is caused by this line:\r\n[code]var arr1 = arguments[0], retArr = {};[\/code]\r\nThe retArr variable gets instantiated as a generic Object, not an Array. Because of this, the return value for this function is not an Array.\r\n\r\nI changed this line to \r\n[code]var arr1 = arguments[0], retArr = new Array;[\/code]\r\nThis solved my problems.\r\n\r\n(Note that this also applies to several other array_ functions that (should) return Arrays)"},{"date":"2009-10-09 05:06:36","author":"Brett Zamir","url":"http:\/\/brett-zamir.me","comment":"@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.: \"[undefined, 'blue']\" or lose the key=1 information, i.e., \"['blue']\".\n\nI 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.\n\nIf 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 \"undefined\" 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:\n\n[CODE]ini_set('phpjs.objectsAsArrays', 0);[\/CODE]\n\n...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).\n\n@Kevin: If you ever decide to add a FAQ, I think this should be the #1 Question! :)"},{"date":"2009-10-09 12:14:24","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Brett Zamir: We could probably start one in the wiki.\nWhat do you think about hosting the wiki here:\nhttp:\/\/wiki.github.com\/kvz\/phpjs ?\n\nthis way i wouldn't have to worry about updating media wiki, accountmanagement, and backups ?"},{"date":"2009-10-09 12:47:34","author":"Itsacon","url":"http:\/\/www.itsacon.net\/","comment":"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). \r\nIf you choose to use generic Objects for arrays throughout, the implode() function should reflect this as well.\r\n\r\nI'll append a comment there."},{"date":"2009-10-18 18:25:42","author":"Brett Zamir","url":"http:\/\/brett-zamir.me","comment":"@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."},{"date":"2009-10-18 18:29:08","author":"Brett Zamir","url":"http:\/\/brett-zamir.me","comment":"@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)"},{"date":"2009-10-25 13:24:39","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ 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 & think of.\nI'll add a link to the github wiki and we'll see how it goes OK?"},{"date":"2009-10-25 13:25:53","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Brett Zamir: The comments, no I'm not having those issues. Could you by any chance supply the errors?\n\nbtw I removed the social javascript. maybe that fixes some issues as well."},{"date":"2009-10-26 00:50:33","author":"Brett Zamir","url":"http:\/\/brett-zamir.me","comment":"It seems the comments problem is fixed now. And, sure, go ahead and add a link to the wiki; that'd be great..."},{"date":"2009-11-07 18:29:31","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"Link is on it's way!"},{"date":"2009-11-08 03:49:09","author":"Brett Zamir","url":"http:\/\/brett-zamir.me","comment":"@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 \"FAQ\/wiki\" 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.?"},{"date":"2009-11-08 03:50:02","author":"Brett Zamir","url":"http:\/\/brett-zamir.me","comment":"@Kevin: also, btw, thanks for adding the link!"},{"date":"2009-11-08 16:40:26","author":"Kevin van Zonneveld","url":"http:\/\/kevin.vanzonneveld.net","comment":"@ Brett Zamir: Yes we have a FAQ! : ) Let's just take a moment to enjoy that fact :D\n\nOk, good.\n\nI've changed the navigation a bit according to your ideas.\n\nAs 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.\n\nAnd 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."},{"date":"2010-11-16 02:33:08","author":"CoR","url":"","comment":"You have pretty nasty bug in code. JavaScript doesn't have real array. Length isn't real number of array items!\n .length = lastIndex + 1;\n\nConsider this:\n[code]\nvar ar = [0,1,2];\nconsole.log(ar.length);\t\/\/ 3, and that's ok\nar[900] = 3;\nconsole.log(ar.length);\t\/\/ 901 and that's horribly wrong!\n[\/code]\nWith 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...\n\n\nFix:\nvar retArr = {}; \/\/ not good\nvar retArr = []; \/\/ good\n\nretArr[k1] = arr1[k1]; \/\/ bug\nretArr.push(arr1[k1]);\n\n\n"},{"date":"2010-11-16 04:39:09","author":"CoR","url":"","comment":"[code]\nvar ar1 =[0,1,2,3,4];\nvar ar2 =[0,1,2];\nvar a = array_diff(ar1,ar2);\nconsole.log(a.length);\t\/\/ 5\n[\/code]\n\n\np.s. a.length will fail because of var retArr = {};\nAfter [] fix it will produce seemingly false .length of 5.\n\na === [undefined, undefined, undefined,3,4];"},{"date":"2010-11-17 12:23:11","author":"Brett Zamir","url":"http:\/\/brett-zamir.me","comment":"@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)."},{"date":"2010-11-18 00:19:52","author":"CoR","url":"","comment":"@Brett Zamir:\nYes, I see what you mean... I was too fast to point array problem without reading faq. My bad ;)\n\nAnyway, in my case array_diff should work only with JS arrays and it MUST return fully functional JS array.\n[] and .push() works great :)\n\nAnother '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...\n\nIt's nice that this site exist. You have tons of useful code!"},{"date":"2011-06-13 12:14:55","author":"George Wilde","url":"","comment":"Hey,\nThanks for the code it worked perfectly as described :)\n\nI 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.\n\n[CODE]\nfunction array_diff (arr1) {\n \/\/ http:\/\/kevin.vanzonneveld.net\n \/\/ + original by: Kevin van Zonneveld (http:\/\/kevin.vanzonneveld.net)\n \/\/ + improved by: Sanjoy Roy\n \/\/ + revised by: Brett Zamir (http:\/\/brett-zamir.me)\n \/\/ * example 1: array_diff(['Kevin', 'van', 'Zonneveld'], ['van', 'Zonneveld']);\n \/\/ * returns 1: [0 => 'Kevin']\n var retArr = [],\n argl = arguments.length,\n k1 = '',\n i = 1,\n k = '',\n arr = {};\n\n arr1keys: for (k1 in arr1) {\n for (i = 1; i < argl; i++) {\n arr = arguments[i];\n for (k in arr) {\n if (arr[k] === arr1[k1]) {\n \/\/ If it reaches here, it was found in at least one array, so try next value\n continue arr1keys;\n }\n }\n retArr.push(arr1[k1]);\n }\n }\n\n return retArr;\n}\n[\/CODE]"},{"date":"2011-06-13 19:23:58","author":"Brett Zamir","url":"http:\/\/brett-zamir.me","comment":"@George Wilde: Great, thanks for sharing!"},{"date":"2012-01-25 16:19:26","author":"Vasil Vasilev","url":"vaseto.eu","comment":"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. \n[CODE]\n var array_diff = function(arr1) {\n var retArr = [],\n arr1length, i, j, z, keyFound,\n argl = arguments.length, arr = [];\n pArr:\n for (i = 0, arr1length = arr1.length; i < arr1length; i++) {\n keyFound = false;\n for (j = 1; j < argl; j++) {\n arr = arguments[j];\n for (var z = 0, arrLength = arr.length; z < arrLength; z++) {\n if (arr[z] === arr1[i]) {\n keyFound = true;\n continue pArr;\n }\n }\n if (keyFound) {\n continue;\n }\n }\n if ( ! keyFound) {\n retArr.push(arr1[i]);\n }\n }\n return retArr;\n };\n[\/CODE]"},{"date":"2012-04-23 14:48:15","author":" ????? ????? ????","url":"http:\/\/an3m1.com\/","comment":"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\n \n"},{"date":"2012-05-15 23:55:04","author":"vinyl ","url":"http:\/\/alsidewindowsz.com","comment":"I love using syntax its such a great system. I love learning new code it