对数组键值进行排序吗?

时间:2018-07-07 02:07:49

标签: php arrays sorting

我有一个数组,其中包含3个键,其中包含一个字符串,如下所示:

Array
(
  [1] = bananas, kiwi, apples, pineapple, mango.tomato
  [2] = fruit, vegetables, meat
  [3] = car, bike, truck
)

我将如何对每个键进行排序,以使值按字母顺序排列,如下所示:

Array
(
  [1] = apples, bananas, kiwi, mango, pineapple, tomato
  [2] = fruit, meat, veg
  [3] = bike, car, truck
)

我曾经尝试过使用usort(),但是它没有运行并抛出错误,而usort期望第一个参数是一个数组。

我也尝试使用多排序,但是它给了我类似的错误消息。

这是我的代码:

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

csv文件实际上不是csv格式,它是一个.txt文件,但它仍然可以正常工作,并且是我能找到从文件中获得所需结果的最佳方法。该文件的格式如下:

1:hannah.Smith:address
2:Bob.jones:address
3:harry.white:address
....

4 个答案:

答案 0 :(得分:1)

这是一个函数,该函数接受一个数组和一个定界符,并返回对每个元素排序的相同数组。

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

输出

array(3) {
  [0] =>
  string(46) "apples, bananas, kiwi, mango.tomato, pineapple"
  [1] =>
  string(23) "fruit, meat, vegetables"
  [2] =>
  string(16) "bike, car, truck"
}

答案 1 :(得分:0)

哦,错误告诉您正在使用字符串数组。

您需要将每个字符串转换为另一个数组,因此最后您将拥有一个字符串数组数组

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

也许吗?

答案 2 :(得分:0)

代码

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

输出

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

我希望这会有所帮助,您可以减少以下步骤:

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

看起来各种各样可能会骗人。我没有时间尝试。我发现我的代码有问题,爆炸定界符是“,”,但在某些值后面是空格。

答案 3 :(得分: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);
}