我有一个像这样的字符串变量:'1111/222/333'
或'1111/222/333/444'
我希望得到一个像这样的数组:
111-222-3333
111-222-
111-
OR
1111-222-333-444
1111-222-333-
1111-222-
1111-
我尝试了$pieces = explode("/", $str);
和
$str = '1111/222/333/444';
$pieces = explode("/", $str);
for($i=0; $i<count($pieces); $i++) {
$var .= $pieces[$i]."-".$pieces[$i+1];
echo $i." - ".$var."<br>";
}
但我没有得到正确的结果!
任何帮助?
答案 0 :(得分:4)
$str = '1111/222/333/444' ;
$pieces = explode("/", $str);
$num = count($pieces);
for($i=0;$i<$num;$i++) {
print implode('-',$pieces).($i?'-':'')."\n";
array_pop($pieces);
}
答案 1 :(得分:0)
这个怎么样:
$str = '1111/222/333/444';
$pieces = explode("/", $str);
$newArray = array();
$s = '';
foreach ($pieces as $piece)
{
$s .= $piece.'-'
$newArray[] = $s;
}
rsort($newArray);
答案 2 :(得分:0)
我的第一次尝试是:
$str = '1111/222/333/444';
$pieces = explode("/", $str);
$countPieces = count($pieces);
echo $countPieces;
while ($countPieces > 0) {
$var = $pieces[0];
for($i = 1; $i < $countPieces; $i++) {
$var = $var . "-" . $pieces[$i];
}
echo $var . "\n";
$countPieces--;
}