{"id":338,"date":"2012-07-17T14:33:09","date_gmt":"2012-07-17T09:03:09","guid":{"rendered":"http:\/\/www.create-dynamic.com\/blog\/?p=338"},"modified":"2015-10-07T15:31:20","modified_gmt":"2015-10-07T10:01:20","slug":"create-dynamic-array-in-php","status":"publish","type":"post","link":"https:\/\/www.create-dynamic.com\/blog\/338\/phptags\/create-dynamic-array-in-php\/","title":{"rendered":"Create dynamic Array in PHP"},"content":{"rendered":"<p>Here are the instructions to create dynamic array using php.<\/p>\n<p><strong>Dynamic Array is one where the elements can be altered during run time.<\/strong><\/p>\n<p>This title explain you both <strong>single dimensional<\/strong> and <strong>multidimensional<\/strong> array.<\/p>\n<p>First let we declare the variables,<\/p>\n<pre><code class=\"php\">\/\/Single Dimensional\r\n<strong>$single_arr<\/strong>=array();\r\n\/\/Multidimensional\r\n<strong>$multi_arr<\/strong>=array();<\/code><\/pre>\n<p>We can able to create dynamic new array from the database tables or existing arrays.<\/p>\n<p><strong>Now we create the dynamic array from tables<\/strong><\/p>\n<p><strong>TableName<\/strong>: test<\/p>\n<pre><code class=\"html\">\r\n<table  class=\" table table-hover\" width=\"300\" border=\"1\"><tr> <td><strong>sno<\/strong><\/td><td><strong>dept<\/strong><\/td><td><strong>name<\/strong><\/td><\/tr>\r\n<tr><td>1<\/td><td>P&AR<\/td><td>D.Premkumar<\/td><\/tr><tr><td>2<\/td><td>H,P&E<\/td><td>N.Thangamani<\/td><\/tr><tr><td>3<\/td><td>Agriculture<\/td><td>K.Shanmugam<\/td><\/tr><tr><td>4<\/td><td>RD&PR<\/td><td>P.Usha<\/td><\/tr><tr><td>5<\/td><td>H&FW<\/td><td>M.Vijayarani<\/td><\/tr><tr><td>6<\/td><td>Public<\/td><td>R.Kalaimani<\/td><\/tr><tr><td>7<\/td><td>Public<\/td><td>E.Dhandapani<\/td><\/tr><tr><td>8<\/td><td>P&AR<\/td><td>A.Anbarasan<\/td><\/tr><tr><td>9<\/td><td>H&FW<\/td><td>B.Kamakshi<\/td><\/tr><tr><td>10<\/td><td>Sch.Edn.<\/td><td>M.Vijayalakshmi<\/td><\/tr><tr><td>11<\/td><td>Agriculture<\/td><td>M.R.Amamath<\/td><\/tr><tr><td>12<\/td><td>L&E<\/td><td>K.Lakshmi<\/td><\/tr><tr><td>13<\/td><td>Revenue<\/td><td>S.Karikalan<\/td><\/tr><tr><td>14<\/td><td>Home<\/td><td>S.Sellakkannu<\/td><\/tr><tr><td>15<\/td><td>Agriculture<\/td><td>G.Ramanuja Surendranath<\/td><\/tr><\/table><\/code><\/pre>\n<pre><code class=\"php\">$sql=\"SELECT sno,dept,name FROM test\";\r\n$query=@mysql_query($sql);\r\n$num_query=@mysql_num_rows($query);\r\nif($num_query&gt;0)\t{\r\nwhile($fetch=mysql_fetch_assoc($query))\t{\r\n\/\/Single Dimension Array\r\n$single_arr[$fetch['sno']]=$fetch['name'];\r\n\/\/Multiple Dimension Array\r\n$multi_arr[$fetch['dept']][$fetch['sno']]=$fetch['name'];\r\n}\r\n}\r\n\/\/echo ' < pre > ';\r\n\/\/print_r( $single_arr );\r\n\/\/print_r( $multi_arr );\r\n<\/code><\/pre>\n<p>Below are the output for created dynamic single dimensional array<\/p>\n<pre><code class=\"html\">Array\r\n(\r\n    [1] =&gt; D.Premkumar\r\n    [2] =&gt; N.Thangamani\r\n    [3] =&gt; K.Shanmugam\r\n    [4] =&gt; P.Usha\r\n    [5] =&gt; M.Vijayarani\r\n    [6] =&gt; R.Kalaimani\r\n    [7] =&gt; E.Dhandapani\r\n    [8] =&gt; A.Anbarasan\r\n    [9] =&gt; B.Kamakshi\r\n    [10] =&gt; M.Vijay\r\n    [11] =&gt; M.R.Amamath\r\n    [12] =&gt; K.Lakshmi\r\n    [13] =&gt; S.Karikalan\r\n    [14] =&gt; S.Sellakkannu\r\n    [15] =&gt; G.Ramanuja\r\n)<\/code><\/pre>\n<p>Below are the output for created dynamic multidimensional array<\/p>\n<pre><code class=\"html\">Array\r\n(\r\n    [P&amp;AR] =&gt; Array        (\r\n            [1] =&gt; D.Premkumar\r\n            [8] =&gt; A.Anbarasan\r\n        )\r\n    [H,P&amp;E] =&gt; Array        (\r\n            [2] =&gt; N.Thangamani\r\n        )\r\n    [Agriculture] =&gt; Array        (\r\n            [3] =&gt; K.Shanmugam\r\n            [11] =&gt; M.R.Amamath\r\n            [15] =&gt; G.Ramanuja\r\n        )\r\n    [RD&amp;PR] =&gt; Array        (\r\n            [4] =&gt; P.Usha\r\n        )\r\n    [H&amp;FW] =&gt; Array        (\r\n            [5] =&gt; M.Vijayarani\r\n            [9] =&gt; B.Kamakshi\r\n        )\r\n    [Public] =&gt; Array        (\r\n            [6] =&gt; R.Kalaimani\r\n            [7] =&gt; E.Dhandapani\r\n        )\r\n    [Sch.Edn.] =&gt; Array        (\r\n            [10] =&gt; M.Vijay\r\n        )\r\n    [L&amp;E] =&gt; Array        (\r\n            [12] =&gt; K.Lakshmi\r\n        )\r\n    [Revenue] =&gt; Array        (\r\n            [13] =&gt; S.Karikalan\r\n        )\r\n    [Home] =&gt; Array        (\r\n            [14] =&gt; S.Sellakkannu\r\n        )\r\n)<\/code><\/pre>\n<p>From above we concluded that we created dynamic php array.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dynamic Array is one where the elements can be altered during run time.create dynamic array in php<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-338","post","type-post","status-publish","format-standard","hentry","category-phptags"],"_links":{"self":[{"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/posts\/338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/comments?post=338"}],"version-history":[{"count":3,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/posts\/338\/revisions"}],"predecessor-version":[{"id":377,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/posts\/338\/revisions\/377"}],"wp:attachment":[{"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/media?parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/categories?post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/tags?post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}