CINXE.COM

PHP: Arrays - Manual

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP: Arrays - Manual </title> <link rel="icon" type="image/svg+xml" sizes="any" href="https://www.php.net/favicon.svg?v=2"> <link rel="icon" type="image/png" sizes="196x196" href="https://www.php.net/favicon-196x196.png?v=2"> <link rel="icon" type="image/png" sizes="32x32" href="https://www.php.net/favicon-32x32.png?v=2"> <link rel="icon" type="image/png" sizes="16x16" href="https://www.php.net/favicon-16x16.png?v=2"> <link rel="shortcut icon" href="https://www.php.net/favicon.ico?v=2"> <link rel="search" type="application/opensearchdescription+xml" href="http://php.net/phpnetimprovedsearch.src" title="Add PHP.net search"> <link rel="alternate" type="application/atom+xml" href="https://www.php.net/releases/feed.php" title="PHP Release feed"> <link rel="alternate" type="application/atom+xml" href="https://www.php.net/feed.atom" title="PHP: Hypertext Preprocessor"> <link rel="canonical" href="https://www.php.net/manual/en/language.types.array.php"> <link rel="shorturl" href="https://www.php.net/types.array"> <link rel="alternate" href="https://www.php.net/types.array" hreflang="x-default"> <link rel="contents" href="https://www.php.net/manual/en/index.php"> <link rel="index" href="https://www.php.net/manual/en/language.types.php"> <link rel="prev" href="https://www.php.net/manual/en/language.types.numeric-strings.php"> <link rel="next" href="https://www.php.net/manual/en/language.types.object.php"> <link rel="alternate" href="https://www.php.net/manual/en/language.types.array.php" hreflang="en"> <link rel="alternate" href="https://www.php.net/manual/de/language.types.array.php" hreflang="de"> <link rel="alternate" href="https://www.php.net/manual/es/language.types.array.php" hreflang="es"> <link rel="alternate" href="https://www.php.net/manual/fr/language.types.array.php" hreflang="fr"> <link rel="alternate" href="https://www.php.net/manual/it/language.types.array.php" hreflang="it"> <link rel="alternate" href="https://www.php.net/manual/ja/language.types.array.php" hreflang="ja"> <link rel="alternate" href="https://www.php.net/manual/pt_BR/language.types.array.php" hreflang="pt_BR"> <link rel="alternate" href="https://www.php.net/manual/ru/language.types.array.php" hreflang="ru"> <link rel="alternate" href="https://www.php.net/manual/tr/language.types.array.php" hreflang="tr"> <link rel="alternate" href="https://www.php.net/manual/uk/language.types.array.php" hreflang="uk"> <link rel="alternate" href="https://www.php.net/manual/zh/language.types.array.php" hreflang="zh"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1707321815&amp;f=/fonts/Fira/fira.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1707321815&amp;f=/fonts/Font-Awesome/css/fontello.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1738926002&amp;f=/styles/theme-base.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1730558402&amp;f=/styles/theme-medium.css" media="screen"> <base href="https://www.php.net/manual/en/language.types.array.php"> <meta name="Description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <meta name="twitter:card" content="summary_large_image" /> <meta name="twitter:site" content="@official_php" /> <meta name="twitter:title" content="PHP: Hypertext Preprocessor" /> <meta name="twitter:description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <meta name="twitter:creator" content="@official_php" /> <meta name="twitter:image:src" content="https://www.php.net/images/meta-image.png" /> <meta itemprop="name" content="PHP: Hypertext Preprocessor" /> <meta itemprop="description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <meta itemprop="image" content="https://www.php.net/images/meta-image.png" /> <meta property="og:image" content="https://www.php.net/images/meta-image.png" /> <meta property="og:description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <link href="https://fosstodon.org/@php" rel="me" /> <!-- Matomo --> <script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(["setDoNotTrack", true]); _paq.push(["disableCookies"]); _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="https://analytics.php.net/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '1']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script> <!-- End Matomo Code --> </head> <body class="docs "> <nav class="navbar navbar-fixed-top"> <div class="navbar__inner"> <a href="/" aria-label="PHP Home" class="navbar__brand"> <img src="/images/logos/php-logo-white.svg" aria-hidden="true" width="80" height="40" > </a> <div id="navbar__offcanvas" tabindex="-1" class="navbar__offcanvas" aria-label="Menu" > <button id="navbar__close-button" class="navbar__icon-item navbar_icon-item--visually-aligned navbar__close-button" > <svg xmlns="http://www.w3.org/2000/svg" width="24" viewBox="0 0 24 24" fill="currentColor"><path d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z" /></svg> </button> <ul class="navbar__nav"> <li class="navbar__item"> <a href="/downloads.php" class="navbar__link " > Downloads </a> </li> <li class="navbar__item"> <a href="/docs.php" aria-current="page" class="navbar__link navbar__link--active " > Documentation </a> </li> <li class="navbar__item"> <a href="/get-involved.php" class="navbar__link " > Get Involved </a> </li> <li class="navbar__item"> <a href="/support.php" class="navbar__link " > Help </a> </li> <li class="navbar__item"> <a href="/releases/8.4/index.php" class="navbar__link navbar__release" > <img src="/images/php8/logo_php8_4.svg" alt="PHP 8.4"> </a> </li> </ul> </div> <div class="navbar__right"> <!-- Desktop default search --> <form action="/manual-lookup.php" class="navbar__search-form" > <label for="navbar__search-input" aria-label="Search docs"> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </label> <input type="search" name="pattern" id="navbar__search-input" class="navbar__search-input" placeholder="Search docs" accesskey="s" > <input type="hidden" name="scope" value="quickref"> </form> <!-- Desktop encanced search --> <button id="navbar__search-button" class="navbar__search-button" hidden > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> Search docs </button> <!-- Mobile default items --> <a id="navbar__search-link" href="/lookup-form.php" aria-label="Search docs" class="navbar__icon-item navbar__search-link" > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </a> <a id="navbar__menu-link" href="/menu.php" aria-label="Menu" class="navbar__icon-item navbar_icon-item--visually-aligned navbar_menu-link" > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="currentColor" > <path d="M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z" /> </svg> </a> <!-- Mobile enhanced items --> <button id="navbar__search-button-mobile" aria-label="Search docs" class="navbar__icon-item navbar__search-button-mobile" hidden > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </button> <button id="navbar__menu-button" aria-label="Menu" class="navbar__icon-item navbar_icon-item--visually-aligned" hidden > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="currentColor" > <path d="M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z" /> </svg> </button> </div> <div id="navbar__backdrop" class="navbar__backdrop" ></div> </div> <div id="flash-message"></div> </nav> <nav id="trick"><div><dl> <dt><a href='/manual/en/getting-started.php'>Getting Started</a></dt> <dd><a href='/manual/en/introduction.php'>Introduction</a></dd> <dd><a href='/manual/en/tutorial.php'>A simple tutorial</a></dd> <dt><a href='/manual/en/langref.php'>Language Reference</a></dt> <dd><a href='/manual/en/language.basic-syntax.php'>Basic syntax</a></dd> <dd><a href='/manual/en/language.types.php'>Types</a></dd> <dd><a href='/manual/en/language.variables.php'>Variables</a></dd> <dd><a href='/manual/en/language.constants.php'>Constants</a></dd> <dd><a href='/manual/en/language.expressions.php'>Expressions</a></dd> <dd><a href='/manual/en/language.operators.php'>Operators</a></dd> <dd><a href='/manual/en/language.control-structures.php'>Control Structures</a></dd> <dd><a href='/manual/en/language.functions.php'>Functions</a></dd> <dd><a href='/manual/en/language.oop5.php'>Classes and Objects</a></dd> <dd><a href='/manual/en/language.namespaces.php'>Namespaces</a></dd> <dd><a href='/manual/en/language.enumerations.php'>Enumerations</a></dd> <dd><a href='/manual/en/language.errors.php'>Errors</a></dd> <dd><a href='/manual/en/language.exceptions.php'>Exceptions</a></dd> <dd><a href='/manual/en/language.fibers.php'>Fibers</a></dd> <dd><a href='/manual/en/language.generators.php'>Generators</a></dd> <dd><a href='/manual/en/language.attributes.php'>Attributes</a></dd> <dd><a href='/manual/en/language.references.php'>References Explained</a></dd> <dd><a href='/manual/en/reserved.variables.php'>Predefined Variables</a></dd> <dd><a href='/manual/en/reserved.exceptions.php'>Predefined Exceptions</a></dd> <dd><a href='/manual/en/reserved.interfaces.php'>Predefined Interfaces and Classes</a></dd> <dd><a href='/manual/en/reserved.attributes.php'>Predefined Attributes</a></dd> <dd><a href='/manual/en/context.php'>Context options and parameters</a></dd> <dd><a href='/manual/en/wrappers.php'>Supported Protocols and Wrappers</a></dd> </dl> <dl> <dt><a href='/manual/en/security.php'>Security</a></dt> <dd><a href='/manual/en/security.intro.php'>Introduction</a></dd> <dd><a href='/manual/en/security.general.php'>General considerations</a></dd> <dd><a href='/manual/en/security.cgi-bin.php'>Installed as CGI binary</a></dd> <dd><a href='/manual/en/security.apache.php'>Installed as an Apache module</a></dd> <dd><a href='/manual/en/security.sessions.php'>Session Security</a></dd> <dd><a href='/manual/en/security.filesystem.php'>Filesystem Security</a></dd> <dd><a href='/manual/en/security.database.php'>Database Security</a></dd> <dd><a href='/manual/en/security.errors.php'>Error Reporting</a></dd> <dd><a href='/manual/en/security.variables.php'>User Submitted Data</a></dd> <dd><a href='/manual/en/security.hiding.php'>Hiding PHP</a></dd> <dd><a href='/manual/en/security.current.php'>Keeping Current</a></dd> <dt><a href='/manual/en/features.php'>Features</a></dt> <dd><a href='/manual/en/features.http-auth.php'>HTTP authentication with PHP</a></dd> <dd><a href='/manual/en/features.cookies.php'>Cookies</a></dd> <dd><a href='/manual/en/features.sessions.php'>Sessions</a></dd> <dd><a href='/manual/en/features.file-upload.php'>Handling file uploads</a></dd> <dd><a href='/manual/en/features.remote-files.php'>Using remote files</a></dd> <dd><a href='/manual/en/features.connection-handling.php'>Connection handling</a></dd> <dd><a href='/manual/en/features.persistent-connections.php'>Persistent Database Connections</a></dd> <dd><a href='/manual/en/features.commandline.php'>Command line usage</a></dd> <dd><a href='/manual/en/features.gc.php'>Garbage Collection</a></dd> <dd><a href='/manual/en/features.dtrace.php'>DTrace Dynamic Tracing</a></dd> </dl> <dl> <dt><a href='/manual/en/funcref.php'>Function Reference</a></dt> <dd><a href='/manual/en/refs.basic.php.php'>Affecting PHP's Behaviour</a></dd> <dd><a href='/manual/en/refs.utilspec.audio.php'>Audio Formats Manipulation</a></dd> <dd><a href='/manual/en/refs.remote.auth.php'>Authentication Services</a></dd> <dd><a href='/manual/en/refs.utilspec.cmdline.php'>Command Line Specific Extensions</a></dd> <dd><a href='/manual/en/refs.compression.php'>Compression and Archive Extensions</a></dd> <dd><a href='/manual/en/refs.crypto.php'>Cryptography Extensions</a></dd> <dd><a href='/manual/en/refs.database.php'>Database Extensions</a></dd> <dd><a href='/manual/en/refs.calendar.php'>Date and Time Related Extensions</a></dd> <dd><a href='/manual/en/refs.fileprocess.file.php'>File System Related Extensions</a></dd> <dd><a href='/manual/en/refs.international.php'>Human Language and Character Encoding Support</a></dd> <dd><a href='/manual/en/refs.utilspec.image.php'>Image Processing and Generation</a></dd> <dd><a href='/manual/en/refs.remote.mail.php'>Mail Related Extensions</a></dd> <dd><a href='/manual/en/refs.math.php'>Mathematical Extensions</a></dd> <dd><a href='/manual/en/refs.utilspec.nontext.php'>Non-Text MIME Output</a></dd> <dd><a href='/manual/en/refs.fileprocess.process.php'>Process Control Extensions</a></dd> <dd><a href='/manual/en/refs.basic.other.php'>Other Basic Extensions</a></dd> <dd><a href='/manual/en/refs.remote.other.php'>Other Services</a></dd> <dd><a href='/manual/en/refs.search.php'>Search Engine Extensions</a></dd> <dd><a href='/manual/en/refs.utilspec.server.php'>Server Specific Extensions</a></dd> <dd><a href='/manual/en/refs.basic.session.php'>Session Extensions</a></dd> <dd><a href='/manual/en/refs.basic.text.php'>Text Processing</a></dd> <dd><a href='/manual/en/refs.basic.vartype.php'>Variable and Type Related Extensions</a></dd> <dd><a href='/manual/en/refs.webservice.php'>Web Services</a></dd> <dd><a href='/manual/en/refs.utilspec.windows.php'>Windows Only Extensions</a></dd> <dd><a href='/manual/en/refs.xml.php'>XML Manipulation</a></dd> <dd><a href='/manual/en/refs.ui.php'>GUI Extensions</a></dd> </dl> <dl> <dt>Keyboard Shortcuts</dt><dt>?</dt> <dd>This help</dd> <dt>j</dt> <dd>Next menu item</dd> <dt>k</dt> <dd>Previous menu item</dd> <dt>g p</dt> <dd>Previous man page</dd> <dt>g n</dt> <dd>Next man page</dd> <dt>G</dt> <dd>Scroll to bottom</dd> <dt>g g</dt> <dd>Scroll to top</dd> <dt>g h</dt> <dd>Goto homepage</dd> <dt>g s</dt> <dd>Goto search<br>(current page)</dd> <dt>/</dt> <dd>Focus search box</dd> </dl></div></nav> <div id="goto"> <div class="search"> <div class="text"></div> <div class="results"><ul></ul></div> </div> </div> <div id="breadcrumbs" class="clearfix"> <div id="breadcrumbs-inner"> <div class="next"> <a href="language.types.object.php"> Objects &raquo; </a> </div> <div class="prev"> <a href="language.types.numeric-strings.php"> &laquo; Numeric strings </a> </div> <ul> <li><a href='index.php'>PHP Manual</a></li> <li><a href='langref.php'>Language Reference</a></li> <li><a href='language.types.php'>Types</a></li> </ul> </div> </div> <div id="layout" class="clearfix"> <section id="layout-content"> <div class="page-tools"> <div class="change-language"> <form action="/manual/change.php" method="get" id="changelang" name="changelang"> <fieldset> <label for="changelang-langs">Change language:</label> <select onchange="document.changelang.submit()" name="page" id="changelang-langs"> <option value='en/language.types.array.php' selected="selected">English</option> <option value='de/language.types.array.php'>German</option> <option value='es/language.types.array.php'>Spanish</option> <option value='fr/language.types.array.php'>French</option> <option value='it/language.types.array.php'>Italian</option> <option value='ja/language.types.array.php'>Japanese</option> <option value='pt_BR/language.types.array.php'>Brazilian Portuguese</option> <option value='ru/language.types.array.php'>Russian</option> <option value='tr/language.types.array.php'>Turkish</option> <option value='uk/language.types.array.php'>Ukrainian</option> <option value='zh/language.types.array.php'>Chinese (Simplified)</option> <option value='help-translate.php'>Other</option> </select> </fieldset> </form> </div> </div><div id="language.types.array" class="sect1"> <h2 class="title">Arrays</h2> <p class="para"> An <span class="type"><a href="language.types.array.php" class="type array">array</a></span> in PHP is actually an ordered map. A map is a type that associates <em>values</em> to <em>keys</em>. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As <span class="type"><a href="language.types.array.php" class="type array">array</a></span> values can be other <span class="type"><a href="language.types.array.php" class="type array">array</a></span>s, trees and multidimensional <span class="type"><a href="language.types.array.php" class="type array">array</a></span>s are also possible. </p> <p class="para"> Explanation of those data structures is beyond the scope of this manual, but at least one example is provided for each of them. For more information, look towards the considerable literature that exists about this broad topic. </p> <div class="sect2" id="language.types.array.syntax"> <h3 class="title">Syntax</h3> <div class="sect3" id="language.types.array.syntax.array-func"> <h4 class="title">Specifying with <span class="function"><a href="function.array.php" class="function">array()</a></span></h4> <p class="para"> An <span class="type"><a href="language.types.array.php" class="type array">array</a></span> can be created using the <span class="function"><a href="function.array.php" class="function">array()</a></span> language construct. It takes any number of comma-separated <code class="literal"><span class="replaceable">key</span> =&gt; <span class="replaceable">value</span></code> pairs as arguments. </p> <pre class="synopsis"> array( <span class="optional"><span class="replaceable">key</span> =&gt; </span><span class="replaceable">value</span>, <span class="optional"><span class="replaceable">key2</span> =&gt; </span><span class="replaceable">value2</span>, <span class="optional"><span class="replaceable">key3</span> =&gt; </span><span class="replaceable">value3</span>, ... )</pre> <p class="para"> The comma after the last array element is optional and can be omitted. This is usually done for single-line arrays, i.e. <code class="literal">array(1, 2)</code> is preferred over <code class="literal">array(1, 2, )</code>. For multi-line arrays on the other hand the trailing comma is commonly used, as it allows easier addition of new elements at the end. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> A short array syntax exists which replaces <code class="literal">array()</code> with <code class="literal">[]</code>. </p> </p></blockquote> <div class="example" id="example-58"> <p><strong>Example #1 A simple array</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">"foo" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"bar" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"foo"</span><span style="color: #007700">,<br />);<br /><br /></span><span style="color: #FF8000">// Using the short array syntax<br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= [<br /> </span><span style="color: #DD0000">"foo" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"bar" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"foo"</span><span style="color: #007700">,<br />];<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <p class="para"> The <span class="replaceable">key</span> can either be an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> or a <span class="type"><a href="language.types.string.php" class="type string">string</a></span>. The <span class="replaceable">value</span> can be of any type. </p> <p class="para" id="language.types.array.key-casts"> Additionally the following <span class="replaceable">key</span> casts will occur: <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.types.string.php" class="type String">String</a></span>s containing valid decimal <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>s, unless the number is preceded by a <code class="literal">+</code> sign, will be cast to the <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> type. E.g. the key <code class="literal">&quot;8&quot;</code> will actually be stored under <code class="literal">8</code>. On the other hand <code class="literal">&quot;08&quot;</code> will not be cast, as it isn&#039;t a valid decimal integer. </span> </li> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.types.float.php" class="type Float">Float</a></span>s are also cast to <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>s, which means that the fractional part will be truncated. E.g. the key <code class="literal">8.7</code> will actually be stored under <code class="literal">8</code>. </span> </li> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.types.boolean.php" class="type Bool">Bool</a></span>s are cast to <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>s, too, i.e. the key <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> will actually be stored under <code class="literal">1</code> and the key <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> under <code class="literal">0</code>. </span> </li> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.types.null.php" class="type Null">Null</a></span> will be cast to the empty string, i.e. the key <code class="literal">null</code> will actually be stored under <code class="literal">&quot;&quot;</code>. </span> </li> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>s and <span class="type"><a href="language.types.object.php" class="type object">object</a></span>s <em>can not</em> be used as keys. Doing so will result in a warning: <code class="literal">Illegal offset type</code>. </span> </li> </ul> </p> <p class="para"> If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten. </p> <div class="example" id="example-59"> <p><strong>Example #2 Type Casting and Overwriting example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= array(<br /> </span><span style="color: #0000BB">1 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"a"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"1" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"b"</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">1.5 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"c"</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">true </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"d"</span><span style="color: #007700">,<br />);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(1) { [1]=&gt; string(1) &quot;d&quot; } </pre></div> </div> <div class="example-contents"><p> As all the keys in the above example are cast to <code class="literal">1</code>, the value will be overwritten on every new element and the last assigned value <code class="literal">&quot;d&quot;</code> is the only one left over. </p></div> </div> <p class="para"> PHP arrays can contain <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> and <span class="type"><a href="language.types.string.php" class="type string">string</a></span> keys at the same time as PHP does not distinguish between indexed and associative arrays. </p> <div class="example" id="example-60"> <p><strong>Example #3 Mixed <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> and <span class="type"><a href="language.types.string.php" class="type string">string</a></span> keys</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">"foo" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"bar" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"foo"</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">100 </span><span style="color: #007700">=&gt; -</span><span style="color: #0000BB">100</span><span style="color: #007700">,<br /> -</span><span style="color: #0000BB">100 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">100</span><span style="color: #007700">,<br />);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(4) { [&quot;foo&quot;]=&gt; string(3) &quot;bar&quot; [&quot;bar&quot;]=&gt; string(3) &quot;foo&quot; [100]=&gt; int(-100) [-100]=&gt; int(100) } </pre></div> </div> </div> <p class="para"> The <span class="replaceable">key</span> is optional. If it is not specified, PHP will use the increment of the largest previously used <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> key. </p> <div class="example" id="example-61"> <p><strong>Example #4 Indexed arrays without key</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">, </span><span style="color: #DD0000">"bar"</span><span style="color: #007700">, </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">, </span><span style="color: #DD0000">"world"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(4) { [0]=&gt; string(3) &quot;foo&quot; [1]=&gt; string(3) &quot;bar&quot; [2]=&gt; string(5) &quot;hello&quot; [3]=&gt; string(5) &quot;world&quot; } </pre></div> </div> </div> <p class="para"> It is possible to specify the key only for some elements and leave it out for others: </p> <div class="example" id="example-62"> <p><strong>Example #5 Keys not on all elements</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">"a"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"b"</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">6 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"c"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"d"</span><span style="color: #007700">,<br />);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(4) { [0]=&gt; string(1) &quot;a&quot; [1]=&gt; string(1) &quot;b&quot; [6]=&gt; string(1) &quot;c&quot; [7]=&gt; string(1) &quot;d&quot; } </pre></div> </div> <div class="example-contents"><p> As you can see the last value <code class="literal">&quot;d&quot;</code> was assigned the key <code class="literal">7</code>. This is because the largest integer key before that was <code class="literal">6</code>. </p></div> </div> <div class="example" id="example-63"> <p><strong>Example #6 Complex Type Casting and Overwriting example</strong></p> <div class="example-contents"><p> This example includes all variations of type casting of keys and overwriting of elements. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= array(<br /> </span><span style="color: #0000BB">1 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'a'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'1' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'b'</span><span style="color: #007700">, </span><span style="color: #FF8000">// the value "a" will be overwritten by "b"<br /> </span><span style="color: #0000BB">1.5 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'c'</span><span style="color: #007700">, </span><span style="color: #FF8000">// the value "b" will be overwritten by "c"<br /> </span><span style="color: #007700">-</span><span style="color: #0000BB">1 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'d'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'01' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'e'</span><span style="color: #007700">, </span><span style="color: #FF8000">// as this is not an integer string it will NOT override the key for 1<br /> </span><span style="color: #DD0000">'1.5' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'f'</span><span style="color: #007700">, </span><span style="color: #FF8000">// as this is not an integer string it will NOT override the key for 1<br /> </span><span style="color: #0000BB">true </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'g'</span><span style="color: #007700">, </span><span style="color: #FF8000">// the value "c" will be overwritten by "g"<br /> </span><span style="color: #0000BB">false </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'h'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'i'</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">null </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'j'</span><span style="color: #007700">, </span><span style="color: #FF8000">// the value "i" will be overwritten by "j"<br /> </span><span style="color: #DD0000">'k'</span><span style="color: #007700">, </span><span style="color: #FF8000">// value "k" is assigned the key 2. This is because the largest integer key before that was 1<br /> </span><span style="color: #0000BB">2 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'l'</span><span style="color: #007700">, </span><span style="color: #FF8000">// the value "k" will be overwritten by "l"<br /></span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(7) { [1]=&gt; string(1) &quot;g&quot; [-1]=&gt; string(1) &quot;d&quot; [&quot;01&quot;]=&gt; string(1) &quot;e&quot; [&quot;1.5&quot;]=&gt; string(1) &quot;f&quot; [0]=&gt; string(1) &quot;h&quot; [&quot;&quot;]=&gt; string(1) &quot;j&quot; [2]=&gt; string(1) &quot;l&quot; } </pre></div> </div> </div> <div class="example" id="example-64"> <p><strong>Example #7 Negative index example</strong></p> <div class="example-contents"><p> When assigning a negative integer key <code class="literal">n</code>, PHP will take care to assign the next key to <code class="literal">n+1</code>. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= [];<br /><br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[-</span><span style="color: #0000BB">5</span><span style="color: #007700">] = </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[] = </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(2) { [-5]=&gt; int(1) [-4]=&gt; int(2) } </pre></div> </div> <div class="warning"><strong class="warning">Warning</strong> <p class="simpara"> Prior to PHP 8.3.0, assigning a negative integer key <code class="literal">n</code> would assign the next key to <code class="literal">0</code>, the previous example would therefore output: </p> <div class="informalexample"> <div class="example-contents screen"> <div class="cdata"><pre> array(2) { [-5]=&gt; int(1) [0]=&gt; int(2) } </pre></div> </div> </div> </div> </div> </div> <div class="sect3" id="language.types.array.syntax.accessing"> <h4 class="title">Accessing array elements with square bracket syntax</h4> <p class="para"> Array elements can be accessed using the <code class="literal">array[key]</code> syntax. </p> <div class="example" id="example-65"> <p><strong>Example #8 Accessing array elements</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$array </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">"foo" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">42 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">24</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"multi" </span><span style="color: #007700">=&gt; array(<br /> </span><span style="color: #DD0000">"dimensional" </span><span style="color: #007700">=&gt; array(<br /> </span><span style="color: #DD0000">"array" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"foo"<br /> </span><span style="color: #007700">)<br /> )<br />);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">42</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">"multi"</span><span style="color: #007700">][</span><span style="color: #DD0000">"dimensional"</span><span style="color: #007700">][</span><span style="color: #DD0000">"array"</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> string(3) &quot;bar&quot; int(24) string(3) &quot;foo&quot; </pre></div> </div> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Prior to PHP 8.0.0, square brackets and curly braces could be used interchangeably for accessing array elements (e.g. <code class="literal">$array[42]</code> and <code class="literal">$array{42}</code> would both do the same thing in the example above). The curly brace syntax was deprecated as of PHP 7.4.0 and no longer supported as of PHP 8.0.0. </p> </p></blockquote> <div class="example" id="example-66"> <p><strong>Example #9 Array dereferencing</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">getArray</span><span style="color: #007700">() {<br /> return array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$secondElement </span><span style="color: #007700">= </span><span style="color: #0000BB">getArray</span><span style="color: #007700">()[</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Attempting to access an array key which has not been defined is the same as accessing any other undefined variable: an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>-level error message (<strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong>-level prior to PHP 8.0.0) will be issued, and the result will be <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Array dereferencing a scalar value which is not a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> yields <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>. Prior to PHP 7.4.0, that did not issue an error message. As of PHP 7.4.0, this issues <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong>; as of PHP 8.0.0, this issues <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>. </p> </p></blockquote> </div> <div class="sect3" id="language.types.array.syntax.modifying"> <h4 class="title">Creating/modifying with square bracket syntax</h4> <p class="para"> An existing <span class="type"><a href="language.types.array.php" class="type array">array</a></span> can be modified by explicitly setting values in it. </p> <p class="para"> This is done by assigning values to the <span class="type"><a href="language.types.array.php" class="type array">array</a></span>, specifying the key in brackets. The key can also be omitted, resulting in an empty pair of brackets (<code class="literal">[]</code>). </p> <pre class="synopsis"> $arr[<span class="replaceable">key</span>] = <span class="replaceable">value</span>; $arr[] = <span class="replaceable">value</span>; // <span class="replaceable">key</span> may be an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> or <span class="type"><a href="language.types.string.php" class="type string">string</a></span> // <span class="replaceable">value</span> may be any value of any type</pre> <p class="para"> If <var class="varname">$arr</var> doesn&#039;t exist yet or is set to <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, it will be created, so this is also an alternative way to create an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>. This practice is however discouraged because if <var class="varname">$arr</var> already contains some value (e.g. <span class="type"><a href="language.types.string.php" class="type string">string</a></span> from request variable) then this value will stay in the place and <code class="literal">[]</code> may actually stand for <a href="language.types.string.php#language.types.string.substr" class="link">string access operator</a>. It is always better to initialize a variable by a direct assignment. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> As of PHP 7.1.0, applying the empty index operator on a string throws a fatal error. Formerly, the string was silently converted to an array. </span> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> As of PHP 8.1.0, creating a new array from <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> value is deprecated. Creating a new array from <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> and undefined values is still allowed. </span> </p></blockquote> <p class="para"> To change a certain value, assign a new value to that element using its key. To remove a key/value pair, call the <span class="function"><a href="function.unset.php" class="function">unset()</a></span> function on it. </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$arr </span><span style="color: #007700">= array(</span><span style="color: #0000BB">5 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">12 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$arr</span><span style="color: #007700">[] = </span><span style="color: #0000BB">56</span><span style="color: #007700">; </span><span style="color: #FF8000">// This is the same as $arr[13] = 56;<br /> // at this point of the script<br /><br /></span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">"x"</span><span style="color: #007700">] = </span><span style="color: #0000BB">42</span><span style="color: #007700">; </span><span style="color: #FF8000">// This adds a new element to<br /> // the array with key "x"<br /><br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">5</span><span style="color: #007700">]); </span><span style="color: #FF8000">// This removes the element from the array<br /><br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">); </span><span style="color: #FF8000">// This deletes the whole array<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> As mentioned above, if no key is specified, the maximum of the existing <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> indices is taken, and the new key will be that maximum value plus 1 (but at least 0). If no <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> indices exist yet, the key will be <code class="literal">0</code> (zero). </p> <p class="para"> Note that the maximum integer key used for this <em>need not currently exist in the <span class="type"><a href="language.types.array.php" class="type array">array</a></span></em>. It need only have existed in the <span class="type"><a href="language.types.array.php" class="type array">array</a></span> at some time since the last time the <span class="type"><a href="language.types.array.php" class="type array">array</a></span> was re-indexed. The following example illustrates: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Create a simple array.<br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Now delete every item, but leave the array itself intact:<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$array </span><span style="color: #007700">as </span><span style="color: #0000BB">$i </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> unset(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">]);<br />}<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Append an item (note that the new key is 5, instead of 0).<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[] = </span><span style="color: #0000BB">6</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Re-index:<br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= </span><span style="color: #0000BB">array_values</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[] = </span><span style="color: #0000BB">7</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> <p class="para">The above example will output:</p> <div class="example-contents screen"> <div class="cdata"><pre> Array ( [0] =&gt; 1 [1] =&gt; 2 [2] =&gt; 3 [3] =&gt; 4 [4] =&gt; 5 ) Array ( ) Array ( [5] =&gt; 6 ) Array ( [0] =&gt; 6 [1] =&gt; 7 ) </pre></div> </div> </div> </p></blockquote> </div> <div class="sect3" id="language.types.array.syntax.destructuring"> <h4 class="title">Array destructuring</h4> <p class="para"> Arrays can be destructured using the <code class="literal">[]</code> (as of PHP 7.1.0) or <span class="function"><a href="function.list.php" class="function">list()</a></span> language constructs. These constructs can be used to destructure an array into distinct variables. </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$source_array </span><span style="color: #007700">= [</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">, </span><span style="color: #DD0000">'baz'</span><span style="color: #007700">];<br /><br />[</span><span style="color: #0000BB">$foo</span><span style="color: #007700">, </span><span style="color: #0000BB">$bar</span><span style="color: #007700">, </span><span style="color: #0000BB">$baz</span><span style="color: #007700">] = </span><span style="color: #0000BB">$source_array</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$foo</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints "foo"<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$bar</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints "bar"<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$baz</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints "baz"<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <p class="para"> Array destructuring can be used in <a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a> to destructure a multi-dimensional array while iterating over it. </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$source_array </span><span style="color: #007700">= [<br /> [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">'John'</span><span style="color: #007700">],<br /> [</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">'Jane'</span><span style="color: #007700">],<br />];<br /><br />foreach (</span><span style="color: #0000BB">$source_array </span><span style="color: #007700">as [</span><span style="color: #0000BB">$id</span><span style="color: #007700">, </span><span style="color: #0000BB">$name</span><span style="color: #007700">]) {<br /> </span><span style="color: #FF8000">// logic here with $id and $name<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <p class="para"> Array elements will be ignored if the variable is not provided. Array destructuring always starts at index <code class="literal">0</code>. </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$source_array </span><span style="color: #007700">= [</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">, </span><span style="color: #DD0000">'baz'</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">// Assign the element at index 2 to the variable $baz<br /></span><span style="color: #007700">[, , </span><span style="color: #0000BB">$baz</span><span style="color: #007700">] = </span><span style="color: #0000BB">$source_array</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$baz</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints "baz"<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <p class="para"> As of PHP 7.1.0, associative arrays can be destructured too. This also allows for easier selection of the right element in numerically indexed arrays as the index can be explicitly specified. </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$source_array </span><span style="color: #007700">= [</span><span style="color: #DD0000">'foo' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">'bar' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">'baz' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">// Assign the element at index 'baz' to the variable $three<br /></span><span style="color: #007700">[</span><span style="color: #DD0000">'baz' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$three</span><span style="color: #007700">] = </span><span style="color: #0000BB">$source_array</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$three</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints 3<br /><br /></span><span style="color: #0000BB">$source_array </span><span style="color: #007700">= [</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">, </span><span style="color: #DD0000">'baz'</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">// Assign the element at index 2 to the variable $baz<br /></span><span style="color: #007700">[</span><span style="color: #0000BB">2 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$baz</span><span style="color: #007700">] = </span><span style="color: #0000BB">$source_array</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$baz</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints "baz"<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <p class="para"> Array destructuring can be used for easy swapping of two variables. </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br />[</span><span style="color: #0000BB">$b</span><span style="color: #007700">, </span><span style="color: #0000BB">$a</span><span style="color: #007700">] = [</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">];<br /><br />echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints 2<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$b</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints 1<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The spread operator (<code class="literal">...</code>) is not supported in assignments. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Attempting to access an array key which has not been defined is the same as accessing any other undefined variable: an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>-level error message (<strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong>-level prior to PHP 8.0.0) will be issued, and the result will be <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>. </p> </p></blockquote> </div> </div> <div class="sect2" id="language.types.array.useful-funcs"> <h3 class="title">Useful functions</h3> <p class="para"> There are quite a few useful functions for working with arrays. See the <a href="ref.array.php" class="link">array functions</a> section. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The <span class="function"><a href="function.unset.php" class="function">unset()</a></span> function allows removing keys from an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>. Be aware that the array will <em>not</em> be reindexed. If a true &quot;remove and shift&quot; behavior is desired, the <span class="type"><a href="language.types.array.php" class="type array">array</a></span> can be reindexed using the <span class="function"><a href="function.array-values.php" class="function">array_values()</a></span> function. </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$a </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'one'</span><span style="color: #007700">, </span><span style="color: #0000BB">2 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'two'</span><span style="color: #007700">, </span><span style="color: #0000BB">3 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'three'</span><span style="color: #007700">);<br />unset(</span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]);<br /></span><span style="color: #FF8000">/* will produce an array that would have been defined as<br /> $a = array(1 =&gt; 'one', 3 =&gt; 'three');<br /> and NOT<br /> $a = array(1 =&gt; 'one', 2 =&gt;'three');<br />*/<br /><br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">array_values</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// Now $b is array(0 =&gt; 'one', 1 =&gt;'three')<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> </p></blockquote> <p class="para"> The <a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a> control structure exists specifically for <span class="type"><a href="language.types.array.php" class="type array">array</a></span>s. It provides an easy way to traverse an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>. </p> </div> <div class="sect2" id="language.types.array.donts"> <h3 class="title">Array do&#039;s and don&#039;ts</h3> <div class="sect3" id="language.types.array.foo-bar"> <h4 class="title">Why is <code class="literal">$foo[bar]</code> wrong?</h4> <p class="para"> Always use quotes around a string literal array index. For example, <code class="literal">$foo[&#039;bar&#039;]</code> is correct, while <code class="literal">$foo[bar]</code> is not. But why? It is common to encounter this kind of syntax in old scripts: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$foo</span><span style="color: #007700">[</span><span style="color: #0000BB">bar</span><span style="color: #007700">] = </span><span style="color: #DD0000">'enemy'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$foo</span><span style="color: #007700">[</span><span style="color: #0000BB">bar</span><span style="color: #007700">];<br /></span><span style="color: #FF8000">// etc<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <p class="para"> This is wrong, but it works. The reason is that this code has an undefined constant (<code class="literal">bar</code>) rather than a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> (<code class="literal">&#039;bar&#039;</code> - notice the quotes). It works because PHP automatically converts a <em>bare string</em> (an unquoted <span class="type"><a href="language.types.string.php" class="type string">string</a></span> which does not correspond to any known symbol) into a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> which contains the bare <span class="type"><a href="language.types.string.php" class="type string">string</a></span>. For instance, if there is no defined constant named <strong><code>bar</code></strong>, then PHP will substitute in the <span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="literal">&#039;bar&#039;</code> and use that. </p> <div class="warning"><strong class="warning">Warning</strong> <p class="simpara"> The fallback to treat an undefined constant as bare string issues an error of level <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong>. This has been deprecated as of PHP 7.2.0, and issues an error of level <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>. As of PHP 8.0.0, it has been removed and throws an <span class="classname"><a href="class.error.php" class="classname">Error</a></span> exception. </p> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> This does not mean to <em>always</em> quote the key. Do not quote keys which are <a href="language.constants.php" class="link">constants</a> or <a href="language.variables.php" class="link">variables</a>, as this will prevent PHP from interpreting them. </span> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'display_errors'</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'html_errors'</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// Simple array:<br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$count </span><span style="color: #007700">= </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br />for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">&lt; </span><span style="color: #0000BB">$count</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br /> echo </span><span style="color: #DD0000">"\nChecking </span><span style="color: #0000BB">$i</span><span style="color: #DD0000">: \n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"Bad: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">'$i'</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"Good: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"Bad: </span><span style="color: #007700">{</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">'$i'</span><span style="color: #007700">]}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"Good: </span><span style="color: #007700">{</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">]}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <p class="para">The above example will output:</p> <div class="example-contents screen"> <div class="cdata"><pre> Checking 0: Notice: Undefined index: $i in /path/to/script.html on line 9 Bad: Good: 1 Notice: Undefined index: $i in /path/to/script.html on line 11 Bad: Good: 1 Checking 1: Notice: Undefined index: $i in /path/to/script.html on line 9 Bad: Good: 2 Notice: Undefined index: $i in /path/to/script.html on line 11 Bad: Good: 2 </pre></div> </div> </p></blockquote> <p class="para"> More examples to demonstrate this behaviour: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Show all errors<br /></span><span style="color: #0000BB">error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$arr </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'fruit' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'apple'</span><span style="color: #007700">, </span><span style="color: #DD0000">'veggie' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'carrot'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Correct<br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">]; </span><span style="color: #FF8000">// apple<br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'veggie'</span><span style="color: #007700">]; </span><span style="color: #FF8000">// carrot<br /><br />// Incorrect. This works but also throws a PHP error of level E_NOTICE because<br />// of an undefined constant named fruit<br />//<br />// Notice: Use of undefined constant fruit - assumed 'fruit' in...<br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">]; </span><span style="color: #FF8000">// apple<br /><br />// This defines a constant to demonstrate what's going on. The value 'veggie'<br />// is assigned to a constant named fruit.<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">, </span><span style="color: #DD0000">'veggie'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Notice the difference now<br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">]; </span><span style="color: #FF8000">// apple<br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">]; </span><span style="color: #FF8000">// carrot<br /><br />// The following is okay, as it's inside a string. Constants are not looked for<br />// within strings, so no E_NOTICE occurs here<br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"Hello </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">]</span><span style="color: #DD0000">"</span><span style="color: #007700">; </span><span style="color: #FF8000">// Hello apple<br /><br />// With one exception: braces surrounding arrays within strings allows constants<br />// to be interpreted<br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"Hello </span><span style="color: #007700">{</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">]}</span><span style="color: #DD0000">"</span><span style="color: #007700">; </span><span style="color: #FF8000">// Hello carrot<br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"Hello </span><span style="color: #007700">{</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">]}</span><span style="color: #DD0000">"</span><span style="color: #007700">; </span><span style="color: #FF8000">// Hello apple<br /><br />// Concatenation is another option<br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"Hello " </span><span style="color: #007700">. </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">]; </span><span style="color: #FF8000">// Hello apple<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000">// This will not work, and will result in a parse error, such as:<br />// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'<br />// This of course applies to using superglobals in strings as well<br />print "Hello $arr['fruit']";<br />print "Hello $_GET['foo']";</span></code></div> </div> </div> <p class="para"> When <a href="errorfunc.configuration.php#ini.error-reporting" class="link">error_reporting</a> is set to show <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> level errors (by setting it to <strong><code><a href="errorfunc.constants.php#constant.e-all">E_ALL</a></code></strong>, for example), such uses will become immediately visible. By default, <a href="errorfunc.configuration.php#ini.error-reporting" class="link">error_reporting</a> is set not to show notices. </p> <p class="para"> As stated in the <a href="language.types.array.php#language.types.array.syntax" class="link">syntax</a> section, what&#039;s inside the square brackets (&#039;<code class="literal">[</code>&#039; and &#039;<code class="literal">]</code>&#039;) must be an expression. This means that code like this works: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">somefunc</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">)];<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <p class="para"> This is an example of using a function return value as the array index. PHP also knows about constants: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">E_ERROR</span><span style="color: #007700">] = </span><span style="color: #DD0000">"A fatal error has occurred"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">E_WARNING</span><span style="color: #007700">] = </span><span style="color: #DD0000">"PHP issued a warning"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">E_NOTICE</span><span style="color: #007700">] = </span><span style="color: #DD0000">"This is just an informal notice"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <p class="para"> Note that <strong><code><a href="errorfunc.constants.php#constant.e-error">E_ERROR</a></code></strong> is also a valid identifier, just like <code class="literal">bar</code> in the first example. But the last example is in fact the same as writing: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">] = </span><span style="color: #DD0000">"A fatal error has occurred"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">] = </span><span style="color: #DD0000">"PHP issued a warning"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">8</span><span style="color: #007700">] = </span><span style="color: #DD0000">"This is just an informal notice"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <p class="para"> because <strong><code><a href="errorfunc.constants.php#constant.e-error">E_ERROR</a></code></strong> equals <code class="literal">1</code>, etc. </p> <div class="sect4" id="language.types.array.foo-bar.why"> <h5 class="title">So why is it bad then?</h5> <p class="para"> At some point in the future, the PHP team might want to add another constant or keyword, or a constant in other code may interfere. For example, it is already wrong to use the words <code class="literal">empty</code> and <code class="literal">default</code> this way, since they are <a href="reserved.php" class="link">reserved keywords</a>. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> To reiterate, inside a double-quoted <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, it&#039;s valid to not surround array indexes with quotes so <code class="literal">&quot;$foo[bar]&quot;</code> is valid. See the above examples for details on why as well as the section on <a href="language.types.string.php#language.types.string.parsing" class="link">variable parsing in strings</a>. </span> </p></blockquote> </div> </div> </div> <div class="sect2" id="language.types.array.casting"> <h3 class="title">Converting to array</h3> <p class="para"> For any of the types <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>, <span class="type"><a href="language.types.float.php" class="type float">float</a></span>, <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> and <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>, converting a value to an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> results in an array with a single element with index zero and the value of the scalar which was converted. In other words, <code class="code">(array) $scalarValue</code> is exactly the same as <code class="literal">array($scalarValue)</code>. </p> <p class="para"> If an <span class="type"><a href="language.types.object.php" class="type object">object</a></span> is converted to an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>, the result is an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> whose elements are the <span class="type"><a href="language.types.object.php" class="type object">object</a></span>&#039;s properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a &#039;*&#039; prepended to the variable name. These prepended values have <code class="literal">NUL</code> bytes on either side. Uninitialized <a href="language.oop5.properties.php#language.oop5.properties.typed-properties" class="link">typed properties</a> are silently discarded. </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> private </span><span style="color: #0000BB">$B</span><span style="color: #007700">;<br /> protected </span><span style="color: #0000BB">$C</span><span style="color: #007700">;<br /> public </span><span style="color: #0000BB">$D</span><span style="color: #007700">;<br /> function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br /> {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;{</span><span style="color: #0000BB">1</span><span style="color: #007700">} = </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">var_export</span><span style="color: #007700">((array) new </span><span style="color: #0000BB">A</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> <p class="para">The above example will output:</p> <div class="example-contents screen"> <div class="cdata"><pre> array ( &#039;&#039; . &quot;\0&quot; . &#039;A&#039; . &quot;\0&quot; . &#039;B&#039; =&gt; NULL, &#039;&#039; . &quot;\0&quot; . &#039;*&#039; . &quot;\0&quot; . &#039;C&#039; =&gt; NULL, &#039;D&#039; =&gt; NULL, 1 =&gt; NULL, ) </pre></div> </div> </div> <p class="para"> These <code class="literal">NUL</code> can result in some unexpected behaviour: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> private </span><span style="color: #0000BB">$A</span><span style="color: #007700">; </span><span style="color: #FF8000">// This will become '\0A\0A'<br /></span><span style="color: #007700">}<br /><br />class </span><span style="color: #0000BB">B </span><span style="color: #007700">extends </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> private </span><span style="color: #0000BB">$A</span><span style="color: #007700">; </span><span style="color: #FF8000">// This will become '\0B\0A'<br /> </span><span style="color: #007700">public </span><span style="color: #0000BB">$AA</span><span style="color: #007700">; </span><span style="color: #FF8000">// This will become 'AA'<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((array) new </span><span style="color: #0000BB">B</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> <p class="para">The above example will output:</p> <div class="example-contents screen"> <div class="cdata"><pre> array(3) { [&quot;BA&quot;]=&gt; NULL [&quot;AA&quot;]=&gt; NULL [&quot;AA&quot;]=&gt; NULL } </pre></div> </div> </div> <p class="para"> The above will appear to have two keys named &#039;AA&#039;, although one of them is actually named &#039;\0A\0A&#039;. </p> <p class="para"> Converting <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> to an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> results in an empty <span class="type"><a href="language.types.array.php" class="type array">array</a></span>. </p> </div> <div class="sect2" id="language.types.array.comparing"> <h3 class="title">Comparing</h3> <p class="para"> It is possible to compare arrays with the <span class="function"><a href="function.array-diff.php" class="function">array_diff()</a></span> function and with <a href="language.operators.array.php" class="link">array operators</a>. </p> </div> <div class="sect2" id="language.types.array.unpacking"> <h3 class="title">Array unpacking</h3> <p class="para"> An array prefixed by <code class="code">...</code> will be expanded in place during array definition. Only arrays and objects which implement <span class="interfacename"><a href="class.traversable.php" class="interfacename">Traversable</a></span> can be expanded. Array unpacking with <code class="code">...</code> is available as of PHP 7.4.0. </p> <p class="para"> It&#039;s possible to expand multiple times, and add normal elements before or after the <code class="code">...</code> operator: <div class="example" id="example-67"> <p><strong>Example #10 Simple array unpacking</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Using short array syntax.<br />// Also, works with array() syntax.<br /></span><span style="color: #0000BB">$arr1 </span><span style="color: #007700">= [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr2 </span><span style="color: #007700">= [...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">]; </span><span style="color: #FF8000">//[1, 2, 3]<br /></span><span style="color: #0000BB">$arr3 </span><span style="color: #007700">= [</span><span style="color: #0000BB">0</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">]; </span><span style="color: #FF8000">//[0, 1, 2, 3]<br /></span><span style="color: #0000BB">$arr4 </span><span style="color: #007700">= [...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr2</span><span style="color: #007700">, </span><span style="color: #0000BB">111</span><span style="color: #007700">]; </span><span style="color: #FF8000">//[1, 2, 3, 1, 2, 3, 111]<br /></span><span style="color: #0000BB">$arr5 </span><span style="color: #007700">= [...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">]; </span><span style="color: #FF8000">//[1, 2, 3, 1, 2, 3]<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">getArr</span><span style="color: #007700">() {<br /> return [</span><span style="color: #DD0000">'a'</span><span style="color: #007700">, </span><span style="color: #DD0000">'b'</span><span style="color: #007700">];<br />}<br /></span><span style="color: #0000BB">$arr6 </span><span style="color: #007700">= [...</span><span style="color: #0000BB">getArr</span><span style="color: #007700">(), </span><span style="color: #DD0000">'c' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'d'</span><span style="color: #007700">]; </span><span style="color: #FF8000">//['a', 'b', 'c' =&gt; 'd']<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> </p> <p class="para"> Unpacking an array with the <code class="code">...</code> operator follows the semantics of the <span class="function"><a href="function.array-merge.php" class="function">array_merge()</a></span> function. That is, later string keys overwrite earlier ones and integer keys are renumbered: <div class="example" id="example-68"> <p><strong>Example #11 Array unpacking with duplicate key</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// string key<br /></span><span style="color: #0000BB">$arr1 </span><span style="color: #007700">= [</span><span style="color: #DD0000">"a" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr2 </span><span style="color: #007700">= [</span><span style="color: #DD0000">"a" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr3 </span><span style="color: #007700">= [</span><span style="color: #DD0000">"a" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">0</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr2</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr3</span><span style="color: #007700">); </span><span style="color: #FF8000">// ["a" =&gt; 2]<br /><br />// integer key<br /></span><span style="color: #0000BB">$arr4 </span><span style="color: #007700">= [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr5 </span><span style="color: #007700">= [</span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #0000BB">6</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr6 </span><span style="color: #007700">= [...</span><span style="color: #0000BB">$arr4</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr5</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr6</span><span style="color: #007700">); </span><span style="color: #FF8000">// [1, 2, 3, 4, 5, 6]<br />// Which is [0 =&gt; 1, 1 =&gt; 2, 2 =&gt; 3, 3 =&gt; 4, 4 =&gt; 5, 5 =&gt; 6]<br />// where the original integer keys have not been retained.<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Keys that are neither integers nor strings throw a <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span>. Such keys can only be generated by a <span class="interfacename"><a href="class.traversable.php" class="interfacename">Traversable</a></span> object. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Prior to PHP 8.1, unpacking an array which has a string key is not supported: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$arr1 </span><span style="color: #007700">= [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr2 </span><span style="color: #007700">= [</span><span style="color: #DD0000">'a' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">4</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr3 </span><span style="color: #007700">= [...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr2</span><span style="color: #007700">];<br /></span><span style="color: #FF8000">// Fatal error: Uncaught Error: Cannot unpack array with string keys in example.php:5<br /><br /></span><span style="color: #0000BB">$arr4 </span><span style="color: #007700">= [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr5 </span><span style="color: #007700">= [</span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr6 </span><span style="color: #007700">= [...</span><span style="color: #0000BB">$arr4</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$arr5</span><span style="color: #007700">]; </span><span style="color: #FF8000">// works. [1, 2, 3, 4, 5]<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> </p></blockquote> </div> <div class="sect2" id="language.types.array.examples"> <h3 class="title">Examples</h3> <p class="para"> The array type in PHP is very versatile. Here are some examples: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// This:<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= array( </span><span style="color: #DD0000">'color' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'red'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'taste' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'sweet'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'shape' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'round'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">4 </span><span style="color: #FF8000">// key will be 0<br /> </span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">, </span><span style="color: #DD0000">'b'</span><span style="color: #007700">, </span><span style="color: #DD0000">'c'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// . . .is completely equivalent with this:<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= array();<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'color'</span><span style="color: #007700">] = </span><span style="color: #DD0000">'red'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'taste'</span><span style="color: #007700">] = </span><span style="color: #DD0000">'sweet'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'shape'</span><span style="color: #007700">] = </span><span style="color: #DD0000">'round'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">] = </span><span style="color: #DD0000">'apple'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[] = </span><span style="color: #0000BB">4</span><span style="color: #007700">; </span><span style="color: #FF8000">// key will be 0<br /><br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= array();<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[] = </span><span style="color: #DD0000">'a'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[] = </span><span style="color: #DD0000">'b'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[] = </span><span style="color: #DD0000">'c'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// After the above code is executed, $a will be the array<br />// array('color' =&gt; 'red', 'taste' =&gt; 'sweet', 'shape' =&gt; 'round',<br />// 'name' =&gt; 'apple', 0 =&gt; 4), and $b will be the array<br />// array(0 =&gt; 'a', 1 =&gt; 'b', 2 =&gt; 'c'), or simply array('a', 'b', 'c').<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <div class="example" id="example-69"> <p><strong>Example #12 Using array()</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Array as (property-)map<br /></span><span style="color: #0000BB">$map </span><span style="color: #007700">= array( </span><span style="color: #DD0000">'version' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">4</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'OS' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Linux'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'lang' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'english'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'short_tags' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">true<br /> </span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// strictly numerical keys<br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= array( </span><span style="color: #0000BB">7</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">8</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">0</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">156</span><span style="color: #007700">,<br /> -</span><span style="color: #0000BB">10<br /> </span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// this is the same as array(0 =&gt; 7, 1 =&gt; 8, ...)<br /><br /></span><span style="color: #0000BB">$switching </span><span style="color: #007700">= array( </span><span style="color: #0000BB">10</span><span style="color: #007700">, </span><span style="color: #FF8000">// key = 0<br /> </span><span style="color: #0000BB">5 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">6</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">3 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">7</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'a' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">4</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">11</span><span style="color: #007700">, </span><span style="color: #FF8000">// key = 6 (maximum of integer-indices was 5)<br /> </span><span style="color: #DD0000">'8' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #FF8000">// key = 8 (integer!)<br /> </span><span style="color: #DD0000">'02' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">77</span><span style="color: #007700">, </span><span style="color: #FF8000">// key = '02'<br /> </span><span style="color: #0000BB">0 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">12 </span><span style="color: #FF8000">// the value 10 will be overwritten by 12<br /> </span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// empty array<br /></span><span style="color: #0000BB">$empty </span><span style="color: #007700">= array();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <div class="example" id="language.types.array.examples.loop"> <p><strong>Example #13 Collection</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$colors </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'red'</span><span style="color: #007700">, </span><span style="color: #DD0000">'blue'</span><span style="color: #007700">, </span><span style="color: #DD0000">'green'</span><span style="color: #007700">, </span><span style="color: #DD0000">'yellow'</span><span style="color: #007700">);<br /><br />foreach (</span><span style="color: #0000BB">$colors </span><span style="color: #007700">as </span><span style="color: #0000BB">$color</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Do you like </span><span style="color: #0000BB">$color</span><span style="color: #DD0000">?\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Do you like red? Do you like blue? Do you like green? Do you like yellow? </pre></div> </div> </div> <p class="para"> Changing the values of the <span class="type"><a href="language.types.array.php" class="type array">array</a></span> directly is possible by passing them by reference. </p> <div class="example" id="language.types.array.examples.changeloop"> <p><strong>Example #14 Changing element in the loop</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$colors </span><span style="color: #007700">as &amp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$color </span><span style="color: #007700">= </span><span style="color: #0000BB">mb_strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">$color</span><span style="color: #007700">);<br />}<br />unset(</span><span style="color: #0000BB">$color</span><span style="color: #007700">); </span><span style="color: #FF8000">/* ensure that following writes to<br />$color will not modify the last array element */<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$colors</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Array ( [0] =&gt; RED [1] =&gt; BLUE [2] =&gt; GREEN [3] =&gt; YELLOW ) </pre></div> </div> </div> <p class="para"> This example creates a one-based array. </p> <div class="example" id="example-72"> <p><strong>Example #15 One-based index</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$firstquarter </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'January'</span><span style="color: #007700">, </span><span style="color: #DD0000">'February'</span><span style="color: #007700">, </span><span style="color: #DD0000">'March'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$firstquarter</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Array ( [1] =&gt; &#039;January&#039; [2] =&gt; &#039;February&#039; [3] =&gt; &#039;March&#039; ) </pre></div> </div> </div> <div class="example" id="example-73"> <p><strong>Example #16 Filling an array</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// fill an array with all items from a directory<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">opendir</span><span style="color: #007700">(</span><span style="color: #DD0000">'.'</span><span style="color: #007700">);<br />while (</span><span style="color: #0000BB">false </span><span style="color: #007700">!== (</span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">readdir</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">))) {<br /> </span><span style="color: #0000BB">$files</span><span style="color: #007700">[] = </span><span style="color: #0000BB">$file</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">closedir</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <p class="para"> <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>s are ordered. The order can be changed using various sorting functions. See the <a href="ref.array.php" class="link">array functions</a> section for more information. The <span class="function"><a href="function.count.php" class="function">count()</a></span> function can be used to count the number of items in an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>. </p> <div class="example" id="example-74"> <p><strong>Example #17 Sorting an array</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />sort</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <p class="para"> Because the value of an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> can be anything, it can also be another <span class="type"><a href="language.types.array.php" class="type array">array</a></span>. This enables the creation of recursive and multi-dimensional <span class="type"><a href="language.types.array.php" class="type array">array</a></span>s. </p> <div class="example" id="example-75"> <p><strong>Example #18 Recursive and multi-dimensional arrays</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$fruits </span><span style="color: #007700">= array ( </span><span style="color: #DD0000">"fruits" </span><span style="color: #007700">=&gt; array ( </span><span style="color: #DD0000">"a" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"orange"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"b" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"banana"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"c" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"apple"<br /> </span><span style="color: #007700">),<br /> </span><span style="color: #DD0000">"numbers" </span><span style="color: #007700">=&gt; array ( </span><span style="color: #0000BB">1</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">2</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">3</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">4</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">5</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">6<br /> </span><span style="color: #007700">),<br /> </span><span style="color: #DD0000">"holes" </span><span style="color: #007700">=&gt; array ( </span><span style="color: #DD0000">"first"</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">5 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"second"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"third"<br /> </span><span style="color: #007700">)<br /> );<br /><br /></span><span style="color: #FF8000">// Some examples to address values in the array above<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$fruits</span><span style="color: #007700">[</span><span style="color: #DD0000">"holes"</span><span style="color: #007700">][</span><span style="color: #0000BB">5</span><span style="color: #007700">]; </span><span style="color: #FF8000">// prints "second"<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$fruits</span><span style="color: #007700">[</span><span style="color: #DD0000">"fruits"</span><span style="color: #007700">][</span><span style="color: #DD0000">"a"</span><span style="color: #007700">]; </span><span style="color: #FF8000">// prints "orange"<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">[</span><span style="color: #DD0000">"holes"</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">]); </span><span style="color: #FF8000">// remove "first"<br /><br />// Create a new multi-dimensional array<br /></span><span style="color: #0000BB">$juices</span><span style="color: #007700">[</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">][</span><span style="color: #DD0000">"green"</span><span style="color: #007700">] = </span><span style="color: #DD0000">"good"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> <p class="para"> <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span> assignment always involves value copying. Use the <a href="language.operators.php" class="link">reference operator</a> to copy an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> by reference. </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$arr1 </span><span style="color: #007700">= array(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$arr2 </span><span style="color: #007700">= </span><span style="color: #0000BB">$arr1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$arr2</span><span style="color: #007700">[] = </span><span style="color: #0000BB">4</span><span style="color: #007700">; </span><span style="color: #FF8000">// $arr2 is changed,<br /> // $arr1 is still array(2, 3)<br /><br /></span><span style="color: #0000BB">$arr3 </span><span style="color: #007700">= &amp;</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$arr3</span><span style="color: #007700">[] = </span><span style="color: #0000BB">4</span><span style="color: #007700">; </span><span style="color: #FF8000">// now $arr1 and $arr3 are the same<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> </div> </div> <div class="contribute"> <h3 class="title">Found A Problem?</h3> <div> </div> <div class="edit-bug"> <a href="https://github.com/php/doc-base/blob/master/README.md" title="This will take you to our contribution guidelines on GitHub" target="_blank" rel="noopener noreferrer">Learn How To Improve This Page</a> • <a href="https://github.com/php/doc-en/blob/master/language/types/array.xml">Submit a Pull Request</a> • <a href="https://github.com/php/doc-en/issues/new?body=From%20manual%20page:%20https:%2F%2Fphp.net%2Flanguage.types.array%0A%0A---">Report a Bug</a> </div> </div><section id="usernotes"> <div class="head"> <span class="action"><a href="/manual/add-note.php?sect=language.types.array&amp;repo=en&amp;redirect=https://www.php.net/manual/en/language.types.array.php">+<small>add a note</small></a></span> <h3 class="title">User Contributed Notes <span class="count">5 notes</span></h3> </div><div id="allnotes"> <div class="note" id="104064"> <div class="votes"> <div id="Vu104064"> <a href="/manual/vote-note.php?id=104064&amp;page=language.types.array&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd104064"> <a href="/manual/vote-note.php?id=104064&amp;page=language.types.array&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V104064" title="55% like this..."> 128 </div> </div> <a href="#104064" class="name"> <strong class="user"><em>mlvljr</em></strong></a><a class="genanchor" href="#104064"> &para;</a><div class="date" title="2011-05-20 06:37"><strong>13 years ago</strong></div> <div class="text" id="Hcom104064"> <div class="phpcode"><code><span class="html">please note that when arrays are copied, the "reference status" of their members is preserved (<a href="http://www.php.net/manual/en/language.references.whatdo.php" rel="nofollow" target="_blank">http://www.php.net/manual/en/language.references.whatdo.php</a>).</span></code></div> </div> </div> <div class="note" id="118855"> <div class="votes"> <div id="Vu118855"> <a href="/manual/vote-note.php?id=118855&amp;page=language.types.array&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd118855"> <a href="/manual/vote-note.php?id=118855&amp;page=language.types.array&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V118855" title="58% like this..."> 74 </div> </div> <a href="#118855" class="name"> <strong class="user"><em>thomas tulinsky</em></strong></a><a class="genanchor" href="#118855"> &para;</a><div class="date" title="2016-02-17 08:10"><strong>9 years ago</strong></div> <div class="text" id="Hcom118855"> <div class="phpcode"><code><span class="html">I think your first, main example is needlessly confusing, very confusing to newbies:<br /><br />$array = array(<br /> "foo" =&gt; "bar",<br /> "bar" =&gt; "foo",<br />);<br /><br />It should be removed.<br /> <br />For newbies:<br />An array index can be any string value, even a value that is also a value in the array. <br />The value of array["foo"] is "bar".<br />The value of array["bar"] is "foo"<br /><br />The following expressions are both true:<br />$array["foo"] == "bar"<br />$array["bar"] == "foo"</span></code></div> </div> </div> <div class="note" id="80311"> <div class="votes"> <div id="Vu80311"> <a href="/manual/vote-note.php?id=80311&amp;page=language.types.array&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd80311"> <a href="/manual/vote-note.php?id=80311&amp;page=language.types.array&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V80311" title="52% like this..."> 59 </div> </div> <a href="#80311" class="name"> <strong class="user"><em>ken underscore yap atsign email dot com</em></strong></a><a class="genanchor" href="#80311"> &para;</a><div class="date" title="2008-01-09 08:00"><strong>17 years ago</strong></div> <div class="text" id="Hcom80311"> <div class="phpcode"><code><span class="html">"If you convert a NULL value to an array, you get an empty array." <br /> <br />This turns out to be a useful property. Say you have a search function that returns an array of values on success or NULL if nothing found. <br /> <br /><span class="default">&lt;?php $values </span><span class="keyword">= </span><span class="default">search</span><span class="keyword">(...); </span><span class="default">?&gt; <br /></span> <br />Now you want to merge the array with another array. What do we do if $values is NULL? No problem: <br /> <br /><span class="default">&lt;?php $combined </span><span class="keyword">= </span><span class="default">array_merge</span><span class="keyword">((array)</span><span class="default">$values</span><span class="keyword">, </span><span class="default">$other</span><span class="keyword">); </span><span class="default">?&gt; <br /></span> <br />Voila.</span></code></div> </div> </div> <div class="note" id="52124"> <div class="votes"> <div id="Vu52124"> <a href="/manual/vote-note.php?id=52124&amp;page=language.types.array&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd52124"> <a href="/manual/vote-note.php?id=52124&amp;page=language.types.array&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V52124" title="52% like this..."> 55 </div> </div> <a href="#52124" class="name"> <strong class="user"><em>jeff splat codedread splot com</em></strong></a><a class="genanchor" href="#52124"> &para;</a><div class="date" title="2005-04-21 09:16"><strong>19 years ago</strong></div> <div class="text" id="Hcom52124"> <div class="phpcode"><code><span class="html">Beware that if you're using strings as indices in the $_POST array, that periods are transformed into underscores:<br /><br />&lt;html&gt;<br />&lt;body&gt;<br /><span class="default">&lt;?php<br /> printf</span><span class="keyword">(</span><span class="string">"POST: "</span><span class="keyword">); </span><span class="default">print_r</span><span class="keyword">(</span><span class="default">$_POST</span><span class="keyword">); </span><span class="default">printf</span><span class="keyword">(</span><span class="string">"&lt;br/&gt;"</span><span class="keyword">);<br /></span><span class="default">?&gt;<br /></span>&lt;form method="post" action="<span class="default">&lt;?php </span><span class="keyword">echo </span><span class="default">$_SERVER</span><span class="keyword">[</span><span class="string">'PHP_SELF'</span><span class="keyword">]; </span><span class="default">?&gt;</span>"&gt;<br /> &lt;input type="hidden" name="Windows3.1" value="Sux"&gt;<br /> &lt;input type="submit" value="Click" /&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br />Once you click on the button, the page displays the following:<br /><br />POST: Array ( [Windows3_1] =&gt; Sux )</span></code></div> </div> </div> <div class="note" id="112289"> <div class="votes"> <div id="Vu112289"> <a href="/manual/vote-note.php?id=112289&amp;page=language.types.array&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd112289"> <a href="/manual/vote-note.php?id=112289&amp;page=language.types.array&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V112289" title="52% like this..."> 37 </div> </div> <a href="#112289" class="name"> <strong class="user"><em>chris at ocportal dot com</em></strong></a><a class="genanchor" href="#112289"> &para;</a><div class="date" title="2013-05-28 09:01"><strong>11 years ago</strong></div> <div class="text" id="Hcom112289"> <div class="phpcode"><code><span class="html">Note that array value buckets are reference-safe, even through serialization. <br /> <br /><span class="default">&lt;?php <br />$x</span><span class="keyword">=</span><span class="string">'initial'</span><span class="keyword">; <br /></span><span class="default">$test</span><span class="keyword">=array(</span><span class="string">'A'</span><span class="keyword">=&gt;&amp;</span><span class="default">$x</span><span class="keyword">,</span><span class="string">'B'</span><span class="keyword">=&gt;&amp;</span><span class="default">$x</span><span class="keyword">); <br /></span><span class="default">$test</span><span class="keyword">=</span><span class="default">unserialize</span><span class="keyword">(</span><span class="default">serialize</span><span class="keyword">(</span><span class="default">$test</span><span class="keyword">)); <br /></span><span class="default">$test</span><span class="keyword">[</span><span class="string">'A'</span><span class="keyword">]=</span><span class="string">'changed'</span><span class="keyword">; <br />echo </span><span class="default">$test</span><span class="keyword">[</span><span class="string">'B'</span><span class="keyword">]; </span><span class="comment">// Outputs "changed" <br /></span><span class="default">?&gt; <br /></span> <br />This can be useful in some cases, for example saving RAM within complex structures.</span></code></div> </div> </div></div> <div class="foot"><a href="/manual/add-note.php?sect=language.types.array&amp;repo=en&amp;redirect=https://www.php.net/manual/en/language.types.array.php">+<small>add a note</small></a></div> </section> </section><!-- layout-content --> <aside class='layout-menu'> <ul class='parent-menu-list'> <li> <a href="language.types.php">Types</a> <ul class='child-menu-list'> <li class=""> <a href="language.types.intro.php" title="Introduction">Introduction</a> </li> <li class=""> <a href="language.types.type-system.php" title="Type System">Type System</a> </li> <li class=""> <a href="language.types.null.php" title="NULL">NULL</a> </li> <li class=""> <a href="language.types.boolean.php" title="Booleans">Booleans</a> </li> <li class=""> <a href="language.types.integer.php" title="Integers">Integers</a> </li> <li class=""> <a href="language.types.float.php" title="Floating point numbers">Floating point numbers</a> </li> <li class=""> <a href="language.types.string.php" title="Strings">Strings</a> </li> <li class=""> <a href="language.types.numeric-strings.php" title="Numeric strings">Numeric strings</a> </li> <li class="current"> <a href="language.types.array.php" title="Arrays">Arrays</a> </li> <li class=""> <a href="language.types.object.php" title="Objects">Objects</a> </li> <li class=""> <a href="language.types.enumerations.php" title="Enumerations">Enumerations</a> </li> <li class=""> <a href="language.types.resource.php" title="Resources">Resources</a> </li> <li class=""> <a href="language.types.callable.php" title="Callbacks / Callables">Callbacks / Callables</a> </li> <li class=""> <a href="language.types.mixed.php" title="Mixed">Mixed</a> </li> <li class=""> <a href="language.types.void.php" title="Void">Void</a> </li> <li class=""> <a href="language.types.never.php" title="Never">Never</a> </li> <li class=""> <a href="language.types.relative-class-types.php" title="Relative class types">Relative class types</a> </li> <li class=""> <a href="language.types.singleton.php" title="Singleton types">Singleton types</a> </li> <li class=""> <a href="language.types.iterable.php" title="Iterables">Iterables</a> </li> <li class=""> <a href="language.types.declarations.php" title="Type declarations">Type declarations</a> </li> <li class=""> <a href="language.types.type-juggling.php" title="Type Juggling">Type Juggling</a> </li> </ul> </li> </ul> </aside> </div><!-- layout --> <footer> <div class="container footer-content"> <div class="row-fluid"> <ul class="footmenu"> <li><a href="/manual/en/copyright.php">Copyright &copy; 2001-2025 The PHP Documentation Group</a></li> <li><a href="/my.php">My PHP.net</a></li> <li><a href="/contact.php">Contact</a></li> <li><a href="/sites.php">Other PHP.net sites</a></li> <li><a href="/privacy.php">Privacy policy</a></li> </ul> </div> </div> </footer> <script src="/cached.php?t=1731172202&amp;f=/js/ext/jquery-3.6.0.min.js"></script> <script src="/cached.php?t=1723177202&amp;f=/js/ext/FuzzySearch.min.js"></script> <script src="/cached.php?t=1707321815&amp;f=/js/ext/mousetrap.min.js"></script> <script src="/cached.php?t=1707321815&amp;f=/js/ext/jquery.scrollTo.min.js"></script> <script src="/cached.php?t=1738028401&amp;f=/js/search.js"></script> <script src="/cached.php?t=1738742401&amp;f=/js/common.js"></script> <script type="module" src="/cached.php?t=1741309802&amp;f=/js/interactive-examples.js"></script> <a id="toTop" href="javascript:;"><span id="toTopHover"></span><img width="40" height="40" alt="To Top" src="/images/to-top@2x.png"></a> <div id="search-modal__backdrop" class="search-modal__backdrop"> <div role="dialog" aria-label="Search modal" id="search-modal" class="search-modal" > <div class="search-modal__header"> <div class="search-modal__form"> <div class="search-modal__input-icon"> <!-- https://feathericons.com search --> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </div> <input type="search" id="search-modal__input" class="search-modal__input" placeholder="Search docs" aria-label="Search docs" /> </div> <button aria-label="Close" class="search-modal__close"> <!-- https://pictogrammers.com/library/mdi/icon/close/ --> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" > <path d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z"/> </svg> </button> </div> <div role="listbox" aria-label="Search results" id="search-modal__results" class="search-modal__results" ></div> <div class="search-modal__helper-text"> <div> <kbd>↑</kbd> and <kbd>↓</kbd> to navigate • <kbd>Enter</kbd> to select • <kbd>Esc</kbd> to close </div> <div> Press <kbd>Enter</kbd> without selection to search using Google </div> </div> </div> </div> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10