English 中文(简体)
我想用首封信在PHP中过滤一个阵列。
原标题:I want to filter an array by first letter in PHP
  • 时间:2011-10-24 20:57:47
  •  标签:
  • php
<?php
function filter($fst, $arr){
$new_arr=array();

    for($i=0;$i<=(count($arr)-1);$i++){
        if(substr($arr[$i], 0, 1) == $fst){
            $new_arr[] = $fst;
        }
    }
    return $new_arr;
}



$list = Array("Apparel","Associations","Building/Grounds","Building/Materials",
              "Dispensing","Disposables","Distributors");

$new_list[]=filter("A", $list);

for($i=0;$i<=(count($new_list)-1);$i++){ 
    echo  <li><a href="?id= .$i. "> .$new_list[$i]. </a></li> ; 
}

?>

我设立了一个称为过滤器的功能,以过滤从“a”这样的特性开始的阵列的内容。 它目前没有工作。

最佳回答

首先,我假定,你想要打印大韩民国语,而不是你正在检查的字面? <代码>new_arr[] = fst; 应new_arr[] = arr[$i];

第二,你将由此而产生的一系列职能重新纳入新的阵列,而不是将阵列分配给你们的变量。 <代码>new_list[] =。

页: 1 我在座标有变化。

function filter($fst, $arr){
$new_arr=array();

    for($i=0;$i<=(count($arr)-1);$i++){
        if(substr($arr[$i], 0, 1) == $fst){
            $new_arr[] = $arr[$i];         <----- Changed here
        }
    }
    return $new_arr;
}



$list = Array("Apparel","Associations","Building/Grounds","Building/Materials",
              "Dispensing","Disposables","Distributors");

$new_list=filter("A", $list);  <----- And changed here

for($i=0;$i<=(count($new_list)-1);$i++){ 
    echo  <li><a href="?id= .$i. "> .$new_list[$i]. </a></li> ; 
}

产出:

<li><a href="?id=0">Apparel</a></li>
<li><a href="?id=1">Associations</a></li>
问题回答

first
don t use

$new_list[] = filter("A", $list);

而是

$new_list = filter("A", $list);

由于您的代码将尝试将new_arrayfilter(>上排在下个阵列的免费索引new_list

<>2>

$new_arr[] = $fst;

错了,因为你重新确定<代码>的新阵列价值。 A,不匹配。 而是使用:

$new_arr[] = $arr[$i];
for($i=0;$i<=(count($arr)-1);$i++){
    if(substr($arr[$i], 0, 1) == $fst){
        //$new_arr[] = $fst; // <-- Bug is here
        $new_arr[] = $arr[$i]; // Try this
    }
}

还有一点:

//$new_list[]=filter("A", $list); // This
$new_list = filter("A", $list); // Should be this
foreach($arr as $word) {
  // do something with $word
}

优于C类<代码>





相关问题
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 ...

热门标签