我正在寻找一种方法来获取一个php数组并在几个数组键之间传递值并保存到一个字符串中.
$array1 = array(0=>'sometext',
1=>'1703',
2=>'North',
3=>5th',
4=>'st',
5=>'sometext')
I know the starting key and the end key in my script
$startnum = 1;
$endnum = 4;
I need to get this
$string = '1703 North 5th st'
Run Code Online (Sandbox Code Playgroud)
不更改数组中的键,因为我必须稍后再次遍历数组.我目前正在使用数组拼接,但它删除了数组中的项和键,所以当我再次遍历数组时,键和值都搞砸了.如果我需要更好的解释,请告诉我.
获取感兴趣的片段array_slice,然后使用implode以下方式折叠:
$string = implode(' ', array_slice($array1, $startnum, $endnum-$startnum));
Run Code Online (Sandbox Code Playgroud)
注意,array_slice取一个偏移量和一个长度,因此长度计算为结束减去开始.