开发者

PHP: simple array operation

I have an array like this (one dimension only):

$arr = array('one', 'two', 'three', 'foo', 'bar', 'etc');

Now I need a for() loop that creates a new 开发者_开发知识库array from $arr, like that:

$newArr = array('one', 'onetwo', 'onetwothree', 'onetwothreefoo', 'onetwothreefoobar', 'onetwothreefoobaretc');

Seems to be simple but I can't figure it out.

Thanks in advance!


$mash = "";
$res = array();

foreach ($arr as $el) {
    $mash .= $el;
    array_push($res, $mash);
}


$newArr = array();
$finish = count($arr);
$start = 0;
foreach($arr as $key => $value) {
   for ($i = $start; $i < $finish; $i++) {
      if (isset($newArray[$i])) {
         $newArray[$i] .= $value;
      } else {
         $newArray[$i] = $value;
      }
   }
   $start++;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜