将字符串添加到数组中除最后一项以外的所有项 [英] adding string to all but last item in array

查看:66
本文介绍了将字符串添加到数组中除最后一项以外的所有项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,除了最后一个项目,我想向数组中的每个项目添加一个字符串。

I have an array and I'd like to add a string to each item in the array, apart from the last item.

任何想法我都会

谢谢

推荐答案

数字索引数组和关联数组:

This should do it for both numerically-indexed arrays and associative arrays:

$i = 0;
$c = count($array);

foreach ($array as $key => $val) {
    if ($i++ < $c - 1) {
        $array[$key] .= 'string';
    }
}

这篇关于将字符串添加到数组中除最后一项以外的所有项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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