String in between an array?
ok so I have 开发者_StackOverflow社区an array with an unknown array amount.
so like it can be
$array[0] = "a"
$array[1] = "b"
$array[2] = "c"
and what I want is the possibility to make an extra array in between each one, so like
$array[0] = "a"
$array[1] = "1"
$array[2] = "b"
$array[3] = "1"
$array[4] = "c"
$array[5] = "1"
<?php
function stringInBetweenArray($arr, $str){
$returnArray = array();
foreach($arr as $item){
$returnArray[] = $item;
$returnArray[] = $str;
}
return $returnArray;
}
$array = array("a", "b", "c");
print_r(stringInBetweenArray($array, "1"));
?>
Psuedocode:
new_array = new array(old_array_length * 2)
foreach (index, value) in old_array
new_array[index * 2] = value;
new_array[index * 2 + 1] = new_value;
return new_array
精彩评论