How Can I Pull An Entire Result From A Foreach Loop

stevieD

I want to get the result from a foreach loop, but use it outside of the foreach brackets. What is the easiest way to do this.

Currently the below is working fine:

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

However, I would like to have this so I can add text to it (echo is outside of foreach brackets):

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

Currently if I do the second example, it is only returning the final record in the array.

Any help would be greatly appreciated.

Vick
$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

P.S. Despite there is a lot examples of global variables usage in the manual, you actually should not use it as it is considered as a bad practice.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I add routing to entire different(from gw) subnet?

분류에서Dev

How can I foreach a hashmap?

분류에서Dev

How can I trace out entire GET request from jersey api client call

분류에서Dev

How can I download an entire playlist from youtube even it exceeds 25 videos?

분류에서Dev

How can I write an if statement using a variable in a foreach loop using Powershell?

분류에서Dev

How can I loop this script from 1 to 50 in SPSS?

분류에서Dev

How can I get the tkinter entry from a loop

분류에서Dev

How can i fill a C# listview by using foreach loop a column and another foreach for second column so that 2nd loop do not over write first

분류에서Dev

How can i make the div clickable with entire background image?

분류에서Dev

How can I output an entire git repo to a single file?

분류에서Dev

How can I delete entire row in my SQL datagridview?

분류에서Dev

How can I clone an entire drive (or partition) to a smaller drive (or partition)?

분류에서Dev

How can i get the result from DB if one column have several keywrods?

분류에서Dev

tcsh: how can we display the body of a foreach loop in history

분류에서Dev

How do I pull all image links from a Wikimedia page?

분류에서Dev

How to pass a variable generated from foreach loop to another page in PHP

분류에서Dev

How can I break a symbolic link loop?

분류에서Dev

How can I iterate through a function with for loop?

분류에서Dev

How can I resolve this loop in c#?

분류에서Dev

How can I get the result in the UI (@escaping , completion)

분류에서Dev

How can I intercept the result of openDocument before NSDocument loads?

분류에서Dev

How can I print the result of add function..?

분류에서Dev

How can I choose a function at compile time based on a constexpr result

분류에서Dev

How can I choose a function at compile time based on a constexpr result

분류에서Dev

sqlalchemy - how can I get update method result?

분류에서Dev

How can I Insert the result of a ".w" evaluation at the end of a line in Vim?

분류에서Dev

How can I get this result in RegEx using Javascript

분류에서Dev

perl foreach array result from a database

분류에서Dev

How can I pause a loop until I get a users input?

Related 관련 기사

  1. 1

    How can I add routing to entire different(from gw) subnet?

  2. 2

    How can I foreach a hashmap?

  3. 3

    How can I trace out entire GET request from jersey api client call

  4. 4

    How can I download an entire playlist from youtube even it exceeds 25 videos?

  5. 5

    How can I write an if statement using a variable in a foreach loop using Powershell?

  6. 6

    How can I loop this script from 1 to 50 in SPSS?

  7. 7

    How can I get the tkinter entry from a loop

  8. 8

    How can i fill a C# listview by using foreach loop a column and another foreach for second column so that 2nd loop do not over write first

  9. 9

    How can i make the div clickable with entire background image?

  10. 10

    How can I output an entire git repo to a single file?

  11. 11

    How can I delete entire row in my SQL datagridview?

  12. 12

    How can I clone an entire drive (or partition) to a smaller drive (or partition)?

  13. 13

    How can i get the result from DB if one column have several keywrods?

  14. 14

    tcsh: how can we display the body of a foreach loop in history

  15. 15

    How do I pull all image links from a Wikimedia page?

  16. 16

    How to pass a variable generated from foreach loop to another page in PHP

  17. 17

    How can I break a symbolic link loop?

  18. 18

    How can I iterate through a function with for loop?

  19. 19

    How can I resolve this loop in c#?

  20. 20

    How can I get the result in the UI (@escaping , completion)

  21. 21

    How can I intercept the result of openDocument before NSDocument loads?

  22. 22

    How can I print the result of add function..?

  23. 23

    How can I choose a function at compile time based on a constexpr result

  24. 24

    How can I choose a function at compile time based on a constexpr result

  25. 25

    sqlalchemy - how can I get update method result?

  26. 26

    How can I Insert the result of a ".w" evaluation at the end of a line in Vim?

  27. 27

    How can I get this result in RegEx using Javascript

  28. 28

    perl foreach array result from a database

  29. 29

    How can I pause a loop until I get a users input?

뜨겁다태그

보관