PHP 存储循环中的所有变量以备后用
PHP to store all variables from loop to use them later
我正在尝试使用一些 WSDL 网络服务来处理每个参数。这工作得很好;然而,一旦脚本被执行,我希望 "log" 通过电子邮件发送给我。
当我在循环中使用 PRINT
或 ECHO
时一切正常(这将显示循环中不同变量的所有值)。然而,在循环之外,这只会显示一个变量。
有没有办法将所有变量存储到循环内的数组中,以便稍后在循环外使用,例如通过电子邮件发送?
这是我试过的:
<?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);
?>
这样试试
$values = array();
foreach ($rows as $row) {
$values[] = $row->attributes(); //stores the each values to the array
}
print_r($values);
尝试这样的事情
$finalArray = array();
foreach($rows as $row)
{
$finalArray[] = $row["someIndex"];
}
那么 finalArray 应该包含来自 foreach 的所有变量:)
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.
不需要取数组。
我正在尝试使用一些 WSDL 网络服务来处理每个参数。这工作得很好;然而,一旦脚本被执行,我希望 "log" 通过电子邮件发送给我。
当我在循环中使用 PRINT
或 ECHO
时一切正常(这将显示循环中不同变量的所有值)。然而,在循环之外,这只会显示一个变量。
有没有办法将所有变量存储到循环内的数组中,以便稍后在循环外使用,例如通过电子邮件发送?
这是我试过的:
<?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);
?>
这样试试
$values = array();
foreach ($rows as $row) {
$values[] = $row->attributes(); //stores the each values to the array
}
print_r($values);
尝试这样的事情
$finalArray = array();
foreach($rows as $row)
{
$finalArray[] = $row["someIndex"];
}
那么 finalArray 应该包含来自 foreach 的所有变量:)
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.
不需要取数组。