{"id":15654,"date":"2020-07-02T17:58:03","date_gmt":"2020-07-02T17:58:03","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=15654"},"modified":"2022-04-28T09:15:23","modified_gmt":"2022-04-28T09:15:23","slug":"php-7-fundamental-tutorial-for-beginners-php-arrays","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/php-7-fundamental-tutorial-for-beginners-php-arrays\/","title":{"rendered":"PHP 7 Fundamental Tutorial for Beginners &#8211; PHP Arrays?"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">What is PHP Arrays<\/h3>\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\n<p><strong>Example<\/strong>:-<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/49e73f1974f5782d97dbbb48a6a63e5c.js\"><\/script>\n\n\n\n<p><strong>An array stores multiple values in one single variable:<\/strong><\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/b988b6fb918326c7eedc1d07581518e3.js\"><\/script>\n\n\n\n<h4 class=\"wp-block-heading\">Create an Array in PHP<\/h4>\n\n\n\n<p>In PHP, the&nbsp;<code>array()<\/code>&nbsp;function is used to create an array:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>array();<\/td><\/tr><\/tbody><\/table><\/figure>\n\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<h3 class=\"wp-block-heading\">Types of Arrays in PHP<\/h3>\n\n\n\n<p><strong>There are three types of arrays that you can create. These are:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong><strong>Indexed array<\/strong>&nbsp;\u2014 An array with a numeric key.<\/strong><\/li><li><strong>Associative array<\/strong>&nbsp;\u2014 An array where each key has its own specific value<\/li><li><strong>Multidimensional arrays<\/strong>&nbsp;&#8211; Arrays containing one or more arrays<\/li><\/ol>\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\n<p><strong>Example:-<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/81b2a9ffa28f0eff035e4bb7d5b8db9c.js\"><\/script>\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\n<p><strong>Example:-<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/89b8b5d9845dffe0e82a0eb413b5d685.js\"><\/script>\n\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\n<p><strong>Example:-<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/326b9b403a42c7bdda58a19d1ecee987.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">Viewing Array Structure and Values<\/h3>\n\n\n\n<p>You can see the structure and values of an 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;the statement, however, gives somewhat less information. Consider the following <strong>Example:<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/c8f175533afb56cd6671a42d794a5a21.js\"><\/script>\n\n\n\n<p>The&nbsp;<code>print_r()<\/code>&nbsp;statement gives the following output:Array ( [0] =&gt; London [1] =&gt; Paris [2] =&gt; New York )<\/p>\n\n\n\n<p>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\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/29ca8e31f4b63f62139d52bc48554f88.js\"><\/script>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_19409\"><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_45112\"  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_19409\"  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: Example:- An array&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"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":[490,6197,6196,6198],"class_list":["post-15654","post","type-post","status-publish","format-standard","hentry","category-php","tag-array","tag-associative-arrays","tag-indexed-arrays","tag-multidimensional-arrays"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15654","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=15654"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15654\/revisions"}],"predecessor-version":[{"id":24697,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15654\/revisions\/24697"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=15654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=15654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=15654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}