i have an array which holds 3 keys which holds a string inside it, like this:
Array
(
[1] = bananas, kiwi, apples, pineapple, mango.tomato
[2] = fruit, vegetables, meat
[3] = car, bike, truck
)
How would i sort each key so that the values are in alphabetical order, like this:
Array
(
[1] = apples, bananas, kiwi, mango, pineapple, tomato
[2] = fruit, meat, veg
[3] = bike, car, truck
)
Ive tried using usort() but it doesn't run and throws the error usort expects the first parameter to be an array.
I also tried using multisort but it gave me a similar error message.
Here is my code:
$file = fopen("file_path", "r");
while(($lines = fgetcsv($file, 0, ":")) !== FALSE) {
$data[$lines[0]] = $lines[1];
array_multisort($lines[0], SORT_ASC, SORT_STRING,
$lines[1], SORT_NUMERIC, SORT_DESC);
echo "$lines[1]\n"; //This line is just to see what it looks like
}
the csv file isnt actually in csv format it is a .txt file but it still works and was the best way i could find to get the results i wanted from the file. The file is formatted like this:
1:hannah.Smith:address
2:Bob.jones:address
3:harry.white:address
....
*sortfunctions expect an array to sort. From the loop you have provided it can be assumed, that you pass scalar value to it (particulary a string):$lines[0]holds first column value (i.e.'1','2', etc.) and$lines[1]holds second column value (i.e.'hannah.Smith'). So your$linesvariable should be called$line.