Page 1 of 1

problem with array... [SOLVED]

Posted: Tue Dec 27, 2005 10:30 am
by duk

Code: Select all

$groups_bb_code = array(

   '1' => 'A',
   '2' => 'B',
   '3' => 'C',
   '4' => 'D',
   '5' => 'E',
   '6' => 'F',
   '7' => 'G',
   '8' => 'H',
   '9' => 'I',
   '10' => 'J',
   '11' => 'K',
   '12' => 'L',
   '13' => 'M',
   '14' => 'N',
   '15' => 'O',
   '16' => 'P',
   '17' => 'Q',
   '18' => 'R',
   '19' => 'S',
   '20' => 'T',
   '21' => 'U',
   '22' => 'V',
   '23' => 'W',
   '24' => 'X',
   '25' => 'Y',
   '26' => 'Z'
);

$total_in_array = count($groups_bb_code);

for ( $i = 1; $i <= $total_in_array ; $i++ ) {

      $letter = str_replace(array_keys($groups_bb_code), array_values($groups_bb_code), $i);

      echo "$i\n -> $letter <br>";

}
output

Code: Select all

1 -> A
2 -> B
3 -> C
4 -> D
5 -> E
6 -> F
7 -> G
8 -> H
9 -> I
10 -> A0
11 -> AA
12 -> AB
13 -> AC
14 -> AD
15 -> AE
16 -> AF
17 -> AG
18 -> AH
19 -> AI
20 -> B0
21 -> BA
22 -> BB
23 -> BC
24 -> BD
25 -> BE
26 -> BF
anyone knows what is happening here ??

Posted: Tue Dec 27, 2005 10:51 am
by nigma
Why use str_replace()? You can do the same thing without the function call:

Code: Select all

foreach ($groups_bb_code as $key => $val) {
  print "$key => $val";
}

Posted: Tue Dec 27, 2005 11:01 am
by duk
wow nice, i have tried and is ok, i didn't remember forech...

anyway about str_replace... still strange... you know why it happens like that ?

Posted: Tue Dec 27, 2005 11:08 am
by josh
duk wrote:wow nice, i have tried and is ok, i didn't remember forech...

anyway about str_replace... still strange... you know why it happens like that ?
probably because str_replace finds the first thing it can match, so it replaces the "1" with the "a", then it looks at the 0 and it doesn't match anything so it stays as is. (me thinks) try reversing the array, so it checks for "10", then checks for "1" later

array_reverse() <- that will help

Posted: Tue Dec 27, 2005 11:10 am
by duk
probably that...

thanks