0

Let's say I have an associative array like this one:

PHP

   $array = array(
       'Corfù' => 'pita gyros',
       'San Gennaro'  => 'pizza',
       'Perù' => 'tortillias'
   );

How can I access values of this array through a sanitized key in php like this? (possibly without loops)

PHP

echo $array['corfu'];
// pita gyros

echo $array['san-gennaro'];
// pizza

instead of this

PHP

echo $array['Corfù'];
// pita gyros

This is because I'm taking the key from the url of a related page, that obviously is missing accents and white spaces

silvered.dragon
  • 407
  • 1
  • 7
  • 19
  • 1
    You cannot do this without a loop, or an implicit loop. With a loop it is relatively easy. – KIKO Software Mar 02 '21 at 12:19
  • 1
    https://stackoverflow.com/questions/5330044/php-case-and-accent-insensitive-search-into-an-array – Nigel Ren Mar 02 '21 at 12:21
  • 2
    _Why_ are your array keys like this to begin with? Are you intending to use them for output to the end user in the frontend at some point? If not, it would probably make much more sense, that you used keys that actually match the values you get passed from the outside in the first place. – CBroe Mar 02 '21 at 12:22
  • @CBroe yes I need to pass the keys from the backend to the frontend, this is for populate the title of the page and some tables, so I need to render the correct word. Maybe I can use a key like corfu and add an additional value inside like "render" => "Corfù", but I think that this is a little annoying. – silvered.dragon Mar 02 '21 at 13:19
  • 2
    _“but I think that this is a little annoying”_ - No, that is rather how you _should_ be doing this. Separate actual _logic_, from how you need stuff to _display_. Otherwise, you’ll get into even bigger trouble, when what the front-end needs to show changes at some point, or when you might want to add a second language. – CBroe Mar 02 '21 at 13:20

1 Answers1

0
function seola($s)
{
    $tr = array('ş', 'Ş', 'ı', 'İ', 'ğ', 'Ğ', 'ü', 'Ü', 'ö', 'Ö', 'Ç', 'ç','ù');
    $eng = array('s', 's', 'i', 'i', 'g', 'g', 'u', 'u', 'o', 'o', 'c', 'c', 'y','u');
    $s = str_replace($tr, $eng, $s);
    $s = strtolower($s);
    $s = preg_replace('/&.+?;/', '', $s);
    $s = preg_replace('/[^%a-z0-9 \/_-]/', '', $s);
    $s = preg_replace('/\s+/', '-', $s);
    $s = preg_replace('|-+|', '-', $s);
    $s = trim($s, '-');
    $s = substr($s, 0, 60);
    return $s;
}

  $array = array(
            'Corfù' => 'pita gyros',
            'San Gennaro' => 'pizza',
            'Perù' => 'tortillias'
        );

       foreach ($array as $key=>$value){
           $array[seola($key)] = $array[$key];
           unset($array[$key]);
       }
print_r($array);
/*
Array
(
    [corfu] => pita gyros
    [san-gennaro] => pizza
    [peru] => tortillias
)
*/

its look like more useful //

  • Hi Berkan Kaya, Can you explain your code and how it solves the question? – bsheps Mar 02 '21 at 13:06
  • this solution works, but i thought there was some sort of php inbuild function to solve the issue without loops – silvered.dragon Mar 02 '21 at 13:21
  • 1
    Unfortunately :( if have be another method i don't know, keep going to research. By the way why dont you use to the loop @silvered.dragon –  Mar 02 '21 at 14:09
  • 1
    there is nothing to explain. I just edit to key using seola function for more useful. @bsheps –  Mar 02 '21 at 14:12
  • @BerkanKaya yes you are right.. it's just because this becomes really complex if I have deep nested arrays(and this is my final case), I have to loop through each level – silvered.dragon Mar 02 '21 at 15:20
  • 1
    @silvered.dragon Yes little bit :( i'm sorry for you. But its look like only this solution solve this issue for now –  Mar 02 '21 at 22:20
  • 1
    @silvered.dragon But i will do added to solution if you want to the nested array too –  Mar 02 '21 at 22:24