Decrementing alphabetical values

php

Solution

PHP has overloaded `++` for strings; this isn't the case for `--`. You can do the same thing with much cleaner code with `chr`, `ord`, and `array_map`:

function decrementLetter($l) {
    return chr(ord($l) - 1);
}

function move_up_left($x) {
    if($x['orientation'] === 'down') $arr = &$x[0];
    else $arr = &$x[1];

    $arr = array_map('decrementLetter', $arr);

    return $x;
}

Here's a demo. Note that you may need to add a special case for decrementing `a` - I'm not sure how you want to deal with that.

Problem

I'm trying to figure out how to shift a bunch of letter values in an array down one step. For example, my array contains values ("d", "e", "f", "g", "h") and I want to change this to ("c", "d", "e", "f", "g"). Here's the code I'm working with: ``` function move_up_left($x) { if($x['orientation'] == "down") { foreach($x[0] as &$value) { $value = --$value; } } else { foreach($x[1] as &$value) { $value = --$value; } } return $x; } ``` When I use positive values, the letters change; however the negative numbers do not seem to be working at all.

Original source