I am writing a PHP function that will take an array in the following format:
array(
'one',
'two',
'three'
)
And echo the following strings:
one
one-two
one-two-three
I can't figure out how to do this. I've tried using a variable to store the previous one and then use it, but it only works for one:
$previous = null;
for($i = 0; $i < count($list); $i++) {
echo ($previous != null ? $route[$previous] . "-" : '') . $route[$i];
$previous = $i;
}
Outputting:
one
two
two-three
That approach would probably be inefficient anyway, as this script should technically be able to handle any length of array.
Can anybody help?
So how to convert String array to String in java. We can use Arrays. toString method that invoke the toString() method on individual elements and use StringBuilder to create String.
The elements of a NumPy array, or simply an array, are usually numbers, but can also be boolians, strings, or other objects. When the elements are numbers, they must all be of the same type. For example, they might be all integers or all floating point numbers.
for ($i = 1, $length = count($array); $i <= $length; $i++) {
echo join('-', array_slice($array, 0, $i)), PHP_EOL;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With