{"id":199,"date":"2016-07-27T16:48:30","date_gmt":"2016-07-27T16:48:30","guid":{"rendered":"https:\/\/barkhane.com\/php\/2016\/07\/27\/associative-arrays-php\/"},"modified":"2024-03-03T17:33:32","modified_gmt":"2024-03-03T17:33:32","slug":"associative-arrays-php","status":"publish","type":"post","link":"https:\/\/barkhane.com\/php\/associative-arrays-php\/","title":{"rendered":"Associative Arrays in php"},"content":{"rendered":"<h2>What is Associative arrays in PHP Or Key Paired array in PHP ?<\/h2>\n<p>Associative arrays in PHP\u00a0 are arrays that use strings as index assign by you .This stores element values in association with key values rather than in a conventional linear index order.Associative array will have their index as string so that you can establish a strong association between key and values. Associative arrays used like a structure by specify string key instread of numeric index.<\/p>\n<p>eg. \\&#8217;Key\\&#8217; =&gt;\\&#8217;value\\&#8217; \u00a0, where<strong>\u00a0\\&#8217;=&gt;\\&#8217;<\/strong> is key value operator<\/p>\n<p>There are two ways to create an associative array:<\/p>\n<pre><code><\/code><\/pre>\n<ul>\n<li>\u00a0$population\u00a0= array(\\&#8221;India\\&#8221;=&gt;\\&#8221;100\\&#8221;, \\&#8221;China\\&#8221;=&gt;\\&#8221;120\\&#8221;, \\&#8221;USA\\&#8221;=&gt;\\&#8221;90\\&#8221;);<\/li>\n<li>$population\u00a0[\\&#8217;India\\&#8217;] = \\&#8221;100\\&#8221;; $population\u00a0[\\&#8217;Ben\\&#8217;] = \\&#8221;120\\&#8221;; $population\u00a0[\\&#8217;USA\\&#8217;] = \\&#8221;90\\&#8221;;<\/li>\n<\/ul>\n<pre><code><\/code><\/pre>\n<p>In both the condition we are storing the value with respect to country in $population array. You can use any one of them as required .<\/p>\n<p>Note: In\u00a0Associative arrays index would not work in place of string key.it means\u00a0$population[1] will not provide any result.<\/p>\n<h2>How to Use\u00a0Associative arrays ?<\/h2>\n<p>As we seen above how to create or store value in associative array, now if we want to use array we have to use foreach loop to fetch values from array .<\/p>\n<p>like for an example<\/p>\n<pre><code>   \n\nforeach($population \u00a0as $key=&gt;$value)\n\n{\n\necho\\\"$key =$value &lt;\/br&gt;\\\"\n\n}\n  <\/code><\/pre>\n<p>Working with\u00a0associative array can also be done with Stack implementation , With the using of stack in array you can add or delete element in array.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Associative arrays in PHP Or Key Paired array in PHP ? Associative arrays in PHP\u00a0 are arrays that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3],"tags":[],"class_list":["post-199","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/comments?post=199"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/199\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/media?parent=199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/categories?post=199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/tags?post=199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}