CamiloEI
CamiloEI

Reputation: 13

How to change the values of this array with letters alphabetically ordered in php?

I'm trying to convert an array in another array replacing the values with letters alphabetically ordered, but when it find an equal value put the same letter.

This is for a PHP script.

In the result I have this:

Array
(
    [1] => 10014869
    [2] => 10014869
    [3] => 10010380
    [4] => 10112932
    [5] => 10112932
    [6] => 10113908
    [7] => 10115620
    [8] => 10014876
    [9] => 10017457
    [10] => 10010397
)

I want to convert it in this:

Array
(
    [1] => A
    [2] => A
    [3] => B
    [4] => C
    [5] => C
    [6] => D
    [7] => E
    [8] => F
    [9] => G
    [10] => H
)

Thanks for all suggestions

Upvotes: 0

Views: 102

Answers (3)

The fourth bird
The fourth bird

Reputation: 163362

One option is to create a mapper mapping the number to the letter and use a foreach loop:

$mapper = [
    10014869 => "A",
    10010380 => "B",
    10112932 => "C",
    10113908 => "D",
    10115620 => "E",
    10014876 => "F",
    10017457 => "G",
    10010397 => "H"
];

$array = [
    1 => 10014869,
    2 => 10014869,
    3 => 10010380,
    4 => 10112932,
    5 => 10112932,
    6 => 10113908,
    7 => 10115620,
    8 => 10014876,
    9 => 10017457,
    10 => 10010397,
];

foreach($array as $key => $value) {
    $array[$key] = $mapper[$value];
}

print_r($array);

Result

Array
(
    [1] => A
    [2] => A
    [3] => B
    [4] => C
    [5] => C
    [6] => D
    [7] => E
    [8] => F
    [9] => G
    [10] => H
)

php demo

Or create the mapper dynamically using a range:

$unique = array_unique($array);
$mapper = array_combine($unique, range('A', chr(65 + count($unique) - 1)));

foreach($array as $key => $value) {
    $array[$key] = $mapper[$value];
}

print_r($array);

php demo

Upvotes: 0

Kep
Kep

Reputation: 5857

My take on this:

<?php

$array = [
    10014869,
    10014869,
    10010380,
    10112932,
    10112932,
    10113908,
    10115620,
    10014876,
    10017457,
    10010397
];
$map  = array_flip(array_values(array_unique($array)));

array_walk($array, function(&$e) use ($map)
{
    $e = chr($map[$e] + 65);
});

print_r($array);

Result:

Array
(
    [0] => A
    [1] => A
    [2] => B
    [3] => C
    [4] => C
    [5] => D
    [6] => E
    [7] => F
    [8] => G
    [9] => H
)

Note: This only works as expected with <= 26 different entries in $array, the next characters won't be letters. (See the ASCII Table for the ASCII values of 91 and above)

Upvotes: 1

carlos a.
carlos a.

Reputation: 188

This is an option, it is not the most optimal or ordered but it works, since we do not know values of the original array. (also works with alphabet beyond position 27)

// raw data
$origin = [ 1 => 10014869,
            2 => 10014869,
            3 => 10010380,
            4 => 10112932,
            5 => 10112932,
            6 => 10113908,
            7 => 10115620,
            8 => 10014876,
            9 => 10017457,
            10 => 10010397 ];

// setup correct unique match value to letter
$unique = array_values(array_unique($origin));

$length = count($unique);

$matchToLetter = [];

for($i=0; $i<$length; $i++){
    $number = $i+1;
    $matchToLetter[$unique[$i]] = columnLetter($number);
}

// match with original array
$resultArray = [];

foreach($origin as $key => $value) {
    $resultArray[$key] = $matchToLetter[$value];
}

print_r($resultArray);


function columnLetter($c){
    $c = intval($c);
    if ($c <= 0) return '';
    $letter = '';
    while($c != 0){
       $p = ($c - 1) % 26;
       $c = intval(($c - $p) / 26);
       $letter = chr(65 + $p) . $letter;
    }
    return $letter;
}

and result:

Array
(
    [1] => A
    [2] => A
    [3] => B
    [4] => C
    [5] => C
    [6] => D
    [7] => E
    [8] => F
    [9] => G
    [10] => H
)

Upvotes: 0

Related Questions