How to generate a certain amount from PHP array? -
i'm trying generate list php array when user enters number say, 3, 3 total of each element in array displayed.
example:
$number_to_generate = 3; jobs = array('academic', 'administrator', 'architect',.......); output: academic academic academic administrator administrator administrator architect architect architect
this have:
// check if tmp_profession_array empty // if empty grab random profession , add 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 not empty, grab last profession end($tmp_profession_array); $profession = key($tmp_profession_array); //// if not less amount generate grab new profession not exist in 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 profession count less amount generate use } elseif ($tmp_profession_array[$profession] < $number_to_generate) { $tmp_profession_array[$profession] += 1; } }
very easy if understand correctly want.
// how many times want see each job? $numbertogenerate = 3; // list of jobs have $jobs = array('academic', 'administrator', 'architect'); // loop on jobs foreach($jobs $job){ // loop $numbertogenerate times for($i=0; $i < $numbertogenerate; $i++){ // echo output echo $i.'-'.$job.'<br />'; } } // done exit;
Comments
Post a Comment