Reputation: 33
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)
Upvotes: 0
Views: 1558
Reputation: 1086
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);
Upvotes: 1
Reputation: 26
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);
}
Upvotes: 0
Reputation: 83
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
Upvotes: 0
Reputation: 633
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) ;
Upvotes: 1
Reputation: 2048
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]);
}
?>
Upvotes: 0
Reputation: 9351
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);
Upvotes: 0