簡體   English   中英

PHP存儲循環中的所有變量以供以后使用

[英]PHP to store all variables from loop to use them later

我正在嘗試使用一些WSDL Web服務對每個參數進行一些工作。 這絕對正常。 但是,一旦腳本執行完畢,我想通過電子郵件將“日志”發送給我。

當我在循環內使用PRINTECHO時,所有方法都可以正常工作(這將顯示循環中不同變量的所有值)。 但是,在循環之外,這將僅顯示一個變量。

有沒有一種方法可以將所有變量存儲到循環內的數組中,以便以后可以在循環外使用,例如通過電子郵件發送?

這是我嘗試過的:

<?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);

 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. 

不需要采取數組。

試試這個

$finalArray = array();
foreach($rows as $row)
{
   $finalArray[] = $row["someIndex"];
}

那么finalArray應該包含來自foreach的所有變量:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM