Thursday, 30 August 2018

Creating an associative array with strings

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);

0 comments:

Post a Comment