获取每个数组值的第一个字符串的PHP代码


PHP code to get the 1st string of each array value

我正在尝试创建一个程序,该程序允许将包含整数和字符串的数组传递给draw_stars((函数。当传递字符串时,根据下面的示例显示字符串的第一个字母,而不是显示*。

例如:

$x = array(4, "Tom", 1, "Michael", 5, 7, "Jimmy Smith");
draw_stars($x) should print the following on the screen/browser:

到目前为止,这是我的代码:

$y = array(4, "Tom", 1, "Michael", 5, 7, "Jimmy Smith");
function draw_stars2($ropes){ 
    foreach ($ropes as $rope) {
        echo str_repeat('*', $rope), '<br />'; 
    } 
}
$output2 = draw_stars2($y);
echo $output2;

知道吗?

开始:

$x = array(4, "Tom", 1, "Michael", 5, 7, "Jimmy Smith");
function draw_stars($array){
   foreach($array as $element){
      if(is_int($element)){
        echo str_repeat("*", $element);
      }
      else{
        echo strtolower(str_repeat(substr($element, 0,1), strlen($element)));
      }
      echo "<br>";
  }
}
draw_stars($x);

输出:

****
ttt
*
mmmmmmm
*****
*******
jjjjjjjjjjj