替换数组php中的字符串

时间:2015-08-22 05:43:34

标签: php arrays

我有一个名为$info的数组。我想替换字符串" YES"在数组中的任何地方找到空格或空字符串。如何才能完成?

以下是代码:

<?php
$conn = mysql_connect("localhost" , "root" , "") or die("Can not connect." . mysql_error());
$db = mysql_select_db("leothian" , $conn) or die("Can not connect." . mysql_error());
$selTable = "select * from dump_hotelbasicinfo ORDER BY id ASC LIMIT 5";
$resultTable = mysql_query($selTable,$conn);
while($rowTable = mysql_fetch_array($resultTable))
{

$selQuery = "SELECT * FROM dump_hotelamenities WHERE HotelCode='$rowTable[HotelCode]' LIMIT 5";
    $resultQuery = mysql_query($selQuery);
    while($row=mysql_fetch_array($resultQuery))
    {
     echo "<br> Hotel Code : " .$row['HotelCode'];
    $info = array();
    $info = (explode(';',$row['PAmenities']));
    echo array_search("YES",$info);
    echo "<br> Hotel Features : " ;
    print_r( $info);

     echo "<hr>";
    }
}
?>

输出:

Hotel Features : Array ( [0] => Small pets allowed under 5 kg [1] => YES Small pets allowed under 5 kg [2] => Large pets allowed over 5 kg [3] => YES Large pets allowed over 5 kg [4] => Wheelchair-accessible [5] => YES Wheelchair-accessible [6] => Car park [7] => YES Car park [8] => Garage [9] => YES Garage [10] => Mobile phone coverage [11] => Wired Internet [12] => Wi-fi [13] => Transfer service [14] => Secure parking [15] => Room service [16] => Laundry service [17] => Hotel safe [18] => Cloakroom [19] => Lift access [20] => Newspaper stand [21] => Supermarket [22] => Bicycle storage [23] => Sun terrace [24] => Gym [25] => Newspapers [26] => Restaurant [27] => Non-smoking area [28] => Photocopier [29] => Sun loungers [30] => Children& [31] => apos [32] => s play area [33] => TV lounge [34] => Sauna [35] => Massage [36] => Spa treatments [37] => Year of construction - 2008 [38] => Number of floors main building - 10 [39] => Apartments - 25 [40] => Studios - 1 [41] => Connecting rooms [42] => YES Connecting rooms [43] => Apartment complex [44] => Nearest Bus / Metro Stop - 25000 m [45] => Ski slopes - 2000 m )

3 个答案:

答案 0 :(得分:0)

每个循环的A都可以使用

foreach($info as $k=>$v)
    $info[$k] = str_replace('YES','',$v);

答案 1 :(得分:0)

<?php
    $array = array('this','is','a','test','yes it is','YES it is');
    $array = array_map(function($item){
        $item = str_replace('YES','', $item);
        return $item;
    }, $array);

    var_dump($array);

答案 2 :(得分:0)

function customSearch($keyword, $arrayToSearch){
    foreach($arrayToSearch as $key => $arrayItem){
        if( stristr( $arrayItem, $keyword ) ){
            $arrayToSearch[key] = "yes";
            return $key;
        }
    }
}

我还没有真正试过这个但是如果我的代码有点偏离,请参考这个问题: Search for PHP array element containing string