如何通过 foreach 循环修改数组的值?

2021-12-26 00:00:00 foreach php

所以我有这个 foreach 循环 - 我想根据我对值的修改来修改数组.但是,当我稍后尝试将 $bizaddarray 转换为字符串时,所有 HTML 标记仍然存在.这是我的 foreach 循环 - 如何使条带标签永久化?

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)));
    }

推荐答案

两种方式,可以直接改变当前值共享的内存位置,或者使用源数组访问该值.

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

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

相关文章