Get the solution ↓↓↓
str_repeat creates a single string, not an array.
Usearray_fill() to create an array with N copies of a value. Then append the array to the result.
foreach ($chars as $char) {
$result = array_merge($result, array_fill(0, 3, $char));
}
Since you know the number of repeats, you can get the desired output using aforeach andfor
$repeat + 1 times; push the char to$result<?php
$chars = ["a", "b" , "c"];
$result = [];
$repeat = 3;
foreach ($chars as $char) {
for ($i = 0; $i < $repeat; $i++) {
$result[] = $char;
}
}
var_dump($result);
array(9) {
[0]=>
string(1) "a"
[1]=>
string(1) "a"
[2]=>
string(1) "a"
[3]=>
string(1) "b"
[4]=>
string(1) "b"
[5]=>
string(1) "b"
[6]=>
string(1) "c"
[7]=>
string(1) "c"
[8]=>
string(1) "c"
}
@Barmar's answer has the same 'idea', but uses
array_merge instead off$result[] = ;array_fill instead off an extrafor.You can get by without the loops asarray_fill will take an array:
$result = array_merge(...array_fill(0, 3, $chars));
Or you could use a string:
$result = str_split(str_repeat(implode($chars), 3));
If needed justsort to get the order that you show.
I'll add a few more techniques for completeness. Solutions that leverage the magic number 3 will be more scalable, but I'll also show some less scalable approaches which repeat a variable or an action 3 times.
A body-less loop pushing each element into a new array 3 times: (Demo)
$result = [];
foreach (array_chunk($chars, 1) as [0 => $result[], 0 => $result[], 0 => $result[]]);
var_export($result);
A functional loop callingarray_fill() andarray_merge() on every iteration: (Demo)
var_export(
array_reduce(
$chars,
fn($result, $v) => array_merge($result, array_fill(0, 3, $v)),
[]
)
);
Transpose and flatten 3 copies of the input array: (Demo)
var_export(
array_merge(...array_map(null, $chars, $chars, $chars))
);
or: (Demo)
var_export(
array_merge(...array_map(null, ...array_fill(0, 3, $chars)))
);
Looped calls ofarray_fill() with its returned array spread as parameters ofarray_push(): (Demo)
$result = [];
foreach ($chars as $char) {
array_push($result, ...array_fill(0, 3, $char));
}
var_export($result);
Afor() loop callingarray_fill() andarray_splice() on every iteration: (Demo)
for ($i = count($chars) - 1; $i >= 0; --$i) {
array_splice($chars, $i, 1, array_fill(0, 3, $chars[$i]));
}
var_export($chars);
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.