English 中文(简体)
Echoing Array in an A-Z list
原标题:Echoing Array in an A-Z List

我有以下阵列:

$aTest = array( apple ,  pear ,  banana ,  kiwi ,  pineapple ,  strawberry );

我需要能够从A-Z中拟定一份清单,并在每封信中显示阵列的相关价值。 E.g A - Diamond, B - Banana, C - 空洞的D - 空的,...... K - Kiwi . P - Pear,Petapple等

谁能帮助我? 我从A-Z中抽空,但当时无法确定如何反映阵列的相关价值(以及每封信中按字母顺序排列的字句,即 p之前的耳朵)。

成就

最佳回答

通过使用有条件和一些内在功能;这部法律是少许的,但它是trick的:

<?php
    $aTest=array( apple , pear , banana , kiwi , pineapple , strawberry );
    $len=(count($aTest)-1);
    $letters=array( a , b , c , d , e , f , g , h , i , j , k , l , m , n , o , p , q , r , s , t , u , v , w , x , y , z );
    $str=null;
    foreach($letters as $letter) {
        for($i=0;$i<=$len;$i++) {
            $str=strtoupper($letter).  -  ;
            if(strtolower(substr($aTest[$i],0,1))==strtolower($letter)) {
                $str.=$aTest[$i];
                break;
            } elseif($i==$len) {
                $str.= empty ;
            }
        }
        echo($str. <br /> );
    }
?>
问题回答

我建议您使用<代码>sort(),以便按顺序排列。 然后跟踪第一封信。 任何时候,它都会改变、节省和产出。

sort( $aTest );

$currChar =   ;
foreach ( $aTest as $word) {
  if ( $word[0] != $currChar ) {
    $currChar = $word[0];
    echo $currChar . PHP_EOL;
  }
  echo $word . PHP_EOL;
}

从你的最初阵列来看,这将产生以下成果:

a
apple
b
banana
k
kiwi
p
pear
pineapple
s
strawberry
<?php
$aTest = array( apple ,  pineapple ,  banana ,  kiwi ,  pear ,  strawberry );

$range = range( a , z );

$output = array();

foreach($range AS $letters){
    foreach($aTest AS $fruit){
        if(strtolower($fruit[0]) == $letters){
            $output[$letters][] = $fruit;
        }
    }
}

foreach($output AS $letter => $fruits){
    echo $letter . "<br/>--------<br/>
";
    sort($fruits);
    foreach($fruits AS $indFruit){
        echo $indFruit . "<br/>
";
    }
    echo "<br/>
";
}
?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签