簡體   English   中英

如何從Foreach循環中提取整個結果

[英]How Can I Pull An Entire Result From A Foreach Loop

我想從foreach循環中獲取結果,但請在foreach括號之外使用它。 什么是最簡單的方法。

目前,下面的工作正常:

foreach($esmc->find('p') as $paragraph){
$showparag = $paragraph->innertext. '<br/>';
echo $showparag;//Will show result of array
}

但是,我想要這個,以便可以向其中添加文本(echo在foreach括號之外):

foreach($esmc->find('p') as $paragraph){
$showparag = $paragraph->innertext. '<br/>';
}
echo "This shows results of array as $showparag";//Contains text + array

當前,如果我執行第二個示例,它將僅返回數組中的最終記錄。

任何幫助將不勝感激。

$string = 'This shows results of array as ';
foreach($esmc->find('p') as $paragraph){
    $showparag = $paragraph->innertext. '<br/>';
    $string .= $showparag;
}
echo $string;

http://php.net/manual/en/language.variables.scope.php

PS盡管手冊中有很多使用全局變量的示例,但是您實際上不應該使用它,因為它被認為是一種不好的做法。

您的問題比較模糊,如果這不是您想要的,請您在評論中加以說明。

但是,這應該生成一個字符串,供您在foreach循環之外將所有結果打印為一個字符串。

$showparag
foreach($esmc->find('p') as $paragraph){
    $showparag .= $paragraph->innertext. '<br/>';
}
echo "This shows results of array as $showparag";//Contains text + array

暫無
暫無

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

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