I have the following and would want another result.
String = A,B,C,D
Trying to get an array of
A
A,B
A,B,C
A,B,C,D
My current code
$arrayA = explode(',', $query);
$arrSize = count($arrayA);
for ($x=0; $x<$arrSize; ++$x) {
for ($y=0; $x==$y; ++$y) {
array_push($arrayB,$arrayA[$x]);
$y=0;
}
}
Here's one way to do it using array_slice
:
$str = 'A,B,C,D';
$strArr = explode(',', $str);
$newArr = array();
for($i=1; $i<=sizeof($strArr); $i++)
{
$newArr[] = implode( ',' , array_slice($strArr, 0, $i) );
}
print_r($newArr);
Outputs:
Array
(
[0] => A
[1] => A,B
[2] => A,B,C
[3] => A,B,C,D
)
Online demo here.
$test="a,b,c,d,e";
$arrayA = explode(',', $test);
$res=array();
$aux=array();
foreach($arrayA as $c){
$aux[]=$c;
$res[]=$aux;
}
i have not tested yet, but i think u catch the idea ;)