[英]PHP to store all variables from loop to use them later
I am trying to use some WSDL webservice to do some work with each parameters. 我正在尝试使用一些WSDL Web服务对每个参数进行一些工作。 This is working absolutely fine; 这绝对正常。 however, once the script is executed, I would like to have "log" emailed to me. 但是,一旦脚本执行完毕,我想通过电子邮件将“日志”发送给我。
All works fine when I use PRINT
, or ECHO
inside the loop (this will display all values of different variables from the loop). 当我在循环内使用PRINT
或ECHO
时,所有方法都可以正常工作(这将显示循环中不同变量的所有值)。 However, outside of the loop, this will only display ONE variable. 但是,在循环之外,这将仅显示一个变量。
Is there a way to store all variables into array inside of loop so this can be used later on, outside of loop, for example emailing this? 有没有一种方法可以将所有变量存储到循环内的数组中,以便以后可以在循环外使用,例如通过电子邮件发送?
This is what I have tried: 这是我尝试过的:
<?php
// API request and response
$requestParams = array(
'Request' => 'Hello'
);
$client = new SoapClient('https://example.com/webservice.asmx?WSDL');
$response = $client->Command($requestParams);
//Load response as XML
$xml = simplexml_load_string($response);
$rows = $xml->children('rs', TRUE)->data->children('z', TRUE)->row;
foreach ($rows as $row) {
$attributes = $row->attributes();
/* XML document contains two columns, first with attribute TO,
second with attribute Ref. This will extract required data */
$To = (string) $attributes->To;
$Ref= (string) $attributes->Ref;
// Here are few more lines in code to do some other work with each variable
// All works absolutely fine until this line
/* I would liket to store all variables so I can use them to email
them as a log in one email */
$ToLog .= "<br>$To</br>";
$RefLog .="<br>$Ref</br>";
}
$to = "nobody@example.com";
$subject = "Script successfully executed";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = $ToLog . $RefLog
mail($to, $subject, $message, $headers);
?>
try like this 这样尝试
$values = array();
foreach ($rows as $row) {
$values[] = $row->attributes(); //stores the each values to the array
}
print_r($values);
I think u just need to define both variable before starting the loop, like $ToLog = ""; $RefLog = ""; Then if you can put whatever in this variable in side the loop you will get it after the loop, You do not need to take an array.
do not need to take an array. 不需要采取数组。
Try something like this 试试这个
$finalArray = array();
foreach($rows as $row)
{
$finalArray[] = $row["someIndex"];
}
then finalArray should contains all variables from foreach :) 那么finalArray应该包含来自foreach的所有变量:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.