-2

I have an array named $info. I want to replace string "YES" found wherever within the array with space or empty string.How can it be done?

Below is the code:

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

OUTPUT :

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 )
iBhavin
  • 1,261
  • 15
  • 30
  • show us what have you tried other than making this array ? this can be easily done by recursion, what all you need to do it go through each element of array and replace it – NullPoiиteя Aug 22 '15 at 06:11

3 Answers3

0

A for each loop would work

foreach($info as $k=>$v)
    $info[$k] = str_replace('YES','',$v);
Njuguna Mureithi
  • 3,506
  • 1
  • 21
  • 41
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);
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
0
function customSearch($keyword, $arrayToSearch){
    foreach($arrayToSearch as $key => $arrayItem){
        if( stristr( $arrayItem, $keyword ) ){
            $arrayToSearch[key] = "yes";
            return $key;
        }
    }
}

I haven't actually tried this but please Refer to this question here if my code is a bit off: Search for PHP array element containing string

Community
  • 1
  • 1
James111
  • 15,378
  • 15
  • 78
  • 121