PHP:

Salma Hayek

PHP IMPLEMENTATIONS #2
PHP implementations of javascript functions already sported on this website. Many javascript functions perform tasks whose utility in Php too is obvious, so here a few of them are turned into Php codex.

The details about the inner workings and the usage and the arrangements of the arguments and about what is returned are not covered here: the javascript version documentation coverage of the same function is sufficient: thus only a link to it is provided, and further details are added only if the Php implementation has some particular specificity of its own.

February 2004
{ @ }
The model above is Salma Hayek
LOADS OF SALMA HAYEK ON THE NET


IMPLEMENTATIONS #2: PHP FUNCTIONS DERIVED FROM JAVASCRIPTS ON THIS SITE
The documentations on what these functions do are on the linked javascript files

a Berenice Abbott photo
a Berenice Abbott photo, statue along the tidal basin, Washington DC, 1954
DEALENTRIES, DEALENTRIES2, CHECKENTRIES



Check the javascript documentation for the details.
Anyway the functions do the following:
  • dealEntries: given an input array, it returns an array of two arrays: the first is a set of randomly extracted (dealt) items all different from each other; the second is a set with all the non extracted items (remains). The input array is passed as first argument, the second argument specifies how many items you want to deal.
    It returns all different items, no copies.
    This function may stall if the input array contains identical entries in such a quantity that it is impossible to draw out of it the defined amount of different items. To veirfy if that could be the case, check by using checkEntries prior to running dealEntries.
  • dealEntries2: as above but does not stall, that is: it accepts drawing more times the same item if more instances of that item are present in the input array.
  • checkEntries: verifies whether you can truly extract the specified amount (second argument) of different entries from an input array (first argument). Usually to be used in combination with (prior to) dealEntries.
An alternative version would have implied just shuffling by the built in php method shuffle and then slicing out the desired amount. Indeed, this is prcisely the implementation used on dealEntries2, which thus differs in its code from its javascript version.
Yet this would have not granted features such as the one granted by dealEntries namely that only different values will be picked - in case this is what you want; shuffling by the php method shuffle an array which carries multiple identical entries and then slicing it, would have implied no guarantee whatsoever that the yielded and sliced entries would be all different. Thus in that case it was necessary to keep the implementation faithful to its javascript version.

Anyway delaEntries uses mt_srand to grab random indexes and that method is more powerful than srand and that could have not been used with the php shuffle routine because php shuffle accepts only srand.
Anyway the shuffling method I use in dealEntries, becomes necessary in the implementation of the so called "Symmetrical Shuffle" which is in the next chapter of this file.

a Berenice Abbott photo
a Berenice Abbott photo, House Belfast, Maine along Route 1, 1954
SYMMETRICALSHUFFLE


Check the javascript documentation for the details.
Anyway the function does the following: php already has a built in shuffle function, so my shuffleArray javascript was not needed to be implemented in php anew.
Yet, php has no symmetrical shuffle built in function.
By this neologism, symmetrical shuffle, I mean to address an exigency that once I was requested to solve, namely the possibility, upon shuffling an array, to maintain the symmetry (entry by entry correspondences, that is) that other arrays had with it before the shuffling process. So, the function takes in a set of subsequent arrays, and shuffles them all accordingly to the same process, so that all the original correspondences among the original arrays are preserved though they undergo a reshuffling.

Unlike its javascript equivalent, this function takes as arguments only two arguments: an array which must be a list of arrays (that is, an array whose each entry is one of the arrays that must be shuffled), and one more arguments that in the javascript version corresponds to the one named recursive (that is, to repeat the shufflings X times).
Differences in the lengths of the passed arrays do not impair the function: it will manage that.
The reason the javascript accepted more is that a javascript is released by definition upon a promiscuous environment for it must be able to work also under generic user inputs: thus it must be very flexible, and account for a range of possible different (maybe even misunderstood) inputs. With php, being entirely on server side namely hidden from the General User Interfaces, we may perhaps safely assume the scripters are more aware of and competent about the restraints a function signature imposes.
The arrays must be passed as one array (that is, an array whose each entry is one of the arrays to be symmetrically shuffled). A second argument named recursive allows more shufflings. Unlike in the javascript version, here a max recursion cycles has been set to 50 rounds at most, to avoid crashes of the server.
Returns an array of arrays: each of the listed arrays is a symmetrically shuffled array.

The function named sortMultishaffle is the more powerful parallel shuffling function with quicksort support. Given the way PHP works, I had to split it into two functions plus two globally defined variables. Yet you still invoke it as you invoked its equivalent javascript, and has its same limtations namely the sizes of the arrays must be identical:
sortMultishuffle($arrayOfArrays_here);

a Berenice Abbott photo
a Berenice Abbott photo, Bonaire Motel, Miami Beach, 1954
ORIENTATOR

Check the javascript documentation for the details.
Anyway the function does the following: given an input array of x numbers representing the coordinates necessary to pinpoint a location within an x dimensioned matrix, it returns the coordinates of all the confining points, both straight and diagonal. Possibly useful also on php side when for instance you want to implement mazes or games and you need to find the confining coordinates to a location a character (such as in a game) is located at.