{"id":100,"date":"2026-05-20T09:34:43","date_gmt":"2026-05-20T09:34:43","guid":{"rendered":"https:\/\/gigz.pk\/php\/?post_type=lesson&#038;p=100"},"modified":"2026-05-21T14:39:17","modified_gmt":"2026-05-21T14:39:17","slug":"associative-arrays","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/php\/?lesson=associative-arrays","title":{"rendered":"Associative Arrays"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Associative arrays in PHP are arrays that use named keys instead of numeric indexes. These keys help store and access data in a meaningful and organized way. Associative arrays are commonly used to manage user information, product details, settings, and database records.<\/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 associative arrays in PHP<\/li>\n\n\n\n<li>Create and use associative arrays<\/li>\n\n\n\n<li>Access array values using keys<\/li>\n\n\n\n<li>Update associative array data<\/li>\n\n\n\n<li>Loop through associative arrays<\/li>\n\n\n\n<li>Use associative arrays in real-world applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is an Associative Array<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An associative array stores data in key-value pairs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$arrayName = array(<br>    \"key\" =&gt; \"value\"<br>);<\/code><\/pre>\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<h2 class=\"wp-block-heading\">Creating Associative Arrays<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can create associative arrays using the <code>array()<\/code> function or square brackets.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using array():<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$employee = array(<br>    \"id\" =&gt; 101,<br>    \"name\" =&gt; \"Ahmed\",<br>    \"department\" =&gt; \"IT\"<br>);<br>?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Using square brackets:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$product = &#91;<br>    \"name\" =&gt; \"Laptop\",<br>    \"price\" =&gt; 50000,<br>    \"brand\" =&gt; \"Dell\"<br>];<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing Associative Array Values<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the key name to access a value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$user = &#91;<br>    \"username\" =&gt; \"admin\",<br>    \"email\" =&gt; \"admin@example.com\"<br>];<br><br>echo $user&#91;\"email\"];<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Updating Associative Arrays<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can change existing values or add new values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$car = &#91;<br>    \"brand\" =&gt; \"Toyota\",<br>    \"model\" =&gt; \"Corolla\"<br>];<br><br>$car&#91;\"model\"] = \"Yaris\";<br>$car&#91;\"color\"] = \"White\";<br><br>print_r($car);<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Looping Through Associative Arrays<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Using foreach Loop<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$student = &#91;<br>    \"name\" =&gt; \"Sara\",<br>    \"age\" =&gt; 22,<br>    \"course\" =&gt; \"Web Development\"<br>];<br><br>foreach($student as $key =&gt; $value) {<br>    echo $key . \": \" . $value . \"&lt;br&gt;\";<br>}<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>name: Sara<br>age: 22<br>course: Web Development<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Multidimensional Associative Arrays<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Associative arrays can contain other arrays.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$employees = &#91;<br>    \"emp1\" =&gt; &#91;<br>        \"name\" =&gt; \"Ali\",<br>        \"department\" =&gt; \"HR\"<br>    ],<br>    \"emp2\" =&gt; &#91;<br>        \"name\" =&gt; \"Ahmed\",<br>        \"department\" =&gt; \"Finance\"<br>    ]<br>];<br><br>echo $employees&#91;\"emp1\"]&#91;\"name\"];<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Common Associative 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 number of elements.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$colors = &#91;<br>    \"first\" =&gt; \"Red\",<br>    \"second\" =&gt; \"Blue\"<br>];<br><br>echo count($colors);<br>?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">array_keys()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Returns all keys from the array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$data = &#91;<br>    \"name\" =&gt; \"Ali\",<br>    \"city\" =&gt; \"Karachi\"<br>];<br><br>print_r(array_keys($data));<br>?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">array_values()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Returns all values from the array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$data = &#91;<br>    \"name\" =&gt; \"Ali\",<br>    \"city\" =&gt; \"Karachi\"<br>];<br><br>print_r(array_values($data));<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Real World Uses of Associative Arrays<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Associative arrays are used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>User profile management<\/li>\n\n\n\n<li>Product information systems<\/li>\n\n\n\n<li>Student record systems<\/li>\n\n\n\n<li>Website settings<\/li>\n\n\n\n<li>Shopping carts<\/li>\n\n\n\n<li>Database result handling<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Associative Arrays<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Easy to organize data<\/li>\n\n\n\n<li>Meaningful key names improve readability<\/li>\n\n\n\n<li>Faster data access using keys<\/li>\n\n\n\n<li>Useful for structured information<\/li>\n\n\n\n<li>Ideal for dynamic web applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use descriptive key names<\/li>\n\n\n\n<li>Keep array structure consistent<\/li>\n\n\n\n<li>Avoid duplicate keys<\/li>\n\n\n\n<li>Use loops for efficient data handling<\/li>\n\n\n\n<li>Validate array keys before accessing values<\/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 associative arrays are<\/li>\n\n\n\n<li>Difference between indexed and associative arrays<\/li>\n\n\n\n<li>How to create associative arrays<\/li>\n\n\n\n<li>Accessing and updating values<\/li>\n\n\n\n<li>Using loops with associative arrays<\/li>\n\n\n\n<li>Real-world applications of associative 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 > Associative Arrays<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779269672214\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":26,"template":"","class_list":["post-100","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>Associative Arrays - Learn PHP with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn associative arrays in PHP with examples, loops, key value pairs, and real-world web development uses.\" \/>\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=associative-arrays\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Associative Arrays - Learn PHP with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn associative arrays in PHP with examples, loops, key value pairs, and real-world web development uses.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/php\/?lesson=associative-arrays\" \/>\n<meta property=\"og:site_name\" content=\"Learn PHP with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-21T14:39:17+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=associative-arrays\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=associative-arrays\",\"name\":\"Associative Arrays - Learn PHP with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\"},\"datePublished\":\"2026-05-20T09:34:43+00:00\",\"dateModified\":\"2026-05-21T14:39:17+00:00\",\"description\":\"Learn associative arrays in PHP with examples, loops, key value pairs, and real-world web development uses.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=associative-arrays#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=associative-arrays\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=associative-arrays#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/php\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate PHP > Arrays > Associative 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":"Associative Arrays - Learn PHP with GiGz.PK","description":"Learn associative arrays in PHP with examples, loops, key value pairs, and real-world web development uses.","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=associative-arrays","og_locale":"en_US","og_type":"article","og_title":"Associative Arrays - Learn PHP with GiGz.PK","og_description":"Learn associative arrays in PHP with examples, loops, key value pairs, and real-world web development uses.","og_url":"https:\/\/gigz.pk\/php\/?lesson=associative-arrays","og_site_name":"Learn PHP with GiGz.PK","article_modified_time":"2026-05-21T14:39:17+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=associative-arrays","url":"https:\/\/gigz.pk\/php\/?lesson=associative-arrays","name":"Associative Arrays - Learn PHP with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/php\/#website"},"datePublished":"2026-05-20T09:34:43+00:00","dateModified":"2026-05-21T14:39:17+00:00","description":"Learn associative arrays in PHP with examples, loops, key value pairs, and real-world web development uses.","breadcrumb":{"@id":"https:\/\/gigz.pk\/php\/?lesson=associative-arrays#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/php\/?lesson=associative-arrays"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/php\/?lesson=associative-arrays#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/php"},{"@type":"ListItem","position":2,"name":"Intermediate PHP > Arrays > Associative 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\/100","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=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}