【问题标题】:Placing SQL query results into one array将 SQL 查询结果放入一个数组
【发布时间】:2017-06-01 07:01:54
【问题描述】:

我有这个 PhP 脚本:

 <?php

   $gregorianMonth = date(n);
   $gregorianDay = date(j);
   $gregorianYear = date(Y);

   $jdDate = gregoriantojd($gregorianMonth,$gregorianDay,$gregorianYear); 

   $hebrewMonthName = jdmonthname($jdDate,4);

   $hebrewDate = jdtojewish($jdDate); 

   list($hebrewMonth, $hebrewDay, $hebrewYear) = split('/',$hebrewDate);
  ?>

 <?php
   $table_name =  "candle_number";
   $data = $wpdb->get_results( "SELECT email FROM `{$table_name}` WHERE `datehebrew` LIKE '%%$hebrewDay%%' AND `datehebrew` LIKE '%%$hebrewMonth%%' AND date_pref = \"hebrew\"");

   foreach( $data as $rs ){
     echo "
       <p>{$rs->email}</p>
        ";hat I can 
       $i++;
    }
 ?>

我最初的想法是使用 foreach 循环和 CURL Mailchimp API 将每封电子邮件发送到我的端点,有点像一次性。

但是 - 我知道 foreach 可能不会循环进程并且它不是很干净。我认为更好的解决方案是将每个结果放入一个数组中,然后在 curl 中发送那个数组,因为 MC API 会接受它。

虽然我还是个新手,所以这就是我卡住的地方。那么如何从查询中获取这些结果并将它们放入一个字符串中呢?

【问题讨论】:

  • 添加到数组是使用[]array_push 完成的。请按照手册进行操作。

标签: php arrays wordpress curl


【解决方案1】:

试试:

//create blank array
$final_output = array();

foreach( $data as $rs ){

   //add data to array in next index.
   final_output[] = $rs->email;
   $i++;

}

//at this point send your data array wherever you want.
print_r($final_output);

如果要将数据转换为字符串,请使用:

echo json_encode($final_output);

【讨论】:

  • 其实我不知道为什么这得到了反对票。完美运行。
  • 不知道是谁干的。不过谢谢。它会完美运行。
  • 我喜欢你的短臂。 Humblebrag :) 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 2012-08-26
  • 2021-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多