Voting

The Note You're Voting On

tezcatl at fedoraproject dot org
6 years ago
Each of the arguments required by the function, must be in the array supplied in the third argument to iterator_apply. You can use references too. Example:

<?php

function translate_to(string $target_language, Iterator $words, array $dictionaries) {

$language = ucfirst($target_language);
$dictionary = $dictionaries[$target_language] ?? 'not found';

if (
$dictionary === 'not found') {
echo
"Not found dictionary for {$language}\n";
return;
}

echo
"English words translated to {$language}\n";

$not_found = [];

iterator_apply($words, function($words, $dictionary, &$not_found){

$english_word = $words->current();

$translated_word = $dictionary[$english_word] ?? '';

if (
$translated_word !== '') {
echo
"{$english_word} translates to {$translated_word}\n";
} else {
$not_found[] = $english_word;
}

return
true;

}, array(
$words, $dictionary, &$not_found));

echo
"\nNot found words:\n" . implode("\n", $not_found) . "\n";
}

$dictionaries = [
'nahuatl' => [
'one' => 'Ze',
'two' => 'Ome',
'three' => 'Yei',
'four' => 'Nahui',
],
];

$iterator = new \ArrayIterator(array('one', 'two', 'three', 'four', 'gasoil'));

translate_to('nahuatl', $iterator, $dictionaries);
?>

English words translated to Nahuatl
one translates to Ze
two translates to Ome
three translates to Yei
four translates to Nahui

Not found words:
gasoil

<< Back to user notes page

To Top