php数组迭代&连接 [英] php array iteration & concatenate

查看:64
本文介绍了php数组迭代&连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像下面的数组,我想检查下一个elem是否以空格开头,如果是,则将其连接到前一个elem。

I have an array that looks like the one below, I would like to check if the next elem starts with a space, if it does, concatenate it to the previous elem.

[1] => Array (
    [1] => lenny/volatile/main Packages
    [2] => lenny/volatile/main Packages
    [3] => lenny/volatile/main Sources
    [4] =>  Reading package
    [5] => lenny/volatile/main Sources
)

Output:

[1] => Array (
    [1] => lenny/volatile/main Packages
    [2] => lenny/volatile/main Packages
    [3] => lenny/volatile/main Sources Reading package
    [5] => lenny/volatile/main Sources
)

谢谢!

推荐答案

$count = count($array);
for($i = 0; $i < $count; $i++){
    if($array[$i][0] == ' '){
        if($i > 0){
             $array[$i-1] .= $array[$i];
             unset($array[$i])
        }
    }
}

应该这样做(也就是说,如果您的数组名为 $ array ,否则适合您的需要)

That should do it (that is if your array is named $array, otherwise suit it to your needs)

这篇关于php数组迭代&amp;连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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