{"id":138,"date":"2018-07-12T09:58:34","date_gmt":"2018-07-12T09:58:34","guid":{"rendered":"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=138"},"modified":"2018-12-07T09:42:33","modified_gmt":"2018-12-07T09:42:33","slug":"array","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/chapter\/array\/","title":{"rendered":"Array"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/Sx4WsWRp_ZA\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Introduction:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us assume, you want to store name of five students. You can define five variables. Suppose that there 50 students now available. So you have to code 50 variables. To manage 50 variables is indeed cumbersome and error to prone task. Instead you can create an array.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Array is a data type in PHP. Array is a data type which can store multiple items of same data type or different data types. The item in array is known as element. Each element can store a value. To get the value of a particular element of an array, an index is used. In short, array element has index and value. The data type of an index can be either integer or string. The value of an item can be any PHP data type. By Default the data type of index is integer in PHP.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">You can create array by single statement of multiple statements as per your requirement. Both are the valid way to create an array.<\/p>\r\n&nbsp;\r\n\r\n1.Create array using single statement$arrayname = array(value1, value2 [, value3,\u2026.]);\r\n\r\n2.Create array using multiple statements\r\n\r\na. $arrayname = array( );\r\n\r\nb. $araryname[index] = value;\r\n\r\n&nbsp;\r\n\r\nLet us take an example, to create an array:\r\n\r\n&nbsp;\r\n\r\n$name = array( );\r\n\r\n$name[0] = 'Hiren';\r\n\r\n$name[1] = 'Jignesh';\r\n\r\n$name[2] = 'Vimal';\r\n\r\n&nbsp;\r\n\r\n\/\/ Or you can write\r\n\r\n&nbsp;\r\n\r\n$names = array('Vipul','Chetan','Mihir');\r\n\r\n&nbsp;\r\n\r\nIn array, element index starts with 0. The index for second element is 1 and so on.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">So, vipul is stored at index 0 in array $names. Mihir is stored at index 2 in array $names. To retrieve , particular element from array type the arrayname with its index. For example, echo \"&lt;br&gt; $names[1]\";will display Chetan on your browser.<\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 To add an element at the end of an array, the syntax is<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$arrayname [ ] = value<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">In case you code<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$names[ ] = \u201cGautam\u201d;<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">Then $names array has 4 elements : 'Vipul','Chetan','Mihir', \u201eGautam\u201f You can add element in array at specific index also.<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$names[7] = \u201cJaymin\u201d;<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Then the array $names has total 8 elements. The element at index 4,5, and 6 has null value.<\/span><span style=\"text-align: justify;font-size: 1em\">To delete the value at any particular index, unset function is used. The general syntax for deleting value at any particular index is:<\/span><\/p>\r\n\r\n<div>\r\n\r\n&nbsp;\r\n\r\nunset($arrayname[index])\r\n\r\nTo delete the value chetan from array $names, the code is unset($names[1]);\r\n\r\n&nbsp;\r\n\r\nAfter unset the element 2(index 1) , the array has values Vipul Mihir Gautam Jaymin.\r\n\r\n&nbsp;\r\n\r\nunset($names) deletes the entire array $names.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The array_values function is return a new array in which the gaps within an array is removed and the values are reindexed. It takes arrayname as argument.<\/p>\r\n&nbsp;\r\n\r\n$arr = array('one','two','three', 'four');\r\n\r\n&nbsp;\r\n\r\nunset($arr[2]);\r\n\r\n&nbsp;\r\n\r\n$arr = array_values($arr);\r\n\r\n&nbsp;\r\n\r\nFunctions to work with array using for loop\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div><img class=\"size-full wp-image-141 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-88.png\" alt=\"\" width=\"649\" height=\"138\" \/><\/div>\r\n<strong>\u00a0<\/strong><span style=\"text-align: initial;font-size: 1em\">Let us understand the use of these functions by example<\/span>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n$numbers = array ( );\r\n\r\nfor($i = 1 ;$i &lt;= 10; $i++)\r\n\r\n{\r\n\r\n$numbers[] = $i;\r\n\r\n}\r\n\r\n$num = \"\";\r\n\r\nfor($i = 0; $i &lt;count($numbers);$i++)\r\n\r\n{\r\n\r\n$num .= $numbers[$i] .\" \";\r\n\r\n}\r\n\r\necho $num;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The above code create an array $numbers and store 1 to 10 in it. Then using count function we restrict the loop counter and display the same array element in browser.<\/p>\r\n&nbsp;\r\n\r\n$end = end($numbers);\r\n\r\necho \" &lt;br&gt;End is : $end\";\r\n\r\nwill display\r\n\r\nEnd is : 10\r\n\r\n&nbsp;\r\n\r\nAs it is the value of last element in array $numbers.\r\n\r\n&nbsp;\r\n\r\nThe code immediately after it\r\n\r\n$key = key($numbers);\r\n\r\necho \"&lt;br&gt; The current key is: $key\";\r\n\r\n&nbsp;\r\n\r\nwill display\r\n\r\n&nbsp;\r\n\r\nThe current key is: 9\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The index 9 stores the value 10. The highest index of an array is always 1 least then the total numbers of elements in an array because the array index is begin with 0.<\/p>\r\n\r\n<\/div>\r\n<strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0Do it yourself:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Write a PHP code which will take an array of five number and display average value of these numbers.<\/span><\/p>\r\n\r\n<div>\r\n\r\n<strong>\u00a0<\/strong>\r\n\r\n<strong> \u00a0Foreach loop<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The foreach loop is used for array. In foreach loop, there is no requirement of initialize a variable, test condition and increment\/decrement the variable value. The general syntax of foreach loop is<\/p>\r\n&nbsp;\r\n\r\nforeach( $arrayname as [$key =&gt;] $value)\r\n\r\n{\r\n\r\nStatements which are using key and value;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nLet take one example of foreach loop.\r\n\r\n&nbsp;\r\n\r\necho \"&lt;br&gt; For each loop for an array\";\r\n\r\n$nums = array(1,2,3,4,5,6,7,8,9,10);\r\n\r\necho \"&lt;br&gt;\";\r\n\r\nforeach($nums as $num)\r\n\r\n{\r\n\r\necho \"&lt;t&gt; $num\";\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nThen the output will be\r\n\r\nFor each loop for an array\r\n\r\n1 2 3 4 5 6 7 8 9 10\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The foreach loop is used for filling gap in an array. Let us understand by one example.<\/p>\r\n&nbsp;\r\n\r\necho \"&lt;br&gt; For each loop for an array\";\r\n\r\n$nums = array(1,2,3,4,5,6,7,8,9,10);\r\n\r\necho \"&lt;br&gt;\";\r\n\r\nforeach($nums as $num)\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;t&gt; $num\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">unset($nums[4],$nums[5]);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">e<\/span><span style=\"text-align: initial;font-size: 1em\">cho \"&lt;br&gt; After unset elements 5 and 6 &lt;br&gt;\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">foreach($nums as $num)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;t&gt; $num\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThen the output will be\r\n\r\n&nbsp;\r\n\r\nFor each loop for an array\r\n\r\n1 2 3 4 5 6 7 8 9 10\r\n\r\n&nbsp;\r\n\r\nAfter unset elements 5 and 6\r\n\r\n1 2 3 4 7 8 9 10\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The foreach loop fills the gap as the element value 5 (index 4) and element value 6(index 5) was unset.<\/p>\r\n&nbsp;\r\n\r\n<strong>Associative array<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">String can be used as index in array in PHP. When string is used as index in array, this array is known as associative array. In associative array, the index is known as key. Similar to normal array, you can create associative array by two ways. Associative array be created by using a single statement or by using multiple statement.<\/p>\r\n&nbsp;\r\n\r\n<strong>Single statement:<\/strong>\r\n\r\n&nbsp;\r\n\r\nArray ([key1 =&gt; value1, key2 =&gt; value2, key3 =&gt; value3, \u2026])\r\n\r\n&nbsp;\r\n\r\n<strong>Multiple statements:<\/strong>\r\n\r\n&nbsp;\r\n\r\na. $arrayname = array( );\r\n\r\nb. $araryname[key] = value;\r\n\r\n&nbsp;\r\n\r\nLet us understand by one simple example,\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">$statecode = array(\"GJ\" =&gt; \"Gujarat\", \"MP\" =&gt; \"MadhayPradesh\", \"MH\" =&gt; \"Maharashtra\");<\/p>\r\n<p style=\"text-align: justify\">Following code snippet create an associative array using multiple statements:<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 $countrycode = array();\r\n\r\n$countrycode[\"in\"] = \"India\";\r\n\r\n$countrycode['ca'] = \"Canada\";\r\n\r\n$countrycode['fr'] = \"France\";\r\n\r\n$countrycode['usa'] = \"America\";\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Following code snippet create an associative array using multiple statements and display each element.<\/p>\r\n&nbsp;\r\n\r\n$countrycode = array();\r\n\r\n$countrycode[\"in\"] = \"India\";\r\n\r\n$countrycode['ca'] = \"Canada\";\r\n\r\n$countrycode['fr'] = \"France\";\r\n\r\n$countrycode['usa'] = \"America\";\r\n\r\nforeach($countrycode as $value)\r\n\r\n{\r\n\r\necho \"&lt;t&gt; $value\";\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nThen the output will be\r\n\r\nIndia Canada France America\r\n\r\nTo display key and value both\r\n\r\n&nbsp;\r\n\r\nforeach($countrycode as $key =&gt; $value)\r\n\r\n{\r\n\r\necho \"&lt;t&gt; $key =&gt; $value\";\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nThen the output will be\r\n\r\n&nbsp;\r\n\r\nin =&gt; India ca =&gt; Canada fr =&gt; France usa =&gt; America\r\n\r\n<\/div>\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0An array with integer key but keyfrite a PHP code which will take an array of five number and display average value of these numbet of as an associative array.<\/span>\r\n<div>\r\n\r\n\u00a0 \u00a0 $ext = array();\r\n\r\n$ext[200] = \u201cVice Chancellor\u201d;\r\n\r\n$ext[202] = \u201cRegistrar\u201d;\r\n\r\n$ext[205] = \u201cAdmin\u201d;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Though it is not considered a good practice, a single array can contain both integer and string indexes. For example, an array can be defined as below.<\/p>\r\n&nbsp;\r\n\r\n$emp = array();\r\n\r\n$emp[0] = \"Anil\";\r\n\r\n$emp[1] = \"Bhavin\";\r\n\r\n$emp[2] = \"Chetan\";\r\n\r\n$emp[\"senior\"] = \"Geeta\";\r\n\r\n$emp[\"newest\"] = \"Kapil\";\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Associative works same as index array. Only the difference is here in the code, key is used instead of index.<\/p>\r\n&nbsp;\r\n\r\nIn associative array, an element can be added.\r\n\r\n&nbsp;\r\n\r\n$countrycode['uk'] = \"United Kingdom\";\r\n\r\n&nbsp;\r\n\r\nWill add the element with key uk and value for key is united kingdom.\r\n\r\n&nbsp;\r\n\r\nTo get the value of an array element, you can code\r\n\r\n&nbsp;\r\n\r\n$countrycode['uk']\r\n\r\n&nbsp;\r\n\r\nIf the key is not specified in an associative array, PHP uses an integer index.\r\n\r\n&nbsp;\r\n\r\n$countrycode[] = 'Australia';\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In Above code, the index(key) is assigned to zero. In case an integer indexes are already assigned, then PHP take largest index +1 as index(key).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">To delete an element from associative array, unset is used. In unset function passed, the key to delete particular element.<\/p>\r\n&nbsp;\r\n\r\nunset($countrycode['fr']);\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">To delete , the entire array code<\/span>\r\n\r\n<\/div>\r\nunset($countrycode)\r\n\r\n&nbsp;\r\n\r\nFor variable substitution, you have to omit the quotes inside square bracket for a string key.\r\n\r\n&nbsp;\r\n\r\necho \"&lt;br&gt; $statecode[MP] \";\r\n\r\n&nbsp;\r\n\r\nor you can write\r\n\r\n&nbsp;\r\n\r\necho \"&lt;br&gt; {$statecode['MH']}\";\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Foreach loop is also used in array to process all elements in an array which has gaps.<\/p>\r\n&nbsp;\r\n\r\n$countrycode = array();\r\n\r\n$countrycode[\"in\"] = \"India\";\r\n\r\n$countrycode['ca'] = \"Canada\";\r\n\r\n$countrycode['fr'] = \"France\";\r\n\r\n$countrycode['usa'] = \"America\";\r\n\r\nunset($countrycode['fr']);\r\n\r\nforeach($countrycode as $cc =&gt; $con)\r\n\r\n{\r\n\r\necho \"&lt;t&gt; $cc =&gt; $con \";\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nWill display\r\n\r\n&nbsp;\r\n\r\nin =&gt; India ca =&gt; Canada usa =&gt; America\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Array<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/Sx4WsWRp_ZA\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<strong>References:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\r\n<p style=\"text-align: justify\">2.W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional,Apress<\/p>\r\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K.Glass: Beginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\r\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O'Reilly Media<\/p>\r\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove:Professional PHP, Wrox<\/p>\r\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\r\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\r\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\r\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\r\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide,Pearson Education<\/p>\r\n<p style=\"text-align: justify\">11. http:\/\/www.php.net<\/p>\r\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\r\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/Sx4WsWRp_ZA\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Introduction:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us assume, you want to store name of five students. You can define five variables. Suppose that there 50 students now available. So you have to code 50 variables. To manage 50 variables is indeed cumbersome and error to prone task. Instead you can create an array.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Array is a data type in PHP. Array is a data type which can store multiple items of same data type or different data types. The item in array is known as element. Each element can store a value. To get the value of a particular element of an array, an index is used. In short, array element has index and value. The data type of an index can be either integer or string. The value of an item can be any PHP data type. By Default the data type of index is integer in PHP.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">You can create array by single statement of multiple statements as per your requirement. Both are the valid way to create an array.<\/p>\n<p>&nbsp;<\/p>\n<p>1.Create array using single statement$arrayname = array(value1, value2 [, value3,\u2026.]);<\/p>\n<p>2.Create array using multiple statements<\/p>\n<p>a. $arrayname = array( );<\/p>\n<p>b. $araryname[index] = value;<\/p>\n<p>&nbsp;<\/p>\n<p>Let us take an example, to create an array:<\/p>\n<p>&nbsp;<\/p>\n<p>$name = array( );<\/p>\n<p>$name[0] = &#8216;Hiren&#8217;;<\/p>\n<p>$name[1] = &#8216;Jignesh&#8217;;<\/p>\n<p>$name[2] = &#8216;Vimal&#8217;;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Or you can write<\/p>\n<p>&nbsp;<\/p>\n<p>$names = array(&#8216;Vipul&#8217;,&#8217;Chetan&#8217;,&#8217;Mihir&#8217;);<\/p>\n<p>&nbsp;<\/p>\n<p>In array, element index starts with 0. The index for second element is 1 and so on.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">So, vipul is stored at index 0 in array $names. Mihir is stored at index 2 in array $names. To retrieve , particular element from array type the arrayname with its index. For example, echo &#8220;&lt;br&gt; $names[1]&#8221;;will display Chetan on your browser.<\/p>\n<\/div>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 To add an element at the end of an array, the syntax is<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$arrayname [ ] = value<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">In case you code<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$names[ ] = \u201cGautam\u201d;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">Then $names array has 4 elements : &#8216;Vipul&#8217;,&#8217;Chetan&#8217;,&#8217;Mihir&#8217;, \u201eGautam\u201f You can add element in array at specific index also.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$names[7] = \u201cJaymin\u201d;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Then the array $names has total 8 elements. The element at index 4,5, and 6 has null value.<\/span><span style=\"text-align: justify;font-size: 1em\">To delete the value at any particular index, unset function is used. The general syntax for deleting value at any particular index is:<\/span><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>unset($arrayname[index])<\/p>\n<p>To delete the value chetan from array $names, the code is unset($names[1]);<\/p>\n<p>&nbsp;<\/p>\n<p>After unset the element 2(index 1) , the array has values Vipul Mihir Gautam Jaymin.<\/p>\n<p>&nbsp;<\/p>\n<p>unset($names) deletes the entire array $names.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The array_values function is return a new array in which the gaps within an array is removed and the values are reindexed. It takes arrayname as argument.<\/p>\n<p>&nbsp;<\/p>\n<p>$arr = array(&#8216;one&#8217;,&#8217;two&#8217;,&#8217;three&#8217;, &#8216;four&#8217;);<\/p>\n<p>&nbsp;<\/p>\n<p>unset($arr[2]);<\/p>\n<p>&nbsp;<\/p>\n<p>$arr = array_values($arr);<\/p>\n<p>&nbsp;<\/p>\n<p>Functions to work with array using for loop<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-141 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-88.png\" alt=\"\" width=\"649\" height=\"138\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-88.png 649w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-88-300x64.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-88-65x14.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-88-225x48.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-88-350x74.png 350w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/div>\n<p><strong>\u00a0<\/strong><span style=\"text-align: initial;font-size: 1em\">Let us understand the use of these functions by example<\/span><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>$numbers = array ( );<\/p>\n<p>for($i = 1 ;$i &lt;= 10; $i++)<\/p>\n<p>{<\/p>\n<p>$numbers[] = $i;<\/p>\n<p>}<\/p>\n<p>$num = &#8220;&#8221;;<\/p>\n<p>for($i = 0; $i &lt;count($numbers);$i++)<\/p>\n<p>{<\/p>\n<p>$num .= $numbers[$i] .&#8221; &#8220;;<\/p>\n<p>}<\/p>\n<p>echo $num;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The above code create an array $numbers and store 1 to 10 in it. Then using count function we restrict the loop counter and display the same array element in browser.<\/p>\n<p>&nbsp;<\/p>\n<p>$end = end($numbers);<\/p>\n<p>echo &#8221; &lt;br&gt;End is : $end&#8221;;<\/p>\n<p>will display<\/p>\n<p>End is : 10<\/p>\n<p>&nbsp;<\/p>\n<p>As it is the value of last element in array $numbers.<\/p>\n<p>&nbsp;<\/p>\n<p>The code immediately after it<\/p>\n<p>$key = key($numbers);<\/p>\n<p>echo &#8220;&lt;br&gt; The current key is: $key&#8221;;<\/p>\n<p>&nbsp;<\/p>\n<p>will display<\/p>\n<p>&nbsp;<\/p>\n<p>The current key is: 9<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The index 9 stores the value 10. The highest index of an array is always 1 least then the total numbers of elements in an array because the array index is begin with 0.<\/p>\n<\/div>\n<p><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0Do it yourself:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Write a PHP code which will take an array of five number and display average value of these numbers.<\/span><\/p>\n<div>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong> \u00a0Foreach loop<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The foreach loop is used for array. In foreach loop, there is no requirement of initialize a variable, test condition and increment\/decrement the variable value. The general syntax of foreach loop is<\/p>\n<p>&nbsp;<\/p>\n<p>foreach( $arrayname as [$key =&gt;] $value)<\/p>\n<p>{<\/p>\n<p>Statements which are using key and value;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Let take one example of foreach loop.<\/p>\n<p>&nbsp;<\/p>\n<p>echo &#8220;&lt;br&gt; For each loop for an array&#8221;;<\/p>\n<p>$nums = array(1,2,3,4,5,6,7,8,9,10);<\/p>\n<p>echo &#8220;&lt;br&gt;&#8221;;<\/p>\n<p>foreach($nums as $num)<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;t&gt; $num&#8221;;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Then the output will be<\/p>\n<p>For each loop for an array<\/p>\n<p>1 2 3 4 5 6 7 8 9 10<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The foreach loop is used for filling gap in an array. Let us understand by one example.<\/p>\n<p>&nbsp;<\/p>\n<p>echo &#8220;&lt;br&gt; For each loop for an array&#8221;;<\/p>\n<p>$nums = array(1,2,3,4,5,6,7,8,9,10);<\/p>\n<p>echo &#8220;&lt;br&gt;&#8221;;<\/p>\n<p>foreach($nums as $num)<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;t&gt; $num&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">unset($nums[4],$nums[5]);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">e<\/span><span style=\"text-align: initial;font-size: 1em\">cho &#8220;&lt;br&gt; After unset elements 5 and 6 &lt;br&gt;&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">foreach($nums as $num)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;t&gt; $num&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>Then the output will be<\/p>\n<p>&nbsp;<\/p>\n<p>For each loop for an array<\/p>\n<p>1 2 3 4 5 6 7 8 9 10<\/p>\n<p>&nbsp;<\/p>\n<p>After unset elements 5 and 6<\/p>\n<p>1 2 3 4 7 8 9 10<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The foreach loop fills the gap as the element value 5 (index 4) and element value 6(index 5) was unset.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Associative array<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">String can be used as index in array in PHP. When string is used as index in array, this array is known as associative array. In associative array, the index is known as key. Similar to normal array, you can create associative array by two ways. Associative array be created by using a single statement or by using multiple statement.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Single statement:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Array ([key1 =&gt; value1, key2 =&gt; value2, key3 =&gt; value3, \u2026])<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Multiple statements:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>a. $arrayname = array( );<\/p>\n<p>b. $araryname[key] = value;<\/p>\n<p>&nbsp;<\/p>\n<p>Let us understand by one simple example,<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">$statecode = array(&#8220;GJ&#8221; =&gt; &#8220;Gujarat&#8221;, &#8220;MP&#8221; =&gt; &#8220;MadhayPradesh&#8221;, &#8220;MH&#8221; =&gt; &#8220;Maharashtra&#8221;);<\/p>\n<p style=\"text-align: justify\">Following code snippet create an associative array using multiple statements:<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 $countrycode = array();<\/p>\n<p>$countrycode[&#8220;in&#8221;] = &#8220;India&#8221;;<\/p>\n<p>$countrycode[&#8216;ca&#8217;] = &#8220;Canada&#8221;;<\/p>\n<p>$countrycode[&#8216;fr&#8217;] = &#8220;France&#8221;;<\/p>\n<p>$countrycode[&#8216;usa&#8217;] = &#8220;America&#8221;;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Following code snippet create an associative array using multiple statements and display each element.<\/p>\n<p>&nbsp;<\/p>\n<p>$countrycode = array();<\/p>\n<p>$countrycode[&#8220;in&#8221;] = &#8220;India&#8221;;<\/p>\n<p>$countrycode[&#8216;ca&#8217;] = &#8220;Canada&#8221;;<\/p>\n<p>$countrycode[&#8216;fr&#8217;] = &#8220;France&#8221;;<\/p>\n<p>$countrycode[&#8216;usa&#8217;] = &#8220;America&#8221;;<\/p>\n<p>foreach($countrycode as $value)<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;t&gt; $value&#8221;;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Then the output will be<\/p>\n<p>India Canada France America<\/p>\n<p>To display key and value both<\/p>\n<p>&nbsp;<\/p>\n<p>foreach($countrycode as $key =&gt; $value)<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;t&gt; $key =&gt; $value&#8221;;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Then the output will be<\/p>\n<p>&nbsp;<\/p>\n<p>in =&gt; India ca =&gt; Canada fr =&gt; France usa =&gt; America<\/p>\n<\/div>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0An array with integer key but keyfrite a PHP code which will take an array of five number and display average value of these numbet of as an associative array.<\/span><\/p>\n<div>\n<p>\u00a0 \u00a0 $ext = array();<\/p>\n<p>$ext[200] = \u201cVice Chancellor\u201d;<\/p>\n<p>$ext[202] = \u201cRegistrar\u201d;<\/p>\n<p>$ext[205] = \u201cAdmin\u201d;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Though it is not considered a good practice, a single array can contain both integer and string indexes. For example, an array can be defined as below.<\/p>\n<p>&nbsp;<\/p>\n<p>$emp = array();<\/p>\n<p>$emp[0] = &#8220;Anil&#8221;;<\/p>\n<p>$emp[1] = &#8220;Bhavin&#8221;;<\/p>\n<p>$emp[2] = &#8220;Chetan&#8221;;<\/p>\n<p>$emp[&#8220;senior&#8221;] = &#8220;Geeta&#8221;;<\/p>\n<p>$emp[&#8220;newest&#8221;] = &#8220;Kapil&#8221;;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Associative works same as index array. Only the difference is here in the code, key is used instead of index.<\/p>\n<p>&nbsp;<\/p>\n<p>In associative array, an element can be added.<\/p>\n<p>&nbsp;<\/p>\n<p>$countrycode[&#8216;uk&#8217;] = &#8220;United Kingdom&#8221;;<\/p>\n<p>&nbsp;<\/p>\n<p>Will add the element with key uk and value for key is united kingdom.<\/p>\n<p>&nbsp;<\/p>\n<p>To get the value of an array element, you can code<\/p>\n<p>&nbsp;<\/p>\n<p>$countrycode[&#8216;uk&#8217;]<\/p>\n<p>&nbsp;<\/p>\n<p>If the key is not specified in an associative array, PHP uses an integer index.<\/p>\n<p>&nbsp;<\/p>\n<p>$countrycode[] = &#8216;Australia&#8217;;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In Above code, the index(key) is assigned to zero. In case an integer indexes are already assigned, then PHP take largest index +1 as index(key).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To delete an element from associative array, unset is used. In unset function passed, the key to delete particular element.<\/p>\n<p>&nbsp;<\/p>\n<p>unset($countrycode[&#8216;fr&#8217;]);<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">To delete , the entire array code<\/span><\/p>\n<\/div>\n<p>unset($countrycode)<\/p>\n<p>&nbsp;<\/p>\n<p>For variable substitution, you have to omit the quotes inside square bracket for a string key.<\/p>\n<p>&nbsp;<\/p>\n<p>echo &#8220;&lt;br&gt; $statecode[MP] &#8220;;<\/p>\n<p>&nbsp;<\/p>\n<p>or you can write<\/p>\n<p>&nbsp;<\/p>\n<p>echo &#8220;&lt;br&gt; {$statecode[&#8216;MH&#8217;]}&#8221;;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Foreach loop is also used in array to process all elements in an array which has gaps.<\/p>\n<p>&nbsp;<\/p>\n<p>$countrycode = array();<\/p>\n<p>$countrycode[&#8220;in&#8221;] = &#8220;India&#8221;;<\/p>\n<p>$countrycode[&#8216;ca&#8217;] = &#8220;Canada&#8221;;<\/p>\n<p>$countrycode[&#8216;fr&#8217;] = &#8220;France&#8221;;<\/p>\n<p>$countrycode[&#8216;usa&#8217;] = &#8220;America&#8221;;<\/p>\n<p>unset($countrycode[&#8216;fr&#8217;]);<\/p>\n<p>foreach($countrycode as $cc =&gt; $con)<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;t&gt; $cc =&gt; $con &#8220;;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Will display<\/p>\n<p>&nbsp;<\/p>\n<p>in =&gt; India ca =&gt; Canada usa =&gt; America<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Array<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/Sx4WsWRp_ZA\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>References:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\n<p style=\"text-align: justify\">2.W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional,Apress<\/p>\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K.Glass: Beginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O&#8217;Reilly Media<\/p>\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove:Professional PHP, Wrox<\/p>\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide,Pearson Education<\/p>\n<p style=\"text-align: justify\">11. http:\/\/www.php.net<\/p>\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>\n","protected":false},"author":3,"menu_order":8,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-hiren-joshi"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-138","chapter","type-chapter","status-publish","hentry","contributor-dr-hiren-joshi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":12,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/138\/revisions"}],"predecessor-version":[{"id":475,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/138\/revisions\/475"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/138\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapter-type?post=138"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/contributor?post=138"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/license?post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}