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++;
}
精彩评论