Hello, I have such a task:

There are two arrays, there are words in these arrays. The words in these arrays are recorded in a random order. It is necessary to combine these 2 arrays into one, so that in the new array, first go words that have a pair of vowels, and then all the other words.

For example: there is an array: $str = 'лама, мама, конь' And there is 2 array $str2 = 'клас, бабушка , клава' . It is necessary for such an array to turn out like this: (llama, mother, clave, horse, grandmother)

WHAT I came up with:

 <?php $str = 'Mother, Fther'; $strTwo = 'sister, brother'; $result = str_split($str); $resultTwo = str_split($strTwo); if(...) { $test = array_merge($result , $resultTwo); } ?> 

How to implement such a task in general, what should I use, what methods, how can I do this task without regularizers? I would be grateful for any help. Thank!

    3 answers 3

    Why use regulars here - I don't know, maybe the "specialists" will do it. And we will do without them. For example, this is how a function looks like that counts the number of vowels in a word:

     function count_letter($x) { $c = 0; for ($i = 0; $i < strlen($x); $i++) { if (strpos('euoaiy', $x[$i]) > -1) { $c+=1; } } return $c; } 

    There are Latin letters, of course, but this is an everyday matter. The code itself is built simply - we go through all the letters of the word and check if this letter is in a special word from some vowels. You could of course write a long if and check each letter, but leave it to the Hindus and mountain optimizers.

    Now the algorithm for solving the problem. We run over each word and if there is an even number of vowels, we write to an array of even numbers, otherwise, we write to an array for odd numbers. Then we do the same with the second array. And at the very end, these two arrays are simply “added”.

    How to check that the number of vowels is even? very simple - using taking a module - a% 2 will be 0 if the number is even. And then on your own :)

    • oyooy I'm confused already. I generally started to write my own code ?? and your phrase is not clear: We run over each word and if there is a pair of vowels there, we write to the first array, otherwise - to the second. Then we do the same with the second array. What does the first, second mean? I also need to put an array in one - M-Misha-M
    • aa understood you gave me a function as an example - M-Misha-M
    • @ M-Misha-M: Does your condition enforce such a pair: tool - two vowels in a row. - romeo
    • @romeo admits, the main thing is to have a double amount - M-Misha-M
    • I tried @KoVadim according to your algorithm, but what didn’t work out to unite, look, plz just want to figure it out for yourself. pastebin.com/m8N4ewUW - M-Misha-M

    You can argue otherwise.

    1. From the point of view of the final result, arrays are unordered, and their subsequent sorting is inevitable. Therefore, we immediately merge them into one (array_merge () function).
    2. Swap the keys and values ​​of the common array (function array_flip ()).
    3. Replace the element number with the array consisting of this number and the parity bit of the number of vowels in the word (array_walk () function).
      The number of vowels in the word counts the case-insensitive function str_ireplace () with the additional parameter $ count or its "regular equivalent" (thanks to KoVadim ).
      The parity bit is (count & 1)
    4. We sort by the uasort () function taking into account the original order and the parity bit.
    5. The ready array of keys is 'cut off' by the function array_keys ().

    Program:

     $array1 = array("Father", "Mother", "Sister", "Grandfather"); $array2 = array("Grandmother", "Brother", "Aunt"); $array12= array_merge($array1, $array2); var_dump($array12); $flip = array_flip($array12); array_walk($flip, function(&$item,$key) { // str_ireplace(array("a","e","i","o","u","y"), "", $key, $count); preg_replace("/[aeiouy]/i", "", $key, -1, $count); $item = array($item, $count & 1); }); uasort($flip, function($a,$b){ if(end($a)==end($b)) return reset($a)-reset($b); else return end($a) - end($b); }); $result = array_keys($flip); var_dump($result); 

    Results (array after merge and ready array):

     array (size = 7)
       0 => string 'Father' (length = 6)
       1 => string 'Mother' (length = 6)
       2 => string 'Sister' (length = 6)
       3 => string 'Grandfather' (length = 11)
       4 => string 'Grandmother' (length = 11)
       5 => string 'Brother' (length = 7)
       6 => string 'Aunt' (length = 4)
     array (size = 7)
       0 => string 'Father' (length = 6)
       1 => string 'Mother' (length = 6)
       2 => string 'Sister' (length = 6)
       3 => string 'Brother' (length = 7)
       4 => string 'Aunt' (length = 4)
       5 => string 'Grandfather' (length = 11)
       6 => string 'Grandmother' (length = 11)
    

      If without using 2 arrays (min. PHP version 5.4.0), then the function for getting the number of vowels is already described, then you can do this:

       $words1 = ['Mother', 'Grandfather', 'Brother', 'Sister']; $words2 = ['Uncle', 'Father', 'Grandmother']; $words = []; array_map(function ($word) use (&$words) { if (count_letter($word) % 2 === 0) { // если количество гласных букв - парное array_unshift($words, $word); // добавляем слово в начало массива } else { $words[] = $word; } }, array_merge($words1, $words2)); print_r($words); 
      • But why array_splice() , and not array_unshift() ? And warn topikstarter, in which versions of PHP it will work. - tutankhamun
      • @tutankhamun Thanks, corrected for array_unshift - JILeXanDR
      • @JILeXanDR, thank you very much, only this line is incomprehensible array_map (function ($ word) use (& $ words) - M-Misha-M
      • @ M-Misha-M In PHP, there is no access to external context in closures, and you can transfer the necessary variables using the use keyword. And the ampersand says that the variable will be passed by reference, otherwise any actions with it will be performed only locally. - JILeXanDR
      • Uncle will not work correctly. We need to fix strpos () on stripos () - Yuri Negometyanov