Answer the question
In order to leave comments, you need to log in
How can I sort the array according to my algorithm?
The input data is, for example :
1. Distribution quantity = 3
2. Array
[0] => Selling cutlet
[1] => Selling shoes
[2] => Selling seeds
[3] = > Selling cutlet [
4 ] => Selling shoes
[5] => Seeds for sale
[6] => Cutlets for sale
[
7] => Shoes for sale
[8] => Seeds
for
sale
[8] => Seeds for sale
[1] => Shoes
for sale [5] => Seeds for sale
[6] => Cutlet for sale
[2] => Seeds for sale
[3] => Cutlet for sale
[7] => Selling shoes
That is, at the output I need to get a resorted array, the elements of which will always be different, the key value in the example, equal to 3 , takes part in the sorting algorithm, because the number can change.
Maybe I'm delusional.
Answer the question
In order to leave comments, you need to log in
Which algorithm is not clear, but keywords: "resort array according to my algorithm" is one of 2 functions:
php.net/uasort
php.net/uksort
----------------- --------------------
If the task is set in a new way, it is easier to do this:
1. highlight the keywords (sell, sell, sale "
2. highlight the second words
3. stick together.
$keys = ["Продам", "Продаётся", "Продажа"];
$words = ["котлету", "ботинок", "семки"];
$groupSize = 3;
$amount = sizeof($keys)*sizeof($words);
$list = array_fill(0,$amount, "");
foreach ($keys as $group => $groupName) {
foreach ($words as $item => $word) {
$index = (($group+$item) * $groupSize + $group)%$amount;
$list[$index] = $groupName . ' ' . $word;
}
}
print_r($list);
/** Создаём список. важно что он имеет строгий формат */
$keys = ["Продам", "Продаётся", "Продажа"];
$words = ["котлету", "ботинок", "семки"];
$list = [];
foreach ($words as $wordId => $word) {
foreach ($keys as $groupId => $groupName) {
$list[] = $groupName . ' ' . $word;
}
}
// сортируем
$groupSize = 3;
$output = array_fill(0, sizeof($list), "");
foreach ($list as $id => $string) {
$group = floor($id/$groupSize);
$item = $id%$groupSize;
$index = (($group+$item) * $groupSize + $item)%sizeof($list);
$output[$index] = $string;
}
print_r($output);
in the function, create a temporary array and add the necessary elements to it with the necessary indices, then the temporary array to exit through return
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question