0

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
....
3
  • 1
    Could you provide a sample of the actual csv file. Also, trying to sort inside the while loop on just one field, doesn't seem like the right way to do it... you would need to sort after you are done in the while loop (so that you have the csv fully added to the array). Commented Jul 7, 2018 at 2:17
  • @Coder, could you, please, ask the actual question about what you trying to accomplish. I do not see how the first half of your post coincides with the second:) From what I see, you want to read from CSV file and order the lines somehow. How should they be ordered and for what purpose? Commented Jul 7, 2018 at 10:36
  • @Coder, regarding the errors you get. Well, the *sort functions 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 $lines variable should be called $line. Commented Jul 7, 2018 at 10:42

4 Answers 4

1

Here's a function that takes an array and a delimiter and returns a the same array with each element sorted.

$array = [
    'bananas, kiwi, apples, pineapple, mango.tomato',
    'fruit, vegetables, meat',
    'car, bike, truck'
];

var_dump(sortArrayStringValues($array, ', '));

/**
 * @param string[] $array
 * @param string $delimiter
 * @return array
 */
function sortArrayStringValues(array $array, string $delimiter = ',')
{
    foreach ($array as &$value) {
        $words = explode($delimiter, $value);
        sort($words);
        $value = implode($delimiter, $words);
    }

    return $array;
}

Output

array(3) {
  [0] =>
  string(46) "apples, bananas, kiwi, mango.tomato, pineapple"
  [1] =>
  string(23) "fruit, meat, vegetables"
  [2] =>
  string(16) "bike, car, truck"
}
Sign up to request clarification or add additional context in comments.

Comments

0

Oh, the error is telling you that you're working with an array of strings.

You need to convert each string in another array, so at the end you would have an array of array of strings

$file = fopen("file_path", "r");
while(($lines = fgetcsv($file, 0, ":")) !== FALSE) {

    $data[$lines[0]] = explode($lines[1], ','); // CONVERT STRING TO ARRAY

    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
}

Maybe?

Comments

0

Code

$a = array(1 =>  'bananas, kiwi, apples, pineapple, mango', 2 => 'fruit, vegetables, 
meat', 3 => 'car, bike, truck');
$b="";


 foreach ($a as $key => $value)
      $b = $b ." " .$value;

print $b."<br>************<br>";

$string = explode(",", $b);
sort($string);
foreach ($string as $val) {
    echo $val."<br>";
}

Output

bananas, kiwi, apples, pineapple, mango fruit, vegetables, meat car, bike, truck
************
apples
bananas
bike
kiwi
mango fruit
meat car
pineapple
truck
vegetables

I hope this helps, you could probably reduce the steps with these:

sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key

It looks like asort might do trick. I didn't have time to try it. I see a problem with my code the explode delimiter is a "," but these are spaces after some values.

Comments

0
You can try this. I hope it will help you.
$inventory=Array
(
  [1] = apples, bananas, kiwi, mango, pineapple, tomato
  [2] = fruit, meat, veg
  [3] = bike, car, truck
);

foreach ($inventory as $key => $row)
{
    inventory[$key] = sortindex($row);//call sorting method
}
echo inventory;



//sorting an array
function sortindex($row){
$array=sort(implode(',',$row));
return explode(',',$array);
}

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.