<small id='bl5gp'></small><noframes id='bl5gp'>

    <tfoot id='bl5gp'></tfoot>
      • <bdo id='bl5gp'></bdo><ul id='bl5gp'></ul>
    1. <legend id='bl5gp'><style id='bl5gp'><dir id='bl5gp'><q id='bl5gp'></q></dir></style></legend>

        <i id='bl5gp'><tr id='bl5gp'><dt id='bl5gp'><q id='bl5gp'><span id='bl5gp'><b id='bl5gp'><form id='bl5gp'><ins id='bl5gp'></ins><ul id='bl5gp'></ul><sub id='bl5gp'></sub></form><legend id='bl5gp'></legend><bdo id='bl5gp'><pre id='bl5gp'><center id='bl5gp'></center></pre></bdo></b><th id='bl5gp'></th></span></q></dt></tr></i><div id='bl5gp'><tfoot id='bl5gp'></tfoot><dl id='bl5gp'><fieldset id='bl5gp'></fieldset></dl></div>

        用数字对替换字符串中的数字求和

        时间:2023-12-01

      1. <legend id='1u7Hh'><style id='1u7Hh'><dir id='1u7Hh'><q id='1u7Hh'></q></dir></style></legend>

              <tbody id='1u7Hh'></tbody>

            <small id='1u7Hh'></small><noframes id='1u7Hh'>

            <tfoot id='1u7Hh'></tfoot>
            • <bdo id='1u7Hh'></bdo><ul id='1u7Hh'></ul>

                  <i id='1u7Hh'><tr id='1u7Hh'><dt id='1u7Hh'><q id='1u7Hh'><span id='1u7Hh'><b id='1u7Hh'><form id='1u7Hh'><ins id='1u7Hh'></ins><ul id='1u7Hh'></ul><sub id='1u7Hh'></sub></form><legend id='1u7Hh'></legend><bdo id='1u7Hh'><pre id='1u7Hh'><center id='1u7Hh'></center></pre></bdo></b><th id='1u7Hh'></th></span></q></dt></tr></i><div id='1u7Hh'><tfoot id='1u7Hh'></tfoot><dl id='1u7Hh'><fieldset id='1u7Hh'></fieldset></dl></div>
                  本文介绍了用数字对替换字符串中的数字求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有这两个数组

                     $letters = array('a','b','c', 'd', 'e');
                     $replace = array( 1,  5,  10, 15 , 20);
                     $text = "abd cde dee ae d" ;
                     $re = str_replace($letters, $replace, $text) ;
                     echo $re ;  //this output:
                  
                       1515 101520 152020 120 15 
                  

                  现在我想将每个单词的上述数字相加,结果应该是这样的:

                  Now i want sum the above numbers for each word and the result should be like that:

                       21 45 55 21 15
                  

                  我尝试的是:

                      $resultArray = explode(" ", $re); 
                  
                      echo array_sum($resultArray).'<br />' ; // but it output wrong result. 
                                                             // it output this : 255190
                  

                  我怎样才能做到这一点?

                  how can i achieve this ?

                  非常感谢任何帮助.

                  像这样的阿拉伯字母

                     $letters = array('', '','','' ) ;
                     $replace = array(1, 5, 10, 15 ) ;
                     $text = "  "; 
                  

                  推荐答案

                  将字符串转换为数组并使用 array_sum.

                  Convert the string into an array and use array_sum.

                  array_sum(explode(' ', $re));
                  

                  编辑

                  对不起,误会了:

                  $letters = array('a','b','c', 'd', 'e');
                  
                  $replace = array( 1,  5,  10, 15 , 20);
                  
                  $text = "abd cde dee ae d" ;
                  
                  $new_array = explode(' ', $text);
                  
                  $sum_array = array();
                  
                  foreach ($new_array as $string)
                  {
                  
                    $nums = str_split($string);
                  
                    foreach ($nums as &$num)
                    {
                      $num = str_replace($letters, $replace, $num);
                    }
                  
                    $sum_array[] = array_sum($nums);
                  
                  }
                  
                  echo implode(' ', $sum_array);
                  

                  这篇关于用数字对替换字符串中的数字求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:MySQL SUM 查询问题 php 下一篇:如何对这个数组求和?

                  相关文章

                    <bdo id='TXcjO'></bdo><ul id='TXcjO'></ul>
                1. <legend id='TXcjO'><style id='TXcjO'><dir id='TXcjO'><q id='TXcjO'></q></dir></style></legend>
                2. <i id='TXcjO'><tr id='TXcjO'><dt id='TXcjO'><q id='TXcjO'><span id='TXcjO'><b id='TXcjO'><form id='TXcjO'><ins id='TXcjO'></ins><ul id='TXcjO'></ul><sub id='TXcjO'></sub></form><legend id='TXcjO'></legend><bdo id='TXcjO'><pre id='TXcjO'><center id='TXcjO'></center></pre></bdo></b><th id='TXcjO'></th></span></q></dt></tr></i><div id='TXcjO'><tfoot id='TXcjO'></tfoot><dl id='TXcjO'><fieldset id='TXcjO'></fieldset></dl></div>

                3. <small id='TXcjO'></small><noframes id='TXcjO'>

                    <tfoot id='TXcjO'></tfoot>