{"id":7306,"date":"2019-10-21T05:38:35","date_gmt":"2019-10-21T05:38:35","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=7306"},"modified":"2021-11-16T05:44:54","modified_gmt":"2021-11-16T05:44:54","slug":"complete-reference-guide-of-php-arrays","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/complete-reference-guide-of-php-arrays\/","title":{"rendered":"Complete reference guide of PHP Arrays!"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is PHP Arrays<\/h2>\n\n\n\n<p>Arrays are complex variables that allow us to store more than one value or a group of values under a single variable name. Let&#8217;s suppose you want to store colors in your PHP script. Storing the colors one by one in a variable could look something like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span> \n$color1 = <span class=\"hljs-string\">\"Red\"<\/span>;\n$color2 = <span class=\"hljs-string\">\"Green\"<\/span>;\n$color3 = <span class=\"hljs-string\">\"Blue\"<\/span>; \n<span class=\"hljs-meta\">?&gt;<\/span><\/span> <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>But what, if you want to store the states or city names of a country in variables and this time this not just three may be hundred. It is quite hard, boring, and bad idea to store each city name in a separate variable. And here array comes into play. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Arrays in PHP<\/h2>\n\n\n\n<p>There are three types of arrays that you can create. These are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Indexed array<\/strong>&nbsp;\u2014 An array with a numeric key.<\/li><li><strong>Associative array<\/strong>&nbsp;\u2014 An array where each key has its own specific value.<\/li><li><strong>Multidimensional array<\/strong>&nbsp;\u2014 An array containing one or more arrays within itself.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Indexed Arrays<\/h2>\n\n\n\n<p>An indexed or numeric array stores each array element with a numeric index. The following examples shows two ways of creating an indexed array, the easiest way is:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span> \n<span class=\"hljs-comment\">\/\/ Define an indexed array <\/span>\n$colors = <span class=\"hljs-keyword\">array<\/span>(<span class=\"hljs-string\">\"Red\"<\/span>, <span class=\"hljs-string\">\"Green\"<\/span>, <span class=\"hljs-string\">\"Blue\"<\/span>); \n<span class=\"hljs-meta\">?&gt;<\/span><\/span> <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Note:<\/strong>&nbsp;In an indexed or numeric array, the indexes are automatically assigned and start with 0, and the values can be any data type. <\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Associative Arrays<\/h2>\n\n\n\n<p>In an associative array, the keys assigned to values can be arbitrary and user defined strings. In the following example the array uses keys instead of index numbers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span> \n<span class=\"hljs-comment\">\/\/ Define an associative array <\/span>\n$ages = <span class=\"hljs-keyword\">array<\/span>(<span class=\"hljs-string\">\"Peter\"<\/span>=&gt;<span class=\"hljs-number\">22<\/span>, <span class=\"hljs-string\">\"Clark\"<\/span>=&gt;<span class=\"hljs-number\">32<\/span>, <span class=\"hljs-string\">\"John\"<\/span>=&gt;<span class=\"hljs-number\">28<\/span>); \n\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Multidimensional Arrays<\/h2>\n\n\n\n<p>The multidimensional array is an array in which each element can also be an array and each element in the sub-array can be an array or further contain array within itself and so on. An example of a multidimensional array will look something like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-comment\">\/\/ Define a multidimensional array<\/span>\n$contacts = <span class=\"hljs-keyword\">array<\/span>(\n    <span class=\"hljs-keyword\">array<\/span>(\n        <span class=\"hljs-string\">\"name\"<\/span> =&gt; <span class=\"hljs-string\">\"Peter Parker\"<\/span>,\n        <span class=\"hljs-string\">\"email\"<\/span> =&gt; <span class=\"hljs-string\">\"peterparker@mail.com\"<\/span>,\n    ),\n    <span class=\"hljs-keyword\">array<\/span>(\n        <span class=\"hljs-string\">\"name\"<\/span> =&gt; <span class=\"hljs-string\">\"Clark Kent\"<\/span>,\n        <span class=\"hljs-string\">\"email\"<\/span> =&gt; <span class=\"hljs-string\">\"clarkkent@mail.com\"<\/span>,\n    ),\n    <span class=\"hljs-keyword\">array<\/span>(\n        <span class=\"hljs-string\">\"name\"<\/span> =&gt; <span class=\"hljs-string\">\"Harry Potter\"<\/span>,\n        <span class=\"hljs-string\">\"email\"<\/span> =&gt; <span class=\"hljs-string\">\"harrypotter@mail.com\"<\/span>,\n    )\n);\n<span class=\"hljs-comment\">\/\/ Access nested value<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Peter Parker's Email-id is: \"<\/span> . $contacts&#91;<span class=\"hljs-number\">0<\/span>]&#91;<span class=\"hljs-string\">\"email\"<\/span>];\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Viewing Array Structure and Values<\/h2>\n\n\n\n<p>You can see the structure and values of any array by using one of two statements \u2014&nbsp;<code>var_dump()<\/code>&nbsp;or&nbsp;<code>print_r()<\/code>. The&nbsp;<code>print_r()<\/code>&nbsp;statement, however, gives somewhat less information. Consider the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-comment\">\/\/ Define array<\/span>\n$cities = <span class=\"hljs-keyword\">array<\/span>(<span class=\"hljs-string\">\"London\"<\/span>, <span class=\"hljs-string\">\"Paris\"<\/span>, <span class=\"hljs-string\">\"New York\"<\/span>);\n \n<span class=\"hljs-comment\">\/\/ Display the cities array<\/span>\nprint_r($cities);\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The&nbsp;<code>print_r()<\/code>&nbsp;statement gives the following output:<br>Array ( [0] =&gt; London [1] =&gt; Paris [2] =&gt; New York )<br>This output shows the key and the value for each element in the array. To get more information, use the following statement: <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-comment\">\/\/ Define array<\/span>\n$cities = <span class=\"hljs-keyword\">array<\/span>(<span class=\"hljs-string\">\"London\"<\/span>, <span class=\"hljs-string\">\"Paris\"<\/span>, <span class=\"hljs-string\">\"New York\"<\/span>);\n \n<span class=\"hljs-comment\">\/\/ Display the cities array<\/span>\nvar_dump($cities);\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This&nbsp;<code>var_dump()<\/code>&nbsp;statement gives the following output:<br>array(3)\u2009{\u2009[0]=&gt;\u2009string(6) &#8220;London&#8221; [1]=&gt;\u2009string(5) &#8220;Paris&#8221; [2]=&gt;\u2009string(8) &#8220;New\u2009York&#8221;\u2009}<br><br> This output shows the data type of each element, such as a string of 6 characters, in addition to the key and value.  <\/p>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_55741\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_77903\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_55741\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>What is PHP Arrays Arrays are complex variables that allow us to store more than one value or a group of values under a single variable name. Let&#8217;s suppose you want to store colors in your PHP script. Storing the colors one by one in a variable could look something like this: But what, if&#8230;<\/p>\n","protected":false},"author":14,"featured_media":7343,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5150],"tags":[5432,5433,1122,177],"class_list":["post-7306","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-arrays","tag-assocative-arrays","tag-guide","tag-php"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=7306"}],"version-history":[{"count":10,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7306\/revisions"}],"predecessor-version":[{"id":25372,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7306\/revisions\/25372"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/7343"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=7306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=7306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=7306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}