简体   繁体   English

PHP存储循环中的所有变量以供以后使用

[英]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). 当我在循环内使用PRINTECHO时,所有方法都可以正常工作(这将显示循环中不同变量的所有值)。 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.

相关问题 prestashop(php / SESSION变量)如何存储表单中的变量并在if / else命令中使用它们 - prestashop (php / SESSION variables ) how to store variables from a form and use them in if / else commands 缓存PHP变量以备后用 - Caching PHP variables for later use 将myPHP数据库中的所有变量查询到数组中,以便以后在HTML中使用 - Query all variables from myPHP db into array for later use in HTML 存储来自 SQL 表的变量,以便稍后使用 PHP - Store variable from SQL Table to use later using PHP 查询mysql表中具有相同月份值的行,并存储为php数组,以备后用在日历上显示它们 - query a mysql table for rows that have the same month value and store as a php array for later use displaying them on a calendar 将PHP类设置存储在变量中或从方法中返回它们 - Store PHP class settings in variables or return them from methods 如何存储PHP Trie以供以后使用? - How to store PHP Trie for all later uses? 将函数结果存储到变量以供以后在PHP中使用? - Store function result to variable for later use in PHP? PHP:将数组输出存储在变量中,以备后用 - PHP: Store array output in a variable for later use 如何从数据库循环中获取值并将其加起来以备后用 - How to get values from database loop and add them up for later use
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM