UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

97 lines (80 loc) 3.3 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://an3m1.com/" rel="nofollow">??????? ????? ???</a> </strong> on 2012-04-17 15:28:17 <br /> They have done such a great job with this. This is why they are deserving of these awards. Keep up the good work. <hr /> <strong> Brett Zamir </strong> on 2008-12-03 17:53:53 <br /> Again, adapting the PHP manual's examples, here is an equivalent for extract(): <pre><code>var size = &amp;quot;large&amp;quot;; var var_array = {&amp;quot;color&amp;quot; : &amp;quot;blue&amp;quot;, &amp;quot;size&amp;quot; : &amp;quot;medium&amp;quot;, &amp;quot;shape&amp;quot; : &amp;quot;sphere&amp;quot;}; extract(var_array, 'EXTR_PREFIX_SAME', &amp;quot;wddx&amp;quot;); alert([color, size, shape, wddx_size].join()); // blue,large,sphere,medium</code></pre> <pre><code>// Only works on global variables function extract (arr, type, prefix) { for (var i in arr) { switch (type) { case 'EXTR_PREFIX_SAME' || 2: if (window[i] !== undefined) { window[prefix+'_'+i] = arr[i]; } // Fall-through case 'EXTR_SKIP' || 1: if (window[i] === undefined) { window[i] = arr[i]; } break; case 'EXTR_PREFIX_ALL' || 3: window[prefix+'_'+i] = arr[i]; break; case 'EXTR_PREFIX_INVALID' || 4: if(i.match(/^[_a-zA-Z$][\w|$]*$/) != null) { // Refine regexp to allow JS 1.5+ Unicode identifiers window[i] = arr[i]; } else { window[prefix+'_'+i] = arr[i]; } break; case 'EXTR_IF_EXISTS' || 6: if (window[i] !== undefined) { window[i] = arr[i]; } break; case 'EXTR_PREFIX_IF_EXISTS' || 5: if (window[i] !== undefined) { window[prefix+'_'+i] = arr[i]; } break; case 'EXTR_REFS' || 256: throw 'The EXTR_REFS type will not work in JavaScript'; break; case 'EXTR_OVERWRITE' || 0: // Fall-through default: window[i] = arr[i]; break; } } }</code></pre> <hr /> <strong> Karol Kowalski </strong> on 2008-02-06 15:04:39 <br /> This version is not recursive as it should be acc to the php manual. Try run following example (taken from the manual, too): <pre><code> var city = &amp;quot;San Francisco&amp;quot;; var state = &amp;quot;CA&amp;quot;; var event = &amp;quot;SIGGRAPH&amp;quot;; var location_vars = new Array(&amp;quot;city&amp;quot;, &amp;quot;state&amp;quot;); var result = compact(&amp;quot;event&amp;quot;, &amp;quot;nothing_here&amp;quot;, &amp;quot;location_vars&amp;quot;); console.log(result) </code></pre> What you get is an object with event property and location_vars property which is an Array. What should be returned is a 'flat' object with event, city and state properties (because location_vars) should be searched inside. <hr />