1

I'm not asking you to code it for me or anything, snippets of code or just pointing me in the right direction will help me out a lot.

This is what I'm basically trying to do, you have the word "abcdefg"

I want it to split up the string into an array (or whatever works best?) , then assign a value based on what letter is stored into the array. (based on the alphabet , a = 1 , b = 2 , c = 3 , z = 26)

so abcdefg will turn into something like

$foo("a" => 1, "b" =>2, "c" => 3, etc..);

but obviously if "abcdefg" was "zaj", the array would be "z" => 26 , "a" => 1 , "j" => 10

Basically, convert a string of letters into their alphabetical num value sorta?

2 Answers 2

1

The functions you need are:

With those functions you can solve this problem in a very small number of lines of code.

Sign up to request clarification or add additional context in comments.

4 Comments

@user3106858 using ord you might need to use strtolower as well as subtract 96 from the result.
I was noticing that also. Thanks again. It's sorta confusing me but ill see what I can do with this. :)
@user3106858 the result of ord is in ascii so you need to subtract 96 to make it ordered as in a=1, b=2, etc.
A typical fragment of code when doing these kinds of manipulations is something like: ord($letter) - ord('a').
0

Steps that has been put to implement this.

  • Generate an alphabet array (alphabets starting from index 1)
  • Get the value which which you want to generate as an array.
  • Split them up with str_split
  • Using a foreach construct search them in the earlier alphabet array that was created using array_search
  • When found just , just grab their key and value and put it in the new array.

<?php
$alph[]='Start';
foreach(range('a','z') as $val)
{
    $alph[]=$val;
}

$makethisvalasarray='zaj';
$makethisvalasarray=str_split($makethisvalasarray);

foreach($makethisvalasarray as $val)
{
    $newarr[$val]=array_search($val,$alph);
}

print_r($newarr);

OUTPUT :

Array
(
    [z] => 26
    [a] => 1
    [j] => 10
)

1 Comment

I already know about str_split, but I'm having an issue assigning the values based upon if what letter it is. I will look again at the php documentation, maybe i missed something.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.