{"id":94,"date":"2026-05-20T09:30:57","date_gmt":"2026-05-20T09:30:57","guid":{"rendered":"https:\/\/gigz.pk\/php\/?post_type=lesson&#038;p=94"},"modified":"2026-05-21T14:39:10","modified_gmt":"2026-05-21T14:39:10","slug":"indexed-arrays","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/php\/?lesson=indexed-arrays","title":{"rendered":"Indexed Arrays"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction to Arrays<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Arrays in PHP are used to store multiple values in a single variable. Instead of creating separate variables for each value, arrays allow you to organize related data together.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Arrays are useful when working with lists of names, numbers, products, records, and other grouped information in web applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Objectives<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this training, you will be able to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Understand the concept of arrays<\/li>\n\n\n\n<li>Create and use arrays in PHP<\/li>\n\n\n\n<li>Access array elements<\/li>\n\n\n\n<li>Modify array values<\/li>\n\n\n\n<li>Use different types of arrays<\/li>\n\n\n\n<li>Loop through arrays<\/li>\n\n\n\n<li>Apply array functions in PHP<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is an Array<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An array is a special variable that can hold more than one value at a time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$colors = array(\"Red\", \"Blue\", \"Green\");<br>?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, the variable <code>$colors<\/code> stores three different values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Arrays<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Arrays help developers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Store multiple values efficiently<\/li>\n\n\n\n<li>Organize data in a structured way<\/li>\n\n\n\n<li>Reduce repetitive variables<\/li>\n\n\n\n<li>Simplify data processing<\/li>\n\n\n\n<li>Manage dynamic web content<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Arrays in PHP<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PHP supports three main types of arrays:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Indexed Arrays<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Indexed arrays use numeric indexes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$fruits = array(\"Apple\", \"Banana\", \"Mango\");<br><br>echo $fruits&#91;0];<br>?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Apple<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Associative Arrays<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Associative arrays use named keys instead of numbers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$student = array(<br>    \"name\" =&gt; \"Ali\",<br>    \"age\" =&gt; 20,<br>    \"city\" =&gt; \"Lahore\"<br>);<br><br>echo $student&#91;\"name\"];<br>?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Ali<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Multidimensional Arrays<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Multidimensional arrays contain multiple arrays inside another array.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$students = array(<br>    array(\"Ali\", 20),<br>    array(\"Ahmed\", 22),<br>    array(\"Sara\", 19)<br>);<br><br>echo $students&#91;0]&#91;0];<br>?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Ali<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing Array Elements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can access array elements using indexes or keys.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$cars = array(\"Toyota\", \"Honda\", \"BMW\");<br><br>echo $cars&#91;1];<br>?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Honda<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Updating Array Values<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can modify existing array values easily.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$colors = array(\"Red\", \"Blue\", \"Green\");<br><br>$colors&#91;1] = \"Black\";<br><br>echo $colors&#91;1];<br>?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Black<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Adding New Elements to Arrays<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$animals = array(\"Cat\", \"Dog\");<br><br>$animals&#91;] = \"Horse\";<br><br>print_r($animals);<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Looping Through Arrays<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Using For Loop<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$numbers = array(10, 20, 30);<br><br>for($i = 0; $i &lt; count($numbers); $i++) {<br>    echo $numbers&#91;$i];<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Using Foreach Loop<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$colors = array(\"Red\", \"Blue\", \"Green\");<br><br>foreach($colors as $color) {<br>    echo $color;<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Common Array Functions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">count()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Returns the total number of elements.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$items = array(\"Pen\", \"Book\", \"Bag\");<br><br>echo count($items);<br>?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">sort()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Sorts an array in ascending order.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$numbers = array(40, 10, 30, 20);<br><br>sort($numbers);<br><br>print_r($numbers);<br>?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">array_push()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Adds elements to the end of an array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$fruits = array(\"Apple\", \"Banana\");<br><br>array_push($fruits, \"Orange\");<br><br>print_r($fruits);<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Real World Uses of Arrays<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Arrays are commonly used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Student management systems<\/li>\n\n\n\n<li>Shopping carts<\/li>\n\n\n\n<li>Product listings<\/li>\n\n\n\n<li>User data storage<\/li>\n\n\n\n<li>Website menus<\/li>\n\n\n\n<li>Reports and dashboards<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Arrays<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Easy data management<\/li>\n\n\n\n<li>Faster data processing<\/li>\n\n\n\n<li>Better code organization<\/li>\n\n\n\n<li>Reusable and scalable<\/li>\n\n\n\n<li>Useful for dynamic applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Arrays<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use meaningful variable names<\/li>\n\n\n\n<li>Choose the correct array type<\/li>\n\n\n\n<li>Keep arrays organized<\/li>\n\n\n\n<li>Avoid unnecessary nested arrays<\/li>\n\n\n\n<li>Use built-in PHP array functions for efficiency<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Final Presentation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In your final presentation, explain:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What arrays are in PHP<\/li>\n\n\n\n<li>Types of arrays<\/li>\n\n\n\n<li>How to create and access arrays<\/li>\n\n\n\n<li>Difference between indexed and associative arrays<\/li>\n\n\n\n<li>Array loops and functions<\/li>\n\n\n\n<li>Real-world applications of arrays<\/li>\n<\/ul>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/php\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate PHP > Arrays > Indexed Arrays<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779269418692\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":25,"template":"","class_list":["post-94","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Indexed Arrays - Learn PHP with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn PHP arrays with examples including indexed, associative, and multidimensional arrays for web development.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/php\/?lesson=indexed-arrays\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Indexed Arrays - Learn PHP with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn PHP arrays with examples including indexed, associative, and multidimensional arrays for web development.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/php\/?lesson=indexed-arrays\" \/>\n<meta property=\"og:site_name\" content=\"Learn PHP with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-21T14:39:10+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=indexed-arrays\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=indexed-arrays\",\"name\":\"Indexed Arrays - Learn PHP with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\"},\"datePublished\":\"2026-05-20T09:30:57+00:00\",\"dateModified\":\"2026-05-21T14:39:10+00:00\",\"description\":\"Learn PHP arrays with examples including indexed, associative, and multidimensional arrays for web development.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=indexed-arrays#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=indexed-arrays\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=indexed-arrays#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/php\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate PHP > Arrays > Indexed Arrays\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/\",\"name\":\"Learn PHP with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Indexed Arrays - Learn PHP with GiGz.PK","description":"Learn PHP arrays with examples including indexed, associative, and multidimensional arrays for web development.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/php\/?lesson=indexed-arrays","og_locale":"en_US","og_type":"article","og_title":"Indexed Arrays - Learn PHP with GiGz.PK","og_description":"Learn PHP arrays with examples including indexed, associative, and multidimensional arrays for web development.","og_url":"https:\/\/gigz.pk\/php\/?lesson=indexed-arrays","og_site_name":"Learn PHP with GiGz.PK","article_modified_time":"2026-05-21T14:39:10+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/php\/?lesson=indexed-arrays","url":"https:\/\/gigz.pk\/php\/?lesson=indexed-arrays","name":"Indexed Arrays - Learn PHP with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/php\/#website"},"datePublished":"2026-05-20T09:30:57+00:00","dateModified":"2026-05-21T14:39:10+00:00","description":"Learn PHP arrays with examples including indexed, associative, and multidimensional arrays for web development.","breadcrumb":{"@id":"https:\/\/gigz.pk\/php\/?lesson=indexed-arrays#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/php\/?lesson=indexed-arrays"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/php\/?lesson=indexed-arrays#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/php"},{"@type":"ListItem","position":2,"name":"Intermediate PHP > Arrays > Indexed Arrays"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/php\/#website","url":"https:\/\/gigz.pk\/php\/","name":"Learn PHP with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}