0

i have an array named $data

$data = array(
                    'word1'=>'Šššš',
                    'word2'=>'Zzzzžzzž',
                    'word3'=>'Āāāa'
                    );

what i want to do is to create new array with the same keys but with different values, so my new array would look like this:

$newdata = array(
                    'word1'=>'Ssss',
                    'word2'=>'Zzzzzzzz',
                    'word3'=>'Aaaa'
                    ); 

all i want to do is to replace letters with diacritical mark to normal letter, i have searched for php functions and i have understood that i could use str_replace function like this:

$search  = array('Ā', 'ā', 'Š', 'š', 'Ž', 'ž');
$replace = array('A', 'a', 'S', 's', 'Ž', ž);
str_replace($search, $replace, $data);

but how can i loop through array checking and replacing values without touching keys? or i cannot do that?(all data have to be be checked)

1

6 Answers 6

1

Try this

function splReplace($string)
{
    $search  = array('Ā', 'ā', 'Š', 'š', 'Ž', 'ž');
    $replace = array('A', 'a', 'S', 's', 'Ž', ž);
    return str_replace($search, $replace, $string);
}

$data = array(
                'word1'=>'Šššš',
                'word2'=>'Zzzzžzzž',
                'word3'=>'Āāāa'
                );

$data = array_map('splReplace',$data)   ;
Sign up to request clarification or add additional context in comments.

1 Comment

str_replace() does not require a loop. It will happily traverse a 1d array.
1

The third parameter, $subject for the str_replace function can be an array. So no need to loop through the values yourself. Tested on php 5.5, it'll keep your keys and values intact

$search  = array('Ā', 'ā', 'Š', 'š', 'Ž', 'ž');
$replace = array('A', 'a', 'S', 's', 'Z', 'z');

$data = array(
            'word1'=>'Šššš',
            'word2'=>'Zzzzžzzž',
            'word3'=>'Āāāa'
);

$data = str_replace($search, $replace, $data);

Comments

0

try like this:

 $data = array(
    'word1'=>'Šššš',
    'word2'=>'Zzzzžzzž',
    'word3'=>'Āāāa'
);

$search  = array('Ā', 'ā', 'Š', 'š', 'Ž', 'ž');
$replace = array('A', 'a', 'S', 's', 'Ž', 'ž');
$data2 = array();
foreach($data as $k =>$v){
    $data2[$k] = str_replace($search, $replace, $data[$k]);
}
print_r($data2);

you can change the line inside foreach by this:

$data2[$k] = str_replace($search, $replace, $v);

demo

5 Comments

check out the demo @Xatenev
Ah i see, why is it acting like this? Normally [] is creating a new element. In my thoughts - you would override $data2['KEY'] with each element again?
Not from me .. anyways.. Can you give an answer on my question?^^
it is new array $data2
str_replace() does not require a loop. It will happily traverse a 1d array.
0

with a simple foreach

<?php
$search  = array('Ā', 'ā', 'Š', 'š', 'Ž', 'ž');
$replace = array('A', 'a', 'S', 's', 'Ž', ž);
foreach($data as $key => $value){
    $data[$key] = str_replace($search, $replace, $data[$key]);
}
?>

1 Comment

str_replace() does not require a loop. It will happily traverse a 1d array.
0

A foreach works well

    $data = array(
                'word1'=>'Šššš',
                'word2'=>'Zzzzžzzž',
                'word3'=>'Āāāa'
                );

    $search  = array('Ā', 'ā', 'Š', 'š', 'Ž', 'ž');
    $replace = array('A', 'a', 'S', 's', 'Ž', ž);      

    foreach($data as $k => $v) { // for each element in $data
        $data[$k] = str_replace($search, $replace, $v); // do replacement and assign back to the array.
    }

    print "<pre>"; print_r($data); print "</pre>"; // print formatted array

1 Comment

str_replace() does not require a loop. It will happily traverse a 1d array.
0

To answer your question you can use a foreach with a reference:

$search  = array('Ā', 'ā', 'Š', 'š');
$replace = array('A', 'a', 'S', 's');
foreach ($data as &$word) {
    $word = str_replace($search, $replace, $word);
}

However, an easier way would to use a foreach with normalizer_normalize:

foreach ($data as &$word) {
    $word = normalizer_normalize($word);
}

or using array_map:

$data = array_map('normalizer_normalize', $data);

Please note that normalizer_normalize was added to the core in PHP 5.3, but in most linux distributions it isn't active by default.

You can also try out iconv():

foreach ($data as &$word) {
    $word = iconv('UTF-8', 'ASCII//TRANSLIT', $word);
}

1 Comment

str_replace() does not require a loop. It will happily traverse a 1d array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.