Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately there is no built-in inverse of array_combine. There is also no way to define one, since array_combine expects multiple parameters and we can't return multiple values from a function.</p> <p>We can construct an alternative to array_combine which takes a single argument: the array of keys and the array of values wrapped up together in another array. This transformation is called "uncurrying" and is performed by the "call_user_func_array" function:</p> <pre><code>$array_comb = function($arr) { return call_user_func_array('array_combine', $arr); }; </code></pre> <p>This alternative function does have an inverse:</p> <pre><code>$array_split = function($arr) { return array(array_keys($arr), array_values($arr)); }; </code></pre> <p>If we define function composition:</p> <pre><code>$compose = function($f, $g) { return function($x) use ($f, $g) { return $f($g($x)); }; }; </code></pre> <p>Then the following functions are all (extensionally) equal, ie. they all return their argument unchanged:</p> <pre><code>$identity = function($x) { return $x; }; $left_inverse = $compose($array_split, $array_comb); // Split then combine $right_inverse = $compose($array_comb, $array_split); // Combine then split </code></pre> <p>Note that they accept different argument types though:</p> <ul> <li>$identity will work on anything.</li> <li>$left_inverse will work on any array.</li> <li>$right_inverse will work on arrays-of-arrays, where the outer array contains 2 elements, both inner arrays are of equal length and the first inner array only contains integers and strings.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload