{"id":192,"date":"2016-07-27T15:06:24","date_gmt":"2016-07-27T15:06:24","guid":{"rendered":"https:\/\/barkhane.com\/php\/2016\/07\/27\/what-are-arrays-in-php\/"},"modified":"2024-03-03T15:04:06","modified_gmt":"2024-03-03T15:04:06","slug":"what-are-arrays-in-php","status":"publish","type":"post","link":"https:\/\/barkhane.com\/php\/what-are-arrays-in-php\/","title":{"rendered":"Arrays in PHP"},"content":{"rendered":"<h2>What are Arrays in PHP ?<\/h2>\n<p>In <a href=\"https:\/\/barkhane.com\/php\/what-is-php\/\" target=\"_blank\" rel=\"noopener\">PHP <\/a>array is the collection of elements with common identifiers (identifiers are used to <b>name<\/b>\u00a0<a href=\"https:\/\/barkhane.com\/php\/php-variable-deceleration\/\" target=\"_blank\" rel=\"noopener\">variables <\/a><b>,<\/b>\u00a0functions, constants, and classes),can have different <a href=\"https:\/\/barkhane.com\/php\/understanding-php-data-type\/\" target=\"_blank\" rel=\"noopener\">Datatypes \u00a0<\/a>and continuous in memory .<\/p>\n<p><strong>Important points Related to Array in PHP\u00a0<\/strong><\/p>\n<ul>\n<li>To declare array ,array function is used $array[].<\/li>\n<li>In array subscript starts with \\&#8217;Zero\\'(0).<\/li>\n<li>In PHP Array can be declared without element.<\/li>\n<li>Array in PHP \u00a0support interpolation.<\/li>\n<li>Array in PHP \u00a0 are using dynamic memory allocation ie they are resized automatically as element are assigned or added to array<\/li>\n<\/ul>\n<p><strong>Types Of Array:<\/strong><\/p>\n<ul>\n<li><a href=\"#Indexedarrays\"><b>Indexed arrays<\/b> <\/a>&#8211; Arrays with numeric index<\/li>\n<li><a href=\"https:\/\/barkhane.com\/php\/associative-arrays-php\/\" target=\"_blank\" rel=\"noopener\"><b>Associative arrays<\/b><\/a> &#8211; Arrays with named keys<\/li>\n<li><a href=\"https:\/\/barkhane.com\/php\/php-multidimensional-array\/\"><b>Multidimensional arrays<\/b><\/a> &#8211; Arrays containing one or more arrays<\/li>\n<\/ul>\n<h2>How to define array in php?<\/h2>\n<pre><code>   \n$collect=<strong>array<\/strong>(\\'red\\',\\'yellow\\',\\'123\\',false);  <\/code><\/pre>\n<p>In above line <strong>$collect<\/strong> is array variable ,<strong>array<\/strong> is syntax of array function and red , yellow ,123,false are \u00a0the element of array.<\/p>\n<p>there are other ways to declare array in PHP<\/p>\n<p>for example<\/p>\n<pre><code>   \n$collection[]=\\'city\\';\n\n$collection[]=\\'phone\\';\n\n$collection[]=\\'dob\\';  <\/code><\/pre>\n<p>in above example we are adding element in array\u00a0 $collection[] simply adding the value in array variable.<\/p>\n<p>we also add element in array thru Index like<\/p>\n<pre><code>  \n\n$collection[3]=\\'sport\\';\n\n$collection[4]=\\'school\\';\n   <\/code><\/pre>\n<p>these type of array called <b id=\"Indexedarrays\">Indexed arrays\u00a0<\/b>these different method are used according to requirement .<\/p>\n<p>To find out length of an array or number of element in array we can use count() function .<\/p>\n<p>eg. count ($collection);<\/p>\n<p>output will be 5 since we are added 5 element in array form 0-city to 4-school.<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>How to display or use the value of an array<\/strong><\/h3>\n<ul>\n<li><strong>print_r($collection)<\/strong>; function is use to print formatted output of an array.<\/li>\n<li>another method is to use for loop to use an array\u00a0eg.<\/li>\n<\/ul>\n<pre><code>  \nfor($i=0;i&lt;$n; $i++)\n{\necho\\\"Element at i Index is $collection[$i]\\\";\n}\n<\/code><\/pre>\n<p style=\"text-align: left\"><strong>Useful Function of array in PHP<\/strong><\/p>\n<ul>\n<li style=\"text-align: left\">shuffle array<\/li>\n<li style=\"text-align: left\">sort\u00a0array<\/li>\n<li style=\"text-align: left\">slice\u00a0array<\/li>\n<li style=\"text-align: left\">merge\u00a0array<\/li>\n<\/ul>\n<p><strong>Shuffle array :<\/strong> \u00a0\u00a0This function\u00a0is use to\u00a0shuffle\u00a0or \u00a0randomizes the order of the elements in an\u00a0array.<\/p>\n<pre><code>  \nSyntax: shuffle($array_name);   <\/code><\/pre>\n<p><strong>Sort\u00a0array<\/strong> :\u00a0This function sort arrays in ascending order, To\u00a0 sort arrays in descending order we use\u00a0\u00a0rsort()<\/p>\n<p>Syntax: <strong>sort($array_name)<\/strong>;\u00a0<strong>rsort($array_name)<\/strong><\/p>\n<p>Other sort function<\/p>\n<ul>\n<li><strong>asort()<\/strong> &#8211; sort associative arrays in ascending order, according to the value<\/li>\n<li><strong>ksort()<\/strong> &#8211; sort associative arrays in ascending order, according to the key<\/li>\n<li><strong>arsort()<\/strong> &#8211; sort associative arrays in descending order, according to the value<\/li>\n<li><strong>krsort()<\/strong> &#8211; sort associative arrays in descending order, according to the key<\/li>\n<\/ul>\n<p><strong>Slice\u00a0array :<\/strong>if you wanted some element from an array then you can use slice function<\/p>\n<p>$array = <strong>array_slice($array, 0, 2)<\/strong>;<\/p>\n<p>The second parameter is the start point ( 0 means to start from the beginning of the array).<\/p>\n<p>The third parameter is the length of the resulting array<\/p>\n<p><strong>Merge\u00a0array:<\/strong> To merge to array wen can use <strong>array_merge()<\/strong> function<\/p>\n<pre><code>   \n\n<strong>$array = array_merge ($array1,$array2);<\/strong>\n  <\/code><\/pre>\n<p>the above will result an array consisting element of both the array.<\/p>\n<table>\n<tbody>\n<tr>\n<th>\n<h4>Array Function syantax<\/h4>\n<\/th>\n<th>\n<h4>Description of function<\/h4>\n<\/th>\n<\/tr>\n<tr>\n<td>array()<\/td>\n<td>Creates an array<\/td>\n<\/tr>\n<tr>\n<td>array_change_key_case()<\/td>\n<td>Changes all keys in an array to lowercase or uppercase<\/td>\n<\/tr>\n<tr>\n<td>array_chunk()<\/td>\n<td>Splits an array into chunks of arrays<\/td>\n<\/tr>\n<tr>\n<td>array_column()<\/td>\n<td>Returns the values from a single column in the input array<\/td>\n<\/tr>\n<tr>\n<td>array_combine()<\/td>\n<td>Creates an array by using the elements from one \\&#8221;keys\\&#8221; array and one \\&#8221;values\\&#8221; array<\/td>\n<\/tr>\n<tr>\n<td>array_count_values()<\/td>\n<td>Counts all the values of an array<\/td>\n<\/tr>\n<tr>\n<td>array_diff()<\/td>\n<td>Compare arrays, and returns the differences (compare values only)<\/td>\n<\/tr>\n<tr>\n<td>array_diff_assoc()<\/td>\n<td>Compare arrays, and returns the differences (compare keys and values)<\/td>\n<\/tr>\n<tr>\n<td>array_diff_key()<\/td>\n<td>Compare arrays, and returns the differences (compare keys only)<\/td>\n<\/tr>\n<tr>\n<td>array_diff_uassoc()<\/td>\n<td>Compare arrays, and returns the differences (compare keys and values, using a user-defined key comparison function)<\/td>\n<\/tr>\n<tr>\n<td>array_diff_ukey()<\/td>\n<td>Compare arrays, and returns the differences (compare keys only, using a user-defined key comparison function)<\/td>\n<\/tr>\n<tr>\n<td>array_fill()<\/td>\n<td>Fills an array with values<\/td>\n<\/tr>\n<tr>\n<td>array_fill_keys()<\/td>\n<td>Fills an array with values, specifying keys<\/td>\n<\/tr>\n<tr>\n<td>array_filter()<\/td>\n<td>Filters the values of an array using a callback function<\/td>\n<\/tr>\n<tr>\n<td>&gt;array_flip()<\/td>\n<td>Flips\/Exchanges all keys with their associated values in an array<\/td>\n<\/tr>\n<tr>\n<td>array_intersect()<\/td>\n<td>Compare arrays, and returns the matches (compare values only)<\/td>\n<\/tr>\n<tr>\n<td>array_intersect_assoc()<\/td>\n<td>Compare arrays and returns the matches (compare keys and values)<\/td>\n<\/tr>\n<tr>\n<td>array_intersect_key()<\/td>\n<td>Compare arrays, and returns the matches (compare keys only)<\/td>\n<\/tr>\n<tr>\n<td>array_intersect_uassoc()<\/td>\n<td>Compare arrays, and returns the matches (compare keys and values, using a user-defined key comparison function)<\/td>\n<\/tr>\n<tr>\n<td>array_intersect_ukey()<\/td>\n<td>Compare arrays, and returns the matches (compare keys only, using a user-defined key comparison function)<\/td>\n<\/tr>\n<tr>\n<td>array_key_exists()<\/td>\n<td>Checks if the specified key exists in the array<\/td>\n<\/tr>\n<tr>\n<td>array_keys()<\/td>\n<td>Returns all the keys of an array<\/td>\n<\/tr>\n<tr>\n<td>array_map()<\/td>\n<td>Sends each value of an array to a user-made function, which returns new values<\/td>\n<\/tr>\n<tr>\n<td>array_merge()<\/td>\n<td>Merges one or more arrays into one array<\/td>\n<\/tr>\n<tr>\n<td>array_merge_recursive()<\/td>\n<td>Merges one or more arrays into one array recursively<\/td>\n<\/tr>\n<tr>\n<td>array_multisort()<\/td>\n<td>Sorts multiple or multi-dimensional arrays<\/td>\n<\/tr>\n<tr>\n<td>array_pad()<\/td>\n<td>Inserts a specified number of items, with a specified value, to an array<\/td>\n<\/tr>\n<tr>\n<td>array_pop()<\/td>\n<td>Deletes the last element of an array<\/td>\n<\/tr>\n<tr>\n<td>array_product()<\/td>\n<td>Calculates the product of the values in an array<\/td>\n<\/tr>\n<tr>\n<td>array_push()<\/td>\n<td>Inserts one or more elements to the end of an array<\/td>\n<\/tr>\n<tr>\n<td>array_rand()<\/td>\n<td>Returns one or more random keys from an array<\/td>\n<\/tr>\n<tr>\n<td>array_reduce()<\/td>\n<td>Returns an array as a string, using a user-defined function<\/td>\n<\/tr>\n<tr>\n<td>array_replace()<\/td>\n<td>Replaces the values of the first array with the values from following arrays<\/td>\n<\/tr>\n<tr>\n<td>array_replace_recursive()<\/td>\n<td>Replaces the values of the first array with the values from following arrays recursively<\/td>\n<\/tr>\n<tr>\n<td>array_reverse()<\/td>\n<td>Returns an array in the reverse order<\/td>\n<\/tr>\n<tr>\n<td>array_search()<\/td>\n<td>Searches an array for a given value and returns the key<\/td>\n<\/tr>\n<tr>\n<td>array_shift()<\/td>\n<td>Removes the first element from an array, and returns the value of the removed element<\/td>\n<\/tr>\n<tr>\n<td>array_slice()<\/td>\n<td>Returns selected parts of an array<\/td>\n<\/tr>\n<tr>\n<td>array_splice()<\/td>\n<td>Removes and replaces specified elements of an array<\/td>\n<\/tr>\n<tr>\n<td>&gt;array_sum()<\/td>\n<td>Returns the sum of the values in an array<\/td>\n<\/tr>\n<tr>\n<td>array_udiff()<\/td>\n<td>Compare arrays, and returns the differences (compare values only, using a user-defined key comparison function)<\/td>\n<\/tr>\n<tr>\n<td>&gt;array_udiff_assoc()<\/td>\n<td>Compare arrays, and returns the differences (compare keys and values, using a built-in function to compare the keys and a user-defined function to compare the values)<\/td>\n<\/tr>\n<tr>\n<td>array_udiff_uassoc()<\/td>\n<td>Compare arrays, and returns the differences (compare keys and values, using two user-defined key comparison functions)<\/td>\n<\/tr>\n<tr>\n<td>array_uintersect()<\/td>\n<td>Compare arrays, and returns the matches (compare values only, using a user-defined key comparison function)<\/td>\n<\/tr>\n<tr>\n<td>array_uintersect_assoc()<\/td>\n<td>Compare arrays, and returns the matches (compare keys and values, using a built-in function to compare the keys and a user-defined function to compare the values)<\/td>\n<\/tr>\n<tr>\n<td>array_uintersect_uassoc()<\/td>\n<td>Compare arrays, and returns the matches (compare keys and values, using two user-defined key comparison functions)<\/td>\n<\/tr>\n<tr>\n<td>array_unique()<\/td>\n<td>Removes duplicate values from an array<\/td>\n<\/tr>\n<tr>\n<td>array_unshift()<\/td>\n<td>Adds one or more elements to the beginning of an array<\/td>\n<\/tr>\n<tr>\n<td>array_values()<\/td>\n<td>Returns all the values of an array<\/td>\n<\/tr>\n<tr>\n<td>array_walk()<\/td>\n<td>Applies a user function to every member of an array<\/td>\n<\/tr>\n<tr>\n<td>array_walk_recursive()<\/td>\n<td>Applies a user function recursively to every member of an array<\/td>\n<\/tr>\n<tr>\n<td>arsort()<\/td>\n<td>Sorts an associative array in descending order, according to the value<\/td>\n<\/tr>\n<tr>\n<td>asort()<\/td>\n<td>Sorts an associative array in ascending order, according to the value<\/td>\n<\/tr>\n<tr>\n<td>compact()<\/td>\n<td>Create array containing variables and their values<\/td>\n<\/tr>\n<tr>\n<td>count()<\/td>\n<td>Returns the number of elements in an array<\/td>\n<\/tr>\n<tr>\n<td>current()<\/td>\n<td>Returns the current element in an array<\/td>\n<\/tr>\n<tr>\n<td>each()<\/td>\n<td>Returns the current key and value pair from an array<\/td>\n<\/tr>\n<tr>\n<td>end()<\/td>\n<td>Sets the internal pointer of an array to its last element<\/td>\n<\/tr>\n<tr>\n<td>extract()<\/td>\n<td>Imports variables into the current symbol table from an array<\/td>\n<\/tr>\n<tr>\n<td>in_array()<\/td>\n<td>Checks if a specified value exists in an array<\/td>\n<\/tr>\n<tr>\n<td>key()<\/td>\n<td>Fetches a key from an array<\/td>\n<\/tr>\n<tr>\n<td>krsort()<\/td>\n<td>Sorts an associative array in descending order, according to the key<\/td>\n<\/tr>\n<tr>\n<td>ksort()<\/td>\n<td>Sorts an associative array in ascending order, according to the key<\/td>\n<\/tr>\n<tr>\n<td>list()<\/td>\n<td>Assigns variables as if they were an array<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>Sorts an array using a case insensitive \\&#8221;natural order\\&#8221; algorithm<\/td>\n<\/tr>\n<tr>\n<td>natsort()<\/td>\n<td>Sorts an array using a \\&#8221;natural order\\&#8221; algorithm<\/td>\n<\/tr>\n<tr>\n<td>next()<\/td>\n<td>Advance the internal array pointer of an array<\/td>\n<\/tr>\n<tr>\n<td>pos()<\/td>\n<td>Alias of current()<\/td>\n<\/tr>\n<tr>\n<td>prev()<\/td>\n<td>Rewinds the internal array pointer<\/td>\n<\/tr>\n<tr>\n<td>range()<\/td>\n<td>Creates an array containing a range of elements<\/td>\n<\/tr>\n<tr>\n<td>reset()<\/td>\n<td>Sets the internal pointer of an array to its first element<\/td>\n<\/tr>\n<tr>\n<td>rsort()<\/td>\n<td>Sorts an indexed array in descending order<\/td>\n<\/tr>\n<tr>\n<td>shuffle()<\/td>\n<td>Shuffles an array<\/td>\n<\/tr>\n<tr>\n<td>sizeof()<\/td>\n<td>Alias of count()<\/td>\n<\/tr>\n<tr>\n<td>sort()<\/td>\n<td>Sorts an indexed array in ascending order<\/td>\n<\/tr>\n<tr>\n<td>uasort()<\/td>\n<td>Sorts an array by values using a user-defined comparison function<\/td>\n<\/tr>\n<tr>\n<td>uksort()<\/td>\n<td>Sorts an array by keys using a user-defined comparison function<\/td>\n<\/tr>\n<tr>\n<td>usort()<\/td>\n<td>Sorts an array using a user-defined comparison function<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>What are Arrays in PHP ? In PHP array is the collection of elements with common identifiers (identifiers are used [&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-192","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/192","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=192"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/192\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/media?parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/categories?post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/tags?post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}