Search code examples
phparraysstripslashes

Stripslashes() on array


I have some problems using stripslashes() on array.

Here is my array :

$tabRegion = array(
1=>"Alsace",
2=>"Aquitaine",
3=>"Auvergne",
4=>"Basse-Normandie",
5=>"Bourgogne",
6=>"Bretagne",
7=>"Centre",
8=>"Champagne-Ardenne",
9=>"Corse",
10=>"Franche-Comté",
(...)
21=>"Provence-Alpes-Côte d'Azur",
22=>"Rhône-Alpes",);

In order to stripslash, I have adapted this PHP code :

foreach ($tabRegion as $key=>$region) {
$tabRegion[$key] = stripslashes($region);
}

After in the file, I generate URL with it for example :

if (file_exists('../region/$tabRegion[$region]/$fonction/messages/$lecturefichier (...)

But the fact is that the last value of the array is always selected ("Rhône-Alpes") by the code... I don't know why.

Do you have an idea? :)

Thank you !


Solution

  • You are using foreach loop then you have to generate url in that loop. In that loop you will get each region value $tabRegion = array( 1=>"Alsace", 2=>"Aquitaine", 3=>"Auvergne", 4=>"Basse-Normandie", 5=>"Bourgogne", 6=>"Bretagne", 7=>"Centre", 8=>"Champagne-Ardenne", 9=>"Corse");

    foreach ($tabRegion as $key=>$region) 
    {
    
     $tabRegion[$key] = stripslashes($region);
     print "<br>".$region;
    
    }
    

    Output will be : Alsace
    Aquitaine
    Auvergne
    Basse-Normandie
    Bourgogne
    Bretagne
    Centre
    Champagne-Ardenne
    Corse

    So that,you have to insert following line in that for loop : if (file_exists('../region/$tabRegion[$region]/$fonction/messages/$lecturefichier (...)