Create dynamic Array in PHP

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


snodeptname
1P&ARD.Premkumar
2H,P&EN.Thangamani
3AgricultureK.Shanmugam
4RD&PRP.Usha
5H&FWM.Vijayarani
6PublicR.Kalaimani
7PublicE.Dhandapani
8P&ARA.Anbarasan
9H&FWB.Kamakshi
10Sch.Edn.M.Vijayalakshmi
11AgricultureM.R.Amamath
12L&EK.Lakshmi
13RevenueS.Karikalan
14HomeS.Sellakkannu
15AgricultureG.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.

ADMIN

I am sasikumar working as php web developer. create-dynamic.com blog will provide some information related to php mysql web development.