我只想从具有多于和少于3个字符的数组中获取单词列表,但是我该怎么做呢?

时间:2018-10-30 13:17:34

标签: php arrays algorithm structure

$name   = array('jake', 'rita', 'ali', 'addert', 'siryteee', 'skeueei', 'wsewwauie', 'aaaaweefio');
$vowels = array('a', 'e', 'i', 'o', 'u');
$massiv = [];
$vowel  = [];

for ($i = 0; $i < count($name); $i++) {

    $massiv[] = $name[$i];

    for ($j = 0; $j < count($vowels); $j++) {
        $vowel[] = $vowels[$j];
    }
}
if (count($massiv[$vowel] >= 3) {
    print_r($massiv);
}

1 个答案:

答案 0 :(得分:1)

您可以使用正则表达式计算元音的数量。

  

我只想从具有更多且   少于3个字符,但我该怎么做?

我假设您想要2个数组,这些数组包含一个名称列表,这些名称的元音多于3个,而另一个名称少于3个元音。注意-我故意省略了具有3个元音的名称,因为我不知道我应该把它们放在哪个列表中。您应该能够轻松地添加它们。

http://sandbox.onlinephpfunctions.com/code/69f838a4dca56f651a521854636212c545dddbb2

<?php
$names = array('jake', 'rita', 'ali', 'addert', 'siryteee', 'skeueei', 'wsewwauie', 'aaaaweefio');

$moreThanThree = [];
$lessThanThree = [];

foreach ($names as $name) {

    $count = preg_match_all('/[aeiou]/i', $name, $matches);

    if ($count > 3) {
        $moreThanThree[] = $name;
    }

    if ($count < 3) {
        $lessThanThree[] = $name;
    }
}

// You now have 2 arrays - $moreThanThree & $lessThanThree

var_dump($moreThanThree);
var_dump($lessThanThree);