使用字符串创建关联数组

I read some words from a text file, storing each word as an array element using the file() function. Now I need to sort each word and create an associative array that stores the sorted strings as keys and the original strings as values like so :

$hash_table = array( 'sorted_string' => 'original string' );

I loop through every word read from the file and sort it in ascending order but when it comes to pushing it to the associative array, I am completely lost. Can anyone show me how to create the associative array ?

$a = array('green', 'yellow', 'red');//actual
$b = array('green', 'yellow', 'red');
sort($b); //sorted
$c = array_combine($b, $a);

If I understand your question right, consider this:

$sorted;   //sorted array
$original; //original array

foreach($sorted as $key){
  $index = 0;
  $new_array[$key] = $original[$index++];
}

Here is what you want:

<?php
//create an array with words, similar to what you get with file()
$str = "here is a list of random words that will be sorted";
$array = explode(" ", $str);

//a place to store the result
$result = array();

//check each value
foreach($array as $word) {
  //str_split will create an array from a string
  $letters = str_split(trim($word));
  //sort the letters
  sort($letters);

  //implode the letters again to a single word
  $sorted = implode($letters);

  //add to result
  $result[$sorted] = $word;
}

//dump
var_dump($result);

//sort on the key
ksort($result);

//dump
var_dump($result);
?>

This will output

//unsorted
array(11) {
  ["eehr"]=>
  string(4) "here"
  ["is"]=>
  string(2) "is"
  ["a"]=>
  string(1) "a"
  ["ilst"]=>
  string(4) "list"
  ["fo"]=>
  string(2) "of"
  ["admnor"]=>
  string(6) "random"
  ["dorsw"]=>
  string(5) "words"
  ["ahtt"]=>
  string(4) "that"
  ["illw"]=>
  string(4) "will"
  ["be"]=>
  string(2) "be"
  ["deorst"]=>
  string(6) "sorted"
}

//sorted on key
array(11) {
  ["a"]=>
  string(1) "a"
  ["admnor"]=>
  string(6) "random"
  ["ahtt"]=>
  string(4) "that"
  ["be"]=>
  string(2) "be"
  ["deorst"]=>
  string(6) "sorted"
  ["dorsw"]=>
  string(5) "words"
  ["eehr"]=>
  string(4) "here"
  ["fo"]=>
  string(2) "of"
  ["illw"]=>
  string(4) "will"
  ["ilst"]=>
  string(4) "list"
  ["is"]=>
  string(2) "is"
}