Steven Matthews
Steven Matthews

Reputation: 11285

How to modify an array's values by a foreach loop?

So I have this foreach loop - and I want to modify the array based on my modification of the values. However when I try to later convert $bizaddarray to a string, all of the HTML tags are still present. Here's my foreach loop - how can I make the strip tags permanent?

    foreach ($bizaddarray as $value) {
        strip_tags(ucwords(strtolower($value)));
    }

Upvotes: 6

Views: 1772

Answers (2)

Mike Purcell
Mike Purcell

Reputation: 19979

Two ways, you can alter the memory location shared by the current value directly, or access the value using the source array.

// Memory reference
foreach ($bizaddarray as &$value) {
    $value = strip_tags(ucwords(strtolower($value)));
}
unset($value); # remove the reference

Or

// Use source array
foreach ($bizaddarray as $key => $value) {
    $bizaddarray[$key] = strip_tags(ucwords(strtolower($value)));
}

Upvotes: 9

Logan Serman
Logan Serman

Reputation: 29870

foreach ($bizaddarray as $key => $value) {
    $bizaddarray[$key] = ucwords(strtolower($value));
}

Upvotes: 0

Related Questions