Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对数组中的数据进行排序_Php - Fatal编程技术网

Php 对数组中的数据进行排序

Php 对数组中的数据进行排序,php,Php,我得到了一个有7种水果的数组: $fruits = array( "lemon", "orange", "banana", "apple", "cherry", "apricot", "Blueberry" ); 我不知道如何以这样的方式打印数据: <A> Apple, Apricot <!--(Note that Apricot is followed by Apple in alphabetic order)--> <B

我得到了一个有7种水果的数组:

$fruits = array(
  "lemon", 
  "orange", 
  "banana", 
  "apple", 
  "cherry", 
  "apricot", 
  "Blueberry"
);
我不知道如何以这样的方式打印数据:

<A>
Apple, Apricot <!--(Note that Apricot is followed by Apple in alphabetic order)-->
<B>
Banana
<C>
Cherry
<L>
Lemon
<O>
Orange

苹果、杏
香蕉
樱桃
柠檬
橙色
很抱歉,这个问题可能有点难。
但是如果可以,请提供帮助。

我会首先使用数组排序功能,如,然后创建一个循环,通过数组跟踪输出的最后一个单词的第一个字母是什么-每次第一个字母发生变化时,首先输出一个新的标题。

尝试以下方法:

sort($fruit);
$lastLetter = null;

foreach ($fruit as $f) {
    $firstLetter = substr($f, 0, 1);
    if ($firstLetter != $lastLetter) {
        echo "\n" . $firstLetter . "\n";
        $lastLetter = $firstLetter;
    }
    echo $f . ", ";
}
需要从该片段中进行一些整理,但您已经明白了。

您可以这样做:

// make the first char of each fruit uppercase. 
for($i=0;$i<count($fruits);$i++) {
        $fruits[$i] = ucfirst($fruits[$i]);
}

// sort alphabetically.
sort($fruits);

// now create a hash with first letter as key and full name as value.
foreach($fruits as $fruit) {
        $temp[$fruit[0]][] = $fruit;
}

// print each element in the hash.
foreach($temp as $k=>$v) {
        print "<$k>\n". implode(',',$v)."\n";
}
//将每个水果的第一个字符设为大写。
对于($i=0;$i$v){
打印“\n”。内爆(“,”,$v)。“\n”;
}

这应该满足您的需求:

    $fruits = array("lemon","orange","banana","apple","cherry","apricot","Blueberry");

//place each fruit in a new array based on its first character (UPPERCASE)
$alphaFruits = array();
foreach($fruits as $fruit) {
    $firstChar = ucwords(substr($fruit,0,1));
    $alphaFruits[$firstChar][] = ucwords($fruit);
}

//sort by key
ksort($alphaFruits);

//output each key followed by the fruits beginning with that letter in a order
foreach($alphaFruits as $key=>$fruits) {
    sort($fruits);
    echo "<{$key}>\n";      
    echo implode(", ", $fruits)."\n";   
}
$fruits=数组(“柠檬”、“橘子”、“香蕉”、“苹果”、“樱桃”、“杏子”、“蓝莓”);
//根据每个水果的第一个字符(大写)将其放入新数组中
$alphaFruits=array();
foreach($水果作为$水果){
$firstChar=ucwords(substr($fruit,0,1));
$alphaFruits[$firstChar][]=ucwords($fruit);
}
//按键排序
ksort($alphaFruits);
//按顺序输出每个键,后跟以该字母开头的水果
foreach($alphaFruits作为$key=>$fruits){
分类(水果);
回音“\n”;
回声内爆(“,”,$FROUTS)。“\n”;
}

请参见,您可以使用
$firstLetter=$f[0]
避免函数calltrue,尽管我更喜欢使用字符串函数。鉴于PHP的松散类型,它使代码在我看来更加清晰。@Tom你怎么知道它在后台不会做同样的事情?@SeanJA:我认为调用函数并将字符串传递给它会有一些开销,等等。可能很小。@Tom:不过在后台,php可以识别出这些都是相同的东西,并为您进行优化(我并不是说它这么聪明……但它可能是,因此微观优化没有什么意义,使代码可读性降低)。您还可以使用
natcasesort
而不是更改数据。