Anon33290830 Anon33290830 - 12 days ago 5
PHP Question

How to figure out gematria total with PHP?

I am trying to create a PHP application that calculates gematria. Gematria is a secret language that turns words into numbers. I got it to work with words that don't have a duplicate letter.

Words that work:


  • Tesla

  • abcde



Words that don't work:


  • aaa ( has multiple a's )

  • monsanto ( has 2 o's )





/**
* English Gematria Values
*
* @return array $array An array of key value pairs
*/

function english_values() {
$values = array(
'a' => 6,
'b' => 12,
'c' => 18,
'd' => 24,
'e' => 30,
'f' => 36,
'g' => 42,
'h' => 48,
'i' => 54,
'j' => 60,
'k' => 66,
'l' => 72,
'm' => 78,
'n' => 84,
'o' => 90,
'p' => 96,
'q' => 102,
'r' => 108,
's' => 114,
't' => 120,
'u' => 126,
'v' => 132,
'w' => 138,
'x' => 144,
'y' => 150,
'z' => 156,
);
return $values;
}


function calculator( $word ) {
$split = array_flip( str_split( $word ) );
$english = array_intersect_key( english_values(), $split );
return $english;
}

print_r( calculator( 'tesla ') );
echo "</br></br>";
print_r( calculator( 'monsanto ') );


The output is:


Array ( [a] => 6 [e] => 30 [l] => 72 [s] => 114 [t] => 120 )

Array ( [a] => 6 [m] => 78 [n] => 84 [o] => 90 [s] => 114 [t] => 120 )


You can see tesla was able to go through each letter. But Monsanto missed the last o so there is no way I will be able to calculate a correct total.

I would also like the letters to be in order of the word so tesla would output:


Array ( [t] => 120 [e] => 30 [s] => 114 [l] => 72 [a] => 6 )


I may be using the wrong PHP functions to handle this so I would appeciate any help in the right direction. I haven't coded in a while so I am a bit rusty. I tried multiple PHP functions but this seemed to get me the closest to what I need to do. Thank you.

Edit: I also understand the reason the duplicate letters don't show is because there can not be duplicate keys. So I assume I am doing this probably pretty backwards.

Answer

Hopefully this moves you forward:

print_r(encodeWord('monsanto'));

function encodeWord($word) {
    for ($i = 0; $i < strlen($word); $i++) {
        $encodedWord[] = [
            'letter' => $word[$i],
            'convertedLetter' => convertLetter($word[$i]),
        ];
    }

    return $encodedWord;
}

function convertLetter($letter) {
    $values = [
        'a' => 6,
        'b' => 12,
        'c' => 18,
        'd' => 24,
        'e' => 30,
        'f' => 36,
        'g' => 42,
        'h' => 48,
        'i' => 54,
        'j' => 60,
        'k' => 66,
        'l' => 72,
        'm' => 78,
        'n' => 84,
        'o' => 90,
        'p' => 96,
        'q' => 102,
        'r' => 108,
        's' => 114,
        't' => 120,
        'u' => 126,
        'v' => 132,
        'w' => 138,
        'x' => 144,
        'y' => 150,
        'z' => 156,
    ];

    return $values[$letter];
}
Comments