Randy
Randy

Reputation: 15

How do I make a FOR loop into a variable?

I see that this type of question has been asked before, but I can't get their use/answers to work in my situation.

I have a set of checkboxes with the values of A,B,C,D, etc. (ex value="a").

And what I want is to create a callable variable based on those choices. So the result may be ABC or ACD, or CD, etc.

Here is what I have so far. I can echo $upgrade[$i] and it lists out correctly, but if I echo/call $result it only does the last one processed (eg. "c" - not "abc")

for ($i=0; $i < $N; $i++)
  {
    $result = $upgrade[$i];
    echo "$result";
  }
echo "$upgrade[$i]"; // outputs correctly ("abc")
echo "result"; // only outputs last line ("c")

I'm very new to php so explanation would be helpful. Thanks!

Upvotes: 0

Views: 91

Answers (2)

user257319
user257319

Reputation:

do you mean implement an iterator, it is possible but heavy on the DOM. better use controllable loops instead of for,

for example this is how you can take "NodeList" and use the forEach function from Array's prototype, also map, filter, some,... act the same.

Array.prototype.forEach.call(document.querySelectorAll('.myclass,a[href],div[style*="red"]'), function(element, index, whole_array){

  console.log(arguments);

});

Upvotes: 0

unknown
unknown

Reputation: 110

In case I understood your question correctly, you want to concatenate values into $result variable, to do so:

for ($i=0; $i < $N; $i++)
  {
    $result .= $upgrade[$i];
    echo "$result";
  }

The following sentence:

$result .= $upgrade[$i];

Is equivalent to:

$result = $result.$upgrade[$i];

Upvotes: 2

Related Questions