简体   繁体   中英

PHP 'foreach' array concatenate echo

The following will echo "12334".

However, I would like for the "12334" to be placed in a variable say $wordNumValue. I know it might be a simple thing, but to me it is not. Any help is much appreciated.

<?php
$msg ="hello";
$arrEn = str_split($msg);
foreach ($arrEn as &$value) {
    if ($value == 'h') {
          echo "1";
        } elseif ($value == 'e') {
          echo "2";
        } elseif ($value == 'l') {
          echo "3";
        } elseif ($value == 'o') {
          echo "4";
        } else {
          echo 'NULL';
        } 
}
?>      
<?php
$msg ="hello";
$arrEn = str_split($msg);
$wordNumValue = "";
foreach ($arrEn as &$value) {
if ($value == 'h') {
      echo "1";
      $wordNumValue .= "1";
    } elseif ($value == 'e') {
      echo "2";
      $wordNumValue .= "2";
    } elseif ($value == 'l') {
      echo "3";
      $wordNumValue .= "3";
    } elseif ($value == 'o') {
      echo "4";
      $wordNumValue .= "4";
    } else {
      echo 'NULL';
    } 
}
?>     

Try this:

<?php
$msg ="hello";
$arrEn = str_split($msg);
$wordNumValue = '';
foreach ($arrEn as &$value) {
    if ($value == 'h') {
        echo "1";
        $wordNumValue .= "1";
    } elseif ($value == 'e') {
        echo "2";
        $wordNumValue .= "2";
    } elseif ($value == 'l') {
        echo "3";
        $wordNumValue .= "3";
    } elseif ($value == 'o') {
        echo "4";
        $wordNumValue .= "4";
    } else {
        echo 'NULL';
    }
}

But as you have many elseif s I, would do this instead:

<?php
$msg ="hello";
$arrEn = str_split($msg);
$wordNumValue = '';
foreach ($arrEn as &$value) {
    switch ($value) {
        case "h":
            $wordNumValue .= "1";
            break;
        case "e":
            $wordNumValue .= "2";
            break;
        case "l":
            $wordNumValue .= "3";
            break;
        case "o":
            $wordNumValue .= "4";
            break;
        default:
            echo 'NULL';
    }
}
echo $wordNumValue;

And finally I echoed $wordNumValue instead of echoing single numbers several times.

You can add each string onto a variable using the .= assignment operator;

<?php
$msg ="hello";
$arrEn = str_split($msg);
foreach ($arrEn as &$value) {
    if ($value == 'h') {
          $wordNumValue .= "1";
        } elseif ($value == 'e') {
          $wordNumValue .= "2";
        } elseif ($value == 'l') {
          $wordNumValue .= "3";
        } elseif ($value == 'o') {
          $wordNumValue .= "4";
        }
}
?>

You don't need the final else . See this, PHP: Assignment Operators , and this , PHP: String Operators .

Hope this helps.

You don't need to change your code , before foreach add ob_start(); and after foreach add $wordNumValue = ob_get_clean();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM