0

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 ?

5
  • The syntax is quite simple : $hash_table["key"] = "value"; Commented Jun 17, 2013 at 11:44
  • How do you sort a single word? Do you want the letters to be switched position to be alphabettical? Commented Jun 17, 2013 at 11:47
  • So "Correct" would be sorted as "ceorrtC" ? Commented Jun 17, 2013 at 11:49
  • @MayankKumar Please post the code you're using to generate the associative array, that way we could "fix" where you went wrong. Commented Jun 17, 2013 at 11:53
  • @HugoDelsing : Exactly. That's what I'm doing. Commented Jun 18, 2013 at 4:36

3 Answers 3

3
$a = array('green', 'yellow', 'red');//actual
$b = array('green', 'yellow', 'red');
sort($b); //sorted
$c = array_combine($b, $a);
Sign up to request clarification or add additional context in comments.

Comments

1

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++];
}

5 Comments

good, not sure if that's what the OP needs since this will give a multidimensional array ($original is an array).
@HamZa I think, the now edited answer will atleast be close to answer the OP's question.
Yeah, you got +1 from me :)
@HamZa I think you should rather +1 the other answer. :)
I did, it's a bit sad since I was going to post exactly that answer if the OP has answered my question (in the comments) :p
0

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"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.