Here are the instructions to create dynamic array using php.
Dynamic Array is one where the elements can be altered during run time.
This title explain you both single dimensional and multidimensional array.
First let we declare the variables,
//Single Dimensional
$single_arr=array();
//Multidimensional
$multi_arr=array();
We can able to create dynamic new array from the database tables or existing arrays.
Now we create the dynamic array from tables
TableName: test
sno dept name
1 P&AR D.Premkumar 2 H,P&E N.Thangamani 3 Agriculture K.Shanmugam 4 RD&PR P.Usha 5 H&FW M.Vijayarani 6 Public R.Kalaimani 7 Public E.Dhandapani 8 P&AR A.Anbarasan 9 H&FW B.Kamakshi 10 Sch.Edn. M.Vijayalakshmi 11 Agriculture M.R.Amamath 12 L&E K.Lakshmi 13 Revenue S.Karikalan 14 Home S.Sellakkannu 15 Agriculture G.Ramanuja Surendranath
$sql="SELECT sno,dept,name FROM test";
$query=@mysql_query($sql);
$num_query=@mysql_num_rows($query);
if($num_query>0) {
while($fetch=mysql_fetch_assoc($query)) {
//Single Dimension Array
$single_arr[$fetch['sno']]=$fetch['name'];
//Multiple Dimension Array
$multi_arr[$fetch['dept']][$fetch['sno']]=$fetch['name'];
}
}
//echo ' < pre > ';
//print_r( $single_arr );
//print_r( $multi_arr );
Below are the output for created dynamic single dimensional array
Array
(
[1] => D.Premkumar
[2] => N.Thangamani
[3] => K.Shanmugam
[4] => P.Usha
[5] => M.Vijayarani
[6] => R.Kalaimani
[7] => E.Dhandapani
[8] => A.Anbarasan
[9] => B.Kamakshi
[10] => M.Vijay
[11] => M.R.Amamath
[12] => K.Lakshmi
[13] => S.Karikalan
[14] => S.Sellakkannu
[15] => G.Ramanuja
)
Below are the output for created dynamic multidimensional array
Array
(
[P&AR] => Array (
[1] => D.Premkumar
[8] => A.Anbarasan
)
[H,P&E] => Array (
[2] => N.Thangamani
)
[Agriculture] => Array (
[3] => K.Shanmugam
[11] => M.R.Amamath
[15] => G.Ramanuja
)
[RD&PR] => Array (
[4] => P.Usha
)
[H&FW] => Array (
[5] => M.Vijayarani
[9] => B.Kamakshi
)
[Public] => Array (
[6] => R.Kalaimani
[7] => E.Dhandapani
)
[Sch.Edn.] => Array (
[10] => M.Vijay
)
[L&E] => Array (
[12] => K.Lakshmi
)
[Revenue] => Array (
[13] => S.Karikalan
)
[Home] => Array (
[14] => S.Sellakkannu
)
)
From above we concluded that we created dynamic php array.