2
Я пытаюсь сгенерировать список из массива PHP, который, когда пользователь вводит число, скажет, 3, тогда будет отображаться только 3 всего элемента в массиве.Как сгенерировать определенную сумму из массива PHP?
Пример:
$number_to_generate = 3;
jobs = array('Academic', 'Administrator', 'Architect',.......);
Output:
Academic
Academic
Academic
Administrator
Administrator
Administrator
Architect
Architect
Architect
Это то, что я в настоящее время:
// Check if tmp_profession_array is empty
// If it is empty grab any random profession and add it to the tmp_profession_array
array_filter($tmp_profession_array);
if (empty($tmp_profession_array)) {
$Profession = array_rand($professions_array, 1);
$tmp_profession_array[$Profession] = 1;
} else {
// If it is not empty, grab the last profession from it
end($tmp_profession_array);
$Profession = key($tmp_profession_array);
//// If it is not less than the amount to generate grab a new profession that does not exist in the tmp_profession_array
if ($tmp_profession_array[$Profession] > $number_to_generate) {
$break = TRUE;
while ($break) {
if (in_array($Profession, $tmp_profession_array)) {
$Profession = array_rand($professions_array, 1);
} else {
$break = FALSE;
}
}
//// If the profession count is less than the amount to generate then use it
} elseif ($tmp_profession_array[$Profession] < $number_to_generate) {
$tmp_profession_array[$Profession] += 1;
}
}
Wow :) Как вы пришли к этой реализации? –