如何通过 foreach 循环修改数组的值? [英] How to modify an array's values by a foreach loop?

查看:24
本文介绍了如何通过 foreach 循环修改数组的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个 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)));
}

这篇关于如何通过 foreach 循环修改数组的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆