Skip to content

Instantly share code, notes, and snippets.

@DavidGoodwin
Forked from Daniel15/gist:5991193
Last active August 29, 2015 13:58
Show Gist options
  • Select an option

  • Save DavidGoodwin/10023233 to your computer and use it in GitHub Desktop.

Select an option

Save DavidGoodwin/10023233 to your computer and use it in GitHub Desktop.

Revisions

  1. DavidGoodwin revised this gist Apr 7, 2014. 1 changed file with 23 additions and 1 deletion.
    24 changes: 23 additions & 1 deletion gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,28 @@
    <?php
    /* Original effort: https://gist.github.com/Daniel15/5991193 */
    function last($arr)
    {
    eval('list(' . str_repeat(',', count($arr) - 1) . '$result) = $arr;');
    return $result;
    }
    }



    /* An interesting challenge - what about : */
    $list = array('a' => 'fish', 'b' => 'sausage', '&c' => 'whal=asdasd!@£123e');

    /**
    * Return the last element of an array AND it's associated key
    * @param array (e.g. [ 23, 4, 5, 'fish' => 'beans' ] etc.)
    *
    * @return array last element of the array (as [$key => $value])
    */

    function last_element($array) {
    $string = http_build_query($array);
    preg_match('/(&?([^&=]*)=([^&=]*))$/', $string, $matches);
    $key = urldecode($matches[2]);
    $value = urldecode($matches[3]);
    return array($key => $value);
    }
    var_dump(last_element($list));
  2. @Daniel15 Daniel15 revised this gist Jul 14, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    function last($arr)
    {
    eval('list(' . str_repeat(',', count($arr) - 1) . '$result) = $arr;');
  3. @Daniel15 Daniel15 created this gist Jul 13, 2013.
    5 changes: 5 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    function last($arr)
    {
    eval('list(' . str_repeat(',', count($arr) - 1) . '$result) = $arr;');
    return $result;
    }