[Solved] Random no but unique from given no


If $row and $ar have an equal element count, I don’t see the reason for two loops. Your original code will do four iterations, and I’m not sure that’s what you want. (Judging by my uncertainty and the number of negative votes, you should probably update your question asap.)

Code: (Demo)

$arr=['11','12'];
$ar=['1','2','3','4','5'];

// shuffle and index $data subarrays
foreach($arr as $v){
    shuffle($ar); // just shuffle before the next loop
    $data[]=['stdid'=>$v,"tid"=>$ar];
}
var_export($data);

$data=[];  // clear the array
echo "\n\n---\n\n";

// or shuffle and use stdid as subarray keys for $data
foreach($arr as $v){
    shuffle($ar); // just shuffle before the next loop
    $data[$v]=["tid"=>$ar];
}
var_export($data);

Output:

array (
  0 => 
  array (
    'stdid' => '11',
    'tid' => 
    array (
      0 => '1',
      1 => '4',
      2 => '5',
      3 => '2',
      4 => '3',
    ),
  ),
  1 => 
  array (
    'stdid' => '12',
    'tid' => 
    array (
      0 => '4',
      1 => '5',
      2 => '2',
      3 => '3',
      4 => '1',
    ),
  ),
)

---

array (
  11 => 
  array (
    'tid' => 
    array (
      0 => '5',
      1 => '2',
      2 => '1',
      3 => '3',
      4 => '4',
    ),
  ),
  12 => 
  array (
    'tid' => 
    array (
      0 => '2',
      1 => '4',
      2 => '1',
      3 => '3',
      4 => '5',
    ),
  ),
)

solved Random no but unique from given no