Anonymous_Coder
Anonymous_Coder

Reputation: 19

Sorting an array key value?

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
....

Upvotes: 0

Views: 60

Answers (4)

PHP Geek
PHP Geek

Reputation: 4033

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

Upvotes: 0

James Leveille
James Leveille

Reputation: 21

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.

Upvotes: 0

brogrammer
brogrammer

Reputation: 11

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

Upvotes: 1

mxcoder
mxcoder

Reputation: 198

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?

Upvotes: 0

Related Questions