将循环添加到数组

时间:2012-10-26 08:37:50

标签: arrays loops coldfusion coldfusion-8

我正在尝试将循环结果添加到数组中。

这是我的代码:

  <cfset mobNumbers = ArrayNew(1)>

<cfloop query = "staffLoop">
    <cfscript>

      mobileNo = staffLoop.mobileno;
      mobileNo = mobileNo.replaceAll("[^0-9]", "");
      ext = staffLoop.extension;
      ext = ext.replaceAll("[^0-9]", "");

      if (Left(mobileNo, 2) == "07" || Left(mobileNo, 3) == "447") {
        recipient = mobileNo;
      } else if (Left(ext, 2) == "07" || Left(ext, 3) == "447") {
        recipient = ext;
      } else {
        recipient = "";
      }

      if (Left(recipient, 2) == "07") {
        recipient = "447" & Right(recipient, Len(recipient) - 2);
      }

      if (Len(recipient) == 12) {

       [send text code]

      }
    </cfscript>
  </cfloop>
<cfset ArrayAppend(mobNumbers, "recipient")>

目标是获得所有手机号码的数组。

我的代码无效,我经过一些研究,我不知道该怎么做。有什么想法吗?

如果可能的话,我想在我的解决方案中使用非cfscript,但如果使用cfscript更容易,那很好。

2 个答案:

答案 0 :(得分:4)

正如Adam指出的那样,ArrayAppend需要在循环中。您还需要在调用ArrayAppend时删除“收件人”周围的引号,否则您将拥有String“收件人”的数组。

答案 1 :(得分:1)

你的arrayAppend()需要循环中,否则你只是在循环结束后附加最后一个结果。

相关问题