This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
php.js
6010 lines (5243 loc) · 205 KB
/
php.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* More info at: http://kevin.vanzonneveld.net/techblog/article/phpjs_licensing/
*
* This is version: 1.76
* php.js is copyright 2008 Kevin van Zonneveld.
*
* Portions copyright Onno Marsman, Michael White (http://getsprink.com),
* _argos, Jack, Jonas Raoni Soares Silva (http://www.jsfromhell.com), Philip
* Peterson, Legaev Andrey, Ates Goral (http://magnetiq.com), Martijn
* Wieringa, Philippe Baumann, Webtoolkit.info (http://www.webtoolkit.info/),
* Carlos R. L. Rodrigues (http://www.jsfromhell.com), Enrique Gonzalez, Ash
* Searle (http://hexmen.com/blog/), Erkekjetter, GeekFG
* (http://geekfg.blogspot.com), Johnny Mast (http://www.phpvrouwen.nl), d3x,
* marrtins, AJ, Alex, Alfonso Jimenez (http://www.alfonsojimenez.com), Aman
* Gupta, Arpad Ray (mailto:[email protected]), Karol Kowalski, Mirek Slugen,
* Nate, Sakimori, Thunder.m, Tyler Akins (http://rumkin.com), mdsjack
* (http://www.mdsjack.bo.it), Alexander Ermolaev
* (http://snippets.dzone.com/user/AlexanderErmolaev), Allan Jensen
* (http://www.winternet.no), Andrea Giammarchi
* (http://webreflection.blogspot.com), Anton Ongson, Arno, Atli Þór, Bayron
* Guevara, Ben Bryan, Benjamin Lupton, Brad Touesnard, Brett Zamir, Cagri
* Ekin, Christian Doebler, Cord, David, David James, Dino, DxGx, FGFEmperor,
* Felix Geisendoerfer (http://www.debuggable.com/felix), Francois,
* FremyCompany, Gabriel Paderni, Howard Yeend, J A R, Kirk Strobeck, LH,
* Leslie Hoare, Lincoln Ramsay, Luke Godfrey, Mateusz "loonquawl" Zalega,
* MeEtc (http://yass.meetcweb.com), Mick@el, Nathan, Nick Callen, Norman
* "zEh" Fuchs, Ozh, Pedro Tainha (http://www.pedrotainha.com), Peter-Paul
* Koch (http://www.quirksmode.org/js/beat.html), Pul, Pyerre, ReverseSyntax,
* Robin, Sanjoy Roy, Saulo Vallory, Scott Cariss, Simon Willison
* (http://simonwillison.net), Slawomir Kaniecki, Steve Clay, Steve Hilder,
* Steven Levithan (http://blog.stevenlevithan.com), T.Wild, T0bsn, Thiago
* Mata (http://thiagomata.blog.com), Tim Wiel, XoraX (http://www.xorax.info),
* Yannoo, baris ozdil, booeyOH, djmix, dptr1988, duncan, echo is bad, gabriel
* paderni, ger, gorthaur, hitwork, jakes, john (http://www.jd-tech.net),
* johnrembo, kenneth, marc andreu, metjay, nobbler, penutbutterjelly, sankai,
* sowberry, stensi
*
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL KEVIN VAN ZONNEVELD BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
// {{{ array
function array( ) {
// #!#!#!#!# array::$descr1 does not contain valid 'array' at line 260
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array/
// + version: 809.522
// + original by: d3x
// * example 1: array('Kevin', 'van', 'Zonneveld');
// * returns 1: ['Kevin', 'van', 'Zonneveld']
return Array.prototype.slice.call(arguments);
}// }}}
// {{{ array_change_key_case
function array_change_key_case( array ) {
// Changes all keys in an array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_change_key_case/
// + version: 809.522
// + original by: Ates Goral (http://magnetiq.com)
// + improved by: marrtins
// * example 1: array_change_key_case(42);
// * returns 1: false
// * example 2: array_change_key_case([ 3, 5 ]);
// * returns 2: {0: 3, 1: 5}
// * example 3: array_change_key_case({ FuBaR: 42 });
// * returns 3: {"fubar": 42}
// * example 4: array_change_key_case({ FuBaR: 42 }, 'CASE_LOWER');
// * returns 4: {"fubar": 42}
// * example 5: array_change_key_case({ FuBaR: 42 }, 'CASE_UPPER');
// * returns 5: {"FUBAR": 42}
// * example 6: array_change_key_case({ FuBaR: 42 }, 2);
// * returns 6: {"FUBAR": 42}
var case_fn, tmp_ar = new Object, argc = arguments.length, argv = arguments, key;
if (array instanceof Array) {
return array;
}
if (array instanceof Object) {
if( argc == 1 || argv[1] == 'CASE_LOWER' || argv[1] == 0 ){
case_fn = "toLowerCase";
} else{
case_fn = "toUpperCase";
}
for (var key in array) {
tmp_ar[key[case_fn]()] = array[key];
}
return tmp_ar;
}
return false;
}// }}}
// {{{ array_chunk
function array_chunk( input, size ) {
// Split an array into chunks
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_chunk/
// + version: 809.522
// + original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
// * example 1: array_chunk(['Kevin', 'van', 'Zonneveld'], 2);
// * returns 1: {0 : {0: 'Kevin', 1: 'van'} , 1 : {0: 'Zonneveld'}}
for(var x, i = 0, c = -1, l = input.length, n = []; i < l; i++){
(x = i % size) ? n[c][x] = input[i] : n[++c] = [input[i]];
}
return n;
}// }}}
// {{{ array_combine
function array_combine( keys, values ) {
// Creates an array by using one array for keys and another for its values
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_combine/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: array_combine([0,1,2], ['kevin','van','zonneveld']);
// * returns 1: {0: 'kevin', 1: 'van', 2: 'zonneveld'}
var new_array = {}, keycount=keys.length, i;
// input sanitation
if( !keys || !values || keys.constructor !== Array || values.constructor !== Array ){
return false;
}
// number of elements does not match
if(keycount != values.length){
return false;
}
for ( i=0; i < keycount; i++ ){
new_array[keys[i]] = values[i];
}
return new_array;
}// }}}
// {{{ array_count_values
function array_count_values( array ) {
// Counts all the values of an array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_count_values/
// + version: 810.2018
// + 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, "foo", "bar", "foo" ]);
// * returns 1: {3:2, 5:1, "foo":2, "bar":1}
// * example 2: array_count_values({ p1: 3, p2: 5, p3: 3, p4: "foo", p5: "bar", p6: "foo" });
// * returns 2: {3:2, 5:1, "foo":2, "bar":1}
// * example 3: array_count_values([ true, 4.2, 42, "fubar" ]);
// * returns 3: {42:1, "fubar":1}
var tmp_arr = {}, key = '', t = '';
var __getType = function(obj) {
// Objects are php associative arrays.
var t = typeof obj;
t = t.toLowerCase();
if (t == "object") {
t = "array";
}
return t;
}
var __countValue = function (value) {
switch (typeof(value)) {
case "number":
if (Math.floor(value) != value) {
return;
}
case "string":
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;
}// }}}
// {{{ array_diff
function array_diff (array) {
// Computes the difference of arrays
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_diff/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Sanjoy Roy
// * 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< 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;
}// }}}
// {{{ array_diff_assoc
function array_diff_assoc ( array ) {
// Computes the difference of arrays with additional index check
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_diff_assoc/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: array_diff_assoc({0: 'Kevin', 1: 'van', 2: 'Zonneveld'}, {0: 'Kevin', 4: 'van', 5: 'Zonneveld'});
// * returns 1: {1: 'van', 2: 'Zonneveld'}
var arr_dif = {}, i = 1, argc = arguments.length, argv = arguments, key, key_c, found=false;
// input sanitation
if( !array || (array.constructor !== Array && array.constructor !== Array && typeof array != 'object' && typeof array != 'array') ){
return null;
}
// loop through 1st array
for ( key in array ){
// loop over other arrays
for (i = 1; i< argc; i++){
// find in the compare array
found = false;
if(argv[i][key]){
found = true;
break;
}
if(!found){
arr_dif[key] = array[key];
}
}
}
return arr_dif;
}// }}}
// {{{ array_diff_key
function array_diff_key( object ) {
// Computes the difference of arrays using keys for comparison
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_diff_key/
// + version: 809.522
// + original by: Ates Goral (http://magnetiq.com)
// * example 1: array_diff_key({red: 1, green: 2, blue: 3, white: 4});
// * returns 1: {"red":1, "green":2, "blue":3, "white":4}
// * example 2: array_diff_key({red: 1, green: 2, blue: 3, white: 4}, {red: 5});
// * returns 2: {"green":2, "blue":3, "white":4}
// * example 3: array_diff_key({red: 1, green: 2, blue: 3, white: 4}, {red: 5}, {green: 6, blue: 7});
// * returns 3: {"white":4}
// * example 4: array_diff_key({red: 1, green: 2, blue: 3, white: 4}, {red: 5}, {red: 5});
// * returns 4: {"green":2, "blue":3, "white":4}
var tpm_ar = new Object(), argc = arguments.length, argv = arguments, key, argidx, other;
for (key in object) {
tpm_ar[key] = object[key];
}
for (argidx = 1; argidx < argc; ++argidx) {
other = argv[argidx];
if (other instanceof Object) {
for (key in other) {
delete tpm_ar[key];
}
}
}
return tpm_ar;
}// }}}
// {{{ array_fill
function array_fill( start_index, num, mixed_val ) {
// Fill an array with values
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_fill/
// + version: 810.2018
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: _argos
// * example 1: array_fill(5, 6, 'banana');
// * returns 1: { 5: 'banana', 6: 'banana', 7: 'banana', 8: 'banana', 9: 'banana', 10: 'banana' }
var key, tmp_arr = {};
if ( !isNaN ( start_index ) && !isNaN ( num ) ) {
for( key = 0; key < num; key++ ) {
tmp_arr[(key+start_index)] = mixed_val;
}
}
return tmp_arr;
}// }}}
// {{{ array_flip
function array_flip( trans ) {
// Exchanges all keys with their associated values in an array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_flip/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: array_flip( {a: 1, b: 1, c: 2} );
// * returns 1: {1: 'b', 2: 'c'}
var key, tmp_ar = {};
for( key in trans ) {
tmp_ar[trans[key]] = key;
}
return tmp_ar;
}// }}}
// {{{ array_key_exists
function array_key_exists ( key, search ) {
// Checks if the given key or index exists in the array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_key_exists/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Felix Geisendoerfer (http://www.debuggable.com/felix)
// * example 1: array_key_exists('kevin', {'kevin': 'van Zonneveld'});
// * returns 1: true
// input sanitation
if( !search || (search.constructor !== Array && search.constructor !== Object) ){
return false;
}
return key in search;
}// }}}
// {{{ array_keys
function array_keys( input, search_value, strict ) {
// Return all the keys of an array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_keys/
// + version: 810.2018
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: array_keys( {firstname: 'Kevin', surname: 'van Zonneveld'} );
// * returns 1: {0: 'firstname', 1: 'surname'}
var tmp_arr = {}, strict = !!strict, include = true, cnt = 0;
for ( key in input ){
include = true;
if ( search_value != undefined ) {
if( strict && input[key] !== search_value ){
include = false;
} else if( input[key] != search_value ){
include = false;
}
}
if( include ) {
tmp_arr[cnt] = key;
cnt++;
}
}
return tmp_arr;
}// }}}
// {{{ array_map
function array_map( callback ) {
// Applies the callback to the elements of the given arrays
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_map/
// + version: 809.522
// + original by: Andrea Giammarchi (http://webreflection.blogspot.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: array_map( function(a){return (a * a * a)}, [1, 2, 3, 4, 5] );
// * returns 1: [ 1, 8, 27, 64, 125 ]
var argc = arguments.length, argv = arguments;
var j = argv[1].length, i = 0, k = 1, m = 0;
var tmp = [], tmp_ar = [];
while (i < j) {
while (k < argc){
tmp[m++] = argv[k++][i];
}
m = 0;
k = 1;
if( callback ){
tmp_ar[i++] = callback.apply(null, tmp);
} else{
tmp_ar[i++] = tmp;
}
tmp = [];
}
return tmp_ar;
}// }}}
// {{{ array_pad
function array_pad ( input, pad_size, pad_value ) {
// Pad array to the specified length with a value
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_pad/
// + version: 809.522
// + original by: _argos
// * example 1: array_pad([ 7, 8, 9 ], 2, 'a');
// * returns 1: [ 7, 8, 9]
// * example 2: array_pad([ 7, 8, 9 ], 5, 'a');
// * returns 2: [ 7, 8, 9, 'a', 'a']
// * example 3: array_pad([ 7, 8, 9 ], 5, 2);
// * returns 3: [ 7, 8, 9, 2, 2]
// * example 4: array_pad([ 7, 8, 9 ], -5, 'a');
// * returns 4: [ 'a', 'a', 7, 8, 9 ]
var pad = [], newArray = [], newLength, i=0;
if ( input instanceof Array && !isNaN ( pad_size ) ) {
newLength = ( ( pad_size < 0 ) ? ( pad_size * -1 ) : pad_size );
if ( newLength > input.length ) {
for ( i = 0; i < ( newLength - input.length ); i++ ) { newArray [ i ] = pad_value; }
pad = ( ( pad_size < 0 ) ? newArray.concat ( input ) : input.concat ( newArray ) );
} else {
pad = input;
}
}
return pad;
}// }}}
// {{{ array_pop
function array_pop( array ) {
// Pop the element off the end of array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_pop/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: array_pop([0,1,2]);
// * returns 1: 2
// done popping, are we?
if( !array.length ){
return null;
}
return array.pop();
}// }}}
// {{{ array_product
function array_product ( input ) {
// Calculate the product of values in an array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_product/
// + version: 809.522
// + original by: _argos
// * example 1: array_product([ 2, 4, 6, 8 ]);
// * returns 1: 384
var Index = 0, Product = 1;
if ( input instanceof Array ) {
while ( Index < input.length ) {
Product *= ( !isNaN ( input [ Index ] ) ? input [ Index ] : 0 );
Index++;
}
} else {
Product = null;
}
return Product;
}// }}}
// {{{ array_push
function array_push ( array ) {
// Push one or more elements onto the end of array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_push/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: array_push(['kevin','van'], 'zonneveld');
// * returns 1: 3
var i, argv = arguments, argc = argv.length;
for (i=1; i < argc; i++){
array[array.length++] = argv[i];
}
return array.length;
}// }}}
// {{{ array_rand
function array_rand ( input, num_req ) {
// Pick one or more random entries out of an array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_rand/
// + version: 809.522
// + original by: _argos
// * example 1: array_rand( ['Kevin'], 1 );
// * returns 1: 0
var Indexes = [];
var Ticks = num_req || 1;
var checkDuplicate = function ( input, value ) {
var Exist = false, Index = 0;
while ( Index < input.length ) {
if ( input [ Index ] === value ) {
Exist = true;
break;
}
Index++;
}
return Exist;
};
if ( input instanceof Array && Ticks <= input.length ) {
while ( true ) {
var Rand = Math.floor ( ( Math.random ( ) * input.length ) );
if ( Indexes.length === Ticks ) { break; }
if ( !checkDuplicate ( Indexes, Rand ) ) { Indexes.push ( Rand ); }
}
} else {
Indexes = null;
}
return ( ( Ticks == 1 ) ? Indexes.join ( ) : Indexes );
}// }}}
// {{{ array_reduce
function array_reduce( a_input, callback ) {
// Iteratively reduce the array to a single value using a callback function
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_reduce/
// + version: 809.522
// + original by: Alfonso Jimenez (http://www.alfonsojimenez.com)
// * example 1: array_reduce([1, 2, 3, 4, 5], function(v, w){v += w;return v;});
// * returns 1: 15
var lon = a_input.length;
var res = 0, i = 0;
var tmp = new Array();
for(i = 0; i < lon; i+=2 ) {
tmp[0] = a_input[i];
if(a_input[(i+1)]){
tmp[1] = a_input[(i+1)];
} else {
tmp[1] = 0;
}
res+= callback.apply(null, tmp);
tmp = new Array();
}
return res;
}// }}}
// {{{ array_reverse
function array_reverse( array, preserve_keys ) {
// Return an array with elements in reverse order
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_reverse/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Karol Kowalski
// * example 1: array_reverse( [ 'php', '4.0', ['green', 'red'] ], true );
// * returns 1: { 2: ['green', 'red'], 1: 4, 0: 'php'}
var arr_len=array.length, newkey=0, tmp_ar = {};
for(var key in array){
newkey=arr_len-key-1;
tmp_ar[(!!preserve_keys)?newkey:key]=array[newkey];
}
return tmp_ar;
}// }}}
// {{{ array_search
function array_search( needle, haystack, strict ) {
// Searches the array for a given value and returns the corresponding key if
// successful
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_search/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: array_search('zonneveld', {firstname: 'kevin', middle: 'van', surname: 'zonneveld'});
// * returns 1: 'surname'
var strict = !!strict;
for(var key in haystack){
if( (strict && haystack[key] === needle) || (!strict && haystack[key] == needle) ){
return key;
}
}
return false;
}// }}}
// {{{ array_shift
function array_shift( array ) {
// Shift an element off the beginning of array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_shift/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Martijn Wieringa
// * example 1: array_shift(['Kevin', 'van', 'Zonneveld']);
// * returns 1: 'Kevin'
if (array.length > 0) {
return array.shift();
}
return null;
}// }}}
// {{{ array_sum
function array_sum( array ) {
// Calculate the sum of values in an array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_sum/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: array_sum([4, 9, 182.6]);
// * returns 1: 195.6
var key, sum=0;
// input sanitation
if( !array || (array.constructor !== Array && array.constructor !== Object) || !array.length ){
return null;
}
for(var key in array){
sum += array[key];
}
return sum;
}// }}}
// {{{ array_unique
function array_unique( array ) {
// Removes duplicate values from an array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_unique/
// + version: 810.2018
// + original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
// + input by: duncan
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: array_unique(['Kevin','Kevin','van','Zonneveld','Kevin']);
// * returns 1: ['Kevin','van','Zonneveld']
// * example 2: array_unique({'a': 'green', 0: 'red', 'b': 'green', 1: 'blue', 2: 'red'});
// * returns 2: {'a': 'green', 0: 'red', 1: 'blue'}
var key = '', tmp_arr1 = {}, tmp_arr2 = {};
tmp_arr1 = array;
var __array_search = function ( needle, haystack, strict ) {
var strict = !!strict;
for(var fkey in haystack){
if( (strict && haystack[fkey] === needle) || (!strict && haystack[fkey] == needle) ){
return fkey;
}
}
return false;
}
for (key in tmp_arr1) {
val = tmp_arr1[key];
if (false === __array_search(val, tmp_arr2)) {
tmp_arr2[key] = val;
}
delete tmp_arr1[key];
}
return tmp_arr2;
}// }}}
// {{{ array_unshift
function array_unshift( array ) {
// Prepend one or more elements to the beginning of an array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_unshift/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Martijn Wieringa
// * example 1: array_unshift(['van', 'Zonneveld'], 'Kevin');
// * returns 1: 3
var argc = arguments.length, argv = arguments, i;
for (i = 1; i < argc; i++) {
array.unshift(argv[i]);
}
return (array.length);
}// }}}
// {{{ array_values
function array_values( input ) {
// Return all the values of an array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_values/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: array_values( {firstname: 'Kevin', surname: 'van Zonneveld'} );
// * returns 1: {0: 'Kevin', 1: 'van Zonneveld'}
var tmp_arr = new Array(), cnt = 0;
for ( key in input ){
tmp_arr[cnt] = input[key];
cnt++;
}
return tmp_arr;
}// }}}
// {{{ array_walk
function array_walk (array, funcname, userdata) {
// Apply a user function to every member of an array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_walk/
// + version: 809.522
// + original by: Johnny Mast (http://www.phpvrouwen.nl)
// * example 1: array_walk ({'a':'b'} ,'void', 'userdata');
// * returns 1: true
// * example 2: array_walk ('a' ,'void', 'userdata');
// * returns 2: false
var key;
if (typeof array != 'object'){
return false;
}
for (key in array){
if (typeof (userdata) != 'undefined'){
eval (funcname + '( array [key] , key , userdata )' );
} else {
eval (funcname + '( userdata ) ');
}
}
return true;
}// }}}
// {{{ array_walk_recursive
function array_walk_recursive (array, funcname, userdata) {
// Apply a user function recursively to every member of an array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_walk_recursive/
// + version: 809.522
// + original by: Johnny Mast (http://www.phpvrouwen.nl)
// * example 1: array_walk_recursive ({'a':'b', 'c': {'d' : 'e'}} ,'void', 'userdata');
// * returns 1: true
// * example 2: array_walk_recursive ('a' ,'void', 'userdata');
// * returns 2: false
var key;
if (typeof array != 'object'){
return false;
}
for (key in array) {
if (typeof array[key] == 'object') {
return array_walk_recursive (array [key], funcname, userdata);
}
if (typeof (userdata) != 'undefined') {
eval (funcname + '( array [key] , key , userdata )');
} else {
eval (funcname + '( userdata ) ');
}
}
return true;
}// }}}
// {{{ compact
function compact ( var_names ) {
// Create array containing variables and their values
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_compact/
// + version: 810.112
// + original by: _argos
// + tweaked by: Jack
// * example 1: var1 = 'Kevin'; var2 = 'van'; var3 = 'Zonneveld';
// * example 1: compact('var1', 'var2', 'var3');
// * returns 1: {'var1': 'Kevin', 'var2': 'van', 'var3': 'Zonneveld'}
var Index = 0, Matrix = {};
var process = function ( value ) {
var i = 0, l = value.length, key_value = '';
for (i = 0; i < l; i++ ) {
var key_value = value [ i ];
if ( key_value instanceof Array ) {
process ( key_value );
} else {
if ( typeof window [ key_value ] !== 'undefined' ) {
Matrix [ key_value ] = window [ key_value ];
}
}
}
return true;
};
process ( arguments );
return Matrix;
}// }}}
// {{{ count
function count( mixed_var, mode ) {
// Count elements in an array, or properties in an object
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_count/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: _argos
// * example 1: count([[0,0],[0,-4]], 'COUNT_RECURSIVE');
// * returns 1: 6
// * example 2: count({'one' : [1,2,3,4,5]}, 'COUNT_RECURSIVE');
// * returns 2: 6
var key, cnt = 0;
if( mode == 'COUNT_RECURSIVE' ) mode = 1;
if( mode != 1 ) mode = 0;
for (key in mixed_var){
cnt++;
if( mode==1 && mixed_var[key] && (mixed_var[key].constructor === Array || mixed_var[key].constructor === Object) ){
cnt += count(mixed_var[key], 1);
}
}
return cnt;
}// }}}
// {{{ end
function end ( array ) {
// Set the internal pointer of an array to its last element
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_end/
// + version: 809.1713
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Legaev Andrey
// + revised by: J A R
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + restored by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: end({0: 'Kevin', 1: 'van', 2: 'Zonneveld'});
// * returns 1: 'Zonneveld'
// * example 2: end(['Kevin', 'van', 'Zonneveld']);
// * returns 2: 'Zonneveld'
var last_elm, key;
// The native .pop() method didn't not work with objects (associative arrays)
// We need that for PHP compatibility
if (array.constructor == Array){
last_elm = array[(array.length-1)];
} else {
for (key in array){
last_elm = array[key];
}
}
return last_elm;
}// }}}
// {{{ in_array
function in_array(needle, haystack, strict) {
// Checks if a value exists in an array
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_in_array/
// + version: 809.522
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
// * returns 1: true
var found = false, key, strict = !!strict;
for (key in haystack) {
if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
found = true;
break;
}
}
return found;
}// }}}
// {{{ krsort
function krsort(array, sort_flags) {
// Sort an array by key in reverse order
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_krsort/
// + version: 809.522
// + original by: GeekFG (http://geekfg.blogspot.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: krsort({2: 'van', 3: 'Zonneveld', 1: 'Kevin'});
// * returns 1: true
var tmp_arr = {}, values = array, keys = [], key_num = 0, key = '', i = 0;
var sorter = false, array = false;
// For now only SORT_NUMERIC has a custom sorter
// and SORT_REGULAR, SORT_STRING, and SORT_LOCALE_STRING
// are all handled with the default sorter
if (sort_flags == 'SORT_NUMERIC') {
sorter = function (a, b) {
return(a - b);
};
}
// Make a list of key names
for (key in values) {
keys[key_num++] = key;
}
// Sort key names
if (sorter !== false) {
keys = keys.sort(sorter);
} else {
keys = keys.sort();
}
// What makes it krsort:
keys.reverse();
// Rebuild array with sorted keynames
for (i = 0; i < key_num; i++) {
key = keys[i];
tmp_arr[key] = values[key];
}
// Overwrite the original array, don't return it, to be complient with the php manual
array = tmp_arr;
return true;
}// }}}
// {{{ ksort
function ksort(array, sort_flags) {