How to Create array with dynamic key and value in PHP

How to Create array with dynamic key and value in PHP

// Here table name is - 'monthly_student'
$query = mysqli_query('SELECT * FROM `monthly_student`');

$assoc_Array = array(); // empty array
while($row = mysqli_fetch_array($query)){
   // Put the values into the array, no other variables required
   $assoc_Array[$row['rollno']] = $row['studentname']; // Here 'rollno' and 'studentname' is column of table
}

foreach($assoc_Array as $val => $sid){
    echo $val."=>".$sid;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *