【问题标题】:Php: want to print first two name of string array on first line and remaining names print on next linePhp:想在第一行打印字符串数组的前两个名称,其余名称在下一行打印
【发布时间】:2017-08-28 14:41:40
【问题描述】:

我想在第一行打印字符串数组的前两个名称,在下一行打印剩余的名称。

<?php 
$array = array("johnny bairstow alex hales jonh marush","Marcelo nash tim pane alexender","chaudary Mian 
Muhammad Aqeel shaib ");

for ($i=0; $i < count($array) ; $i++) { 
$len = strlen($array[$i]);
$string =  $array[$i];
for ($j = 8; $j < 9 ; $j++) { 
    $string[$j] ='\n';
}


echo $string."</br>";
 }
?>

输出应该是:

johnny doe
alex hales jonh marush
Marcelo Nash
tim pane alexender
chaudary Mian 
Muhammad  Aqeel shaib 

【问题讨论】:

  • johny doe 可能是 johny bairstow..
  • 如果数组中的第一个字符串只包含 3 个单词而不是 4 个单词,输出会是什么?

标签: php arrays string


【解决方案1】:

如果你确定数组中字符串的总数,你可以使用explode()函数

<?php 
$array = array("johnny doe alex hales","Marcelo nash tim pane","Mian 
Muhammad Aqeel shaib");

for ($i=0; $i < count($array) ; $i++) { 
    $len = strlen($array[$i]);
    $string =  explode(" ",  $array[$i]);
    echo $string[0] . " " . $string[1] . "<br>" .
    $string[2] . " " . $string[3] . "<br>";
}
?>

注意,如果不是数组中的4个字符串会返回错误

编辑:

执行此示例,您可以使用总共 3 个字符串而不会返回错误。我建议您使用此代码

<?php 
$array = array("johnny doe alex ","Marcelo nash tim pane","Mian 
Muhammad Aqeel shaib");

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

    $len = strlen($array[$i]); 
    $string =  explode(" ",  $array[$i]);

    for( $n=0; $n < count($string); $n++ ){

        echo $string[$n];
        echo ( $n & 1 ) ? '<br>' : ' ' ;

    }

 }
?>

【讨论】:

    【解决方案2】:

    另一种简短的解决方案:

    $data = ["johnny doe alex hales", "Marcelo nash tim pane", "Mian Muhammad Aqeel shaib"];
    
    // Make them all into one string and explode the values on space
    $names  = explode(' ', implode(' ', $data));
    
    for ($i = 0; $i < count($names); $i++) {
        // Echo the name for the index, add one to the index and echo the next one.
        echo $names[$i++] . ' ' . $names[$i] . '<br />';
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      <?php 
      $array = array("johnny doe alex hales","Marcelo nash tim pane","Mian 
                     Muhammad Aqeel shaib");
      
      foreach($array as $string) {        
      
          foreach(explode(' ', $string) as $index => $name) {
      
              $br_new_string = true;
      
              echo $name . ' ';
      
              if(1 == $index % 2) {           
                  echo '<br>';
      
                  $br_new_string = false;         
              }
          }
      
          echo $br_new_string ? '<br>' : '';
      }
      

      输出:

      johnny doe 
      alex hales 
      Marcelo nash 
      tim pane 
      Mian Muhammad 
      Aqeel shaib 
      

      【讨论】:

        【解决方案4】:

        你可以在字符串中找到第二个空格,然后用你想要的替换它:

        foreach($array as $string) 
        {
            $sndSpacePos = strpos($string, ' ', strpos($string, ' ')+1); //the offset for strpos is set right after the first space so it found the second one
            echo substr($string, 0, $sndSpacePos).'<br>'.substr($string, $sndSpacePos+1).'<br>'; //echo the concatenation of string parts before and after the second space with a <br> in the middle 
        } 
        

        它避免使用爆炸和第二个循环。

        【讨论】:

          【解决方案5】:
          <?php 
          $data = array(
              "johnny bairstow alex hales jonh marush",
              "Marcelo nash tim pane alexender",
              "chaudary Mian Muhammad Aqeel shaib"
          );
          
          $output = [];
          foreach($data as $names) {
              $first    = strtok($names, ' ');
              $second   = strtok(' ');
              $the_rest = strtok(null);
              $output[] = $first . ' ' . $second;
              $output[] = $the_rest;
          }
          var_export($output);
          

          输出:

          array (
            0 => 'johnny bairstow',
            1 => 'alex hales jonh marush',
            2 => 'Marcelo nash',
            3 => 'tim pane alexender',
            4 => 'chaudary Mian',
            5 => 'Muhammad Aqeel shaib',
          )
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-16
            • 2016-08-26
            • 1970-01-01
            • 2020-05-11
            • 2023-03-13
            • 1970-01-01
            相关资源
            最近更新 更多