Answer the question
In order to leave comments, you need to log in
Where do you need to start building an algorithm for generating urls from an array?
There is an array:
$urlFragments = [
'pozdravlenya',
'na-angliyskom',
[ 'lubimoy', 'lubimomu' ],
'v-proze',
[ 'korotkie', 'nekorotkie' ]
];
Array
(
[0] => pozdravlenya/na-angliyskom/lubimoy/v-proze/korotkie
[1] => pozdravlenya/na-angliyskom/lubimoy/v-proze/nekorotkie
[2] => pozdravlenya/na-angliyskom/lubimomu/v-proze/korotkie
[3] => pozdravlenya/na-angliyskom/lubimomu/v-proze/nekorotkie
)
Answer the question
In order to leave comments, you need to log in
function getUrls($parts, $acc = '') {
$urls = [];
if (count($parts)) {
$part = array_shift($parts);
if (!is_array($part)) {
$part = [ $part ];
}
foreach ($part as $p) {
array_push($urls, ...getUrls($parts, ($acc ? $acc.'/' : '').$p));
}
} else {
$urls[] = $acc;
}
return $urls;
}
$urls = getUrls($urlFragments);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question