CINXE.COM
PHP: is_array - 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: is_array - 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/function.is-array.php"> <link rel="shorturl" href="https://www.php.net/is-array"> <link rel="alternate" href="https://www.php.net/is-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/ref.var.php"> <link rel="prev" href="https://www.php.net/manual/en/function.intval.php"> <link rel="next" href="https://www.php.net/manual/en/function.is-bool.php"> <link rel="alternate" href="https://www.php.net/manual/en/function.is-array.php" hreflang="en"> <link rel="alternate" href="https://www.php.net/manual/de/function.is-array.php" hreflang="de"> <link rel="alternate" href="https://www.php.net/manual/es/function.is-array.php" hreflang="es"> <link rel="alternate" href="https://www.php.net/manual/fr/function.is-array.php" hreflang="fr"> <link rel="alternate" href="https://www.php.net/manual/it/function.is-array.php" hreflang="it"> <link rel="alternate" href="https://www.php.net/manual/ja/function.is-array.php" hreflang="ja"> <link rel="alternate" href="https://www.php.net/manual/pt_BR/function.is-array.php" hreflang="pt_BR"> <link rel="alternate" href="https://www.php.net/manual/ru/function.is-array.php" hreflang="ru"> <link rel="alternate" href="https://www.php.net/manual/tr/function.is-array.php" hreflang="tr"> <link rel="alternate" href="https://www.php.net/manual/uk/function.is-array.php" hreflang="uk"> <link rel="alternate" href="https://www.php.net/manual/zh/function.is-array.php" hreflang="zh"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1707321815&f=/fonts/Fira/fira.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1707321815&f=/fonts/Font-Awesome/css/fontello.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1732428602&f=/styles/theme-base.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1730558402&f=/styles/theme-medium.css" media="screen"> <base href="https://www.php.net/manual/en/function.is-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> <div class="headsup"><a href='/conferences/index.php#2024-12-09-1'>ConFoo 2025</a></div> <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="function.is-bool.php"> is_bool » </a> </div> <div class="prev"> <a href="function.intval.php"> « intval </a> </div> <ul> <li><a href='index.php'>PHP Manual</a></li> <li><a href='funcref.php'>Function Reference</a></li> <li><a href='refs.basic.vartype.php'>Variable and Type Related Extensions</a></li> <li><a href='book.var.php'>Variable handling</a></li> <li><a href='ref.var.php'>Variable handling Functions</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/function.is-array.php' selected="selected">English</option> <option value='de/function.is-array.php'>German</option> <option value='es/function.is-array.php'>Spanish</option> <option value='fr/function.is-array.php'>French</option> <option value='it/function.is-array.php'>Italian</option> <option value='ja/function.is-array.php'>Japanese</option> <option value='pt_BR/function.is-array.php'>Brazilian Portuguese</option> <option value='ru/function.is-array.php'>Russian</option> <option value='tr/function.is-array.php'>Turkish</option> <option value='uk/function.is-array.php'>Ukrainian</option> <option value='zh/function.is-array.php'>Chinese (Simplified)</option> <option value='help-translate.php'>Other</option> </select> </fieldset> </form> </div> </div><div id="function.is-array" class="refentry"> <div class="refnamediv"> <h1 class="refname">is_array</h1> <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">is_array</span> — <span class="dc-title">Finds whether a variable is an array</span></p> </div> <div class="refsect1 description" id="refsect1-function.is-array-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="methodname"><strong>is_array</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div> <p class="para rdfs-comment"> Finds whether the given variable is an array. </p> </div> <div class="refsect1 parameters" id="refsect1-function.is-array-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt><code class="parameter">value</code></dt> <dd> <p class="para"> The variable being evaluated. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.is-array-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> if <code class="parameter">value</code> is an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> otherwise. </p> </div> <div class="refsect1 examples" id="refsect1-function.is-array-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5495"> <p><strong>Example #1 Check that variable is an array</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$yes </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'this'</span><span style="color: #007700">, </span><span style="color: #DD0000">'is'</span><span style="color: #007700">, </span><span style="color: #DD0000">'an array'</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$yes</span><span style="color: #007700">) ? </span><span style="color: #DD0000">'Array' </span><span style="color: #007700">: </span><span style="color: #DD0000">'not an Array'</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$no </span><span style="color: #007700">= </span><span style="color: #DD0000">'this is a string'</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$no</span><span style="color: #007700">) ? </span><span style="color: #DD0000">'Array' </span><span style="color: #007700">: </span><span style="color: #DD0000">'not an Array'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="examplescode"><pre class="examplescode">Array not an Array</pre> </div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.is-array-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li><span class="function"><a href="function.array-is-list.php" class="function" rel="rdfs-seeAlso">array_is_list()</a> - Checks whether a given array is a list</span></li> <li><span class="function"><a href="function.is-float.php" class="function" rel="rdfs-seeAlso">is_float()</a> - Finds whether the type of a variable is float</span></li> <li><span class="function"><a href="function.is-int.php" class="function" rel="rdfs-seeAlso">is_int()</a> - Find whether the type of a variable is integer</span></li> <li><span class="function"><a href="function.is-string.php" class="function" rel="rdfs-seeAlso">is_string()</a> - Find whether the type of a variable is string</span></li> <li><span class="function"><a href="function.is-object.php" class="function" rel="rdfs-seeAlso">is_object()</a> - Finds whether a variable is an object</span></li> </ul> </p> </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/reference/var/functions/is-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%2Ffunction.is-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=function.is-array&repo=en&redirect=https://www.php.net/manual/en/function.is-array.php">+<small>add a note</small></a></span> <h3 class="title">User Contributed Notes <span class="count">19 notes</span></h3> </div><div id="allnotes"> <div class="note" id="119990"> <div class="votes"> <div id="Vu119990"> <a href="/manual/vote-note.php?id=119990&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd119990"> <a href="/manual/vote-note.php?id=119990&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V119990" title="69% like this..."> 71 </div> </div> <a href="#119990" class="name"> <strong class="user"><em>Rob Thomas</em></strong></a><a class="genanchor" href="#119990"> ¶</a><div class="date" title="2016-10-05 10:40"><strong>8 years ago</strong></div> <div class="text" id="Hcom119990"> <div class="phpcode"><code><span class="html">Please note that the 'cast to array' check is horrendously out of date.<br /><br />Running that code against PHP 5.6 results in this:<br /><br />is_array : 0.93975400924683<br />cast, === : 1.2425191402435<br /><br />So, please use 'is_array', not the horrible casting hack.</span></code></div> </div> </div> <div class="note" id="89332"> <div class="votes"> <div id="Vu89332"> <a href="/manual/vote-note.php?id=89332&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd89332"> <a href="/manual/vote-note.php?id=89332&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V89332" title="53% like this..."> 41 </div> </div> <a href="#89332" class="name"> <strong class="user"><em>skaimauve at yahoo dot ca</em></strong></a><a class="genanchor" href="#89332"> ¶</a><div class="date" title="2009-03-03 12:12"><strong>15 years ago</strong></div> <div class="text" id="Hcom89332"> <div class="phpcode"><code><span class="html">Or you could make use of the array_diff_key and array_key function:<br /><br /><span class="default"><?php<br /><br /></span><span class="keyword">function </span><span class="default">is_assoc</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">)<br />{<br /> return </span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">) && </span><span class="default">array_diff_key</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">,</span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">)));<br />}<br /><br />function </span><span class="default">test</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">)<br />{<br /> echo </span><span class="default">is_assoc</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">) ? </span><span class="string">"I'm an assoc array.\n" </span><span class="keyword">: </span><span class="string">"I'm not an assoc array.\n"</span><span class="keyword">;<br />}<br /><br /></span><span class="comment">// an assoc array<br /></span><span class="default">$a </span><span class="keyword">= array(</span><span class="string">"a"</span><span class="keyword">=></span><span class="string">"aaa"</span><span class="keyword">,</span><span class="string">"b"</span><span class="keyword">=></span><span class="default">1</span><span class="keyword">,</span><span class="string">"c"</span><span class="keyword">=></span><span class="default">true</span><span class="keyword">);<br /></span><span class="default">test</span><span class="keyword">(</span><span class="default">$a</span><span class="keyword">);<br /><br /></span><span class="comment">// an array<br /></span><span class="default">$b </span><span class="keyword">= </span><span class="default">array_values</span><span class="keyword">(</span><span class="default">$a</span><span class="keyword">);<br /></span><span class="default">test</span><span class="keyword">(</span><span class="default">$b</span><span class="keyword">);<br /><br /></span><span class="comment">// an object<br /></span><span class="default">$c </span><span class="keyword">= (object)</span><span class="default">$a</span><span class="keyword">;<br /></span><span class="default">test</span><span class="keyword">(</span><span class="default">$c</span><span class="keyword">);<br /><br /></span><span class="comment">// other types<br /></span><span class="default">test</span><span class="keyword">(</span><span class="default">$a</span><span class="keyword">-></span><span class="default">a</span><span class="keyword">);<br /></span><span class="default">test</span><span class="keyword">(</span><span class="default">$a</span><span class="keyword">-></span><span class="default">b</span><span class="keyword">);<br /></span><span class="default">test</span><span class="keyword">(</span><span class="default">$a</span><span class="keyword">-></span><span class="default">c</span><span class="keyword">);<br /><br /></span><span class="default">?><br /></span><br />The above code outputs:<br />I'm an assoc array.<br />I'm not an assoc array.<br />I'm not an assoc array.<br />I'm not an assoc array.<br />I'm not an assoc array.<br />I'm not an assoc array.</span></code></div> </div> </div> <div class="note" id="119847"> <div class="votes"> <div id="Vu119847"> <a href="/manual/vote-note.php?id=119847&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd119847"> <a href="/manual/vote-note.php?id=119847&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V119847" title="59% like this..."> 24 </div> </div> <a href="#119847" class="name"> <strong class="user"><em>ari at umbrella dot bz</em></strong></a><a class="genanchor" href="#119847"> ¶</a><div class="date" title="2016-09-05 08:34"><strong>8 years ago</strong></div> <div class="text" id="Hcom119847"> <div class="phpcode"><code><span class="html">hperrin's results have indeed changed in PHP 7. The opposite is now true, is_array is faster than comparison:<br /><br />is_array : 0.52148389816284<br />cast, === : 0.84179711341858<br /><br />Tested 1000000 iterations.</span></code></div> </div> </div> <div class="note" id="84488"> <div class="votes"> <div id="Vu84488"> <a href="/manual/vote-note.php?id=84488&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd84488"> <a href="/manual/vote-note.php?id=84488&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V84488" title="52% like this..."> 34 </div> </div> <a href="#84488" class="name"> <strong class="user"><em>alex frase</em></strong></a><a class="genanchor" href="#84488"> ¶</a><div class="date" title="2008-07-16 08:05"><strong>16 years ago</strong></div> <div class="text" id="Hcom84488"> <div class="phpcode"><code><span class="html">Yet another simpler, faster is_assoc():<br /><br /><span class="default"><?php<br /></span><span class="keyword">function </span><span class="default">is_assoc</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) {<br /> foreach (</span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) as </span><span class="default">$k </span><span class="keyword">=> </span><span class="default">$v</span><span class="keyword">) {<br /> if (</span><span class="default">$k </span><span class="keyword">!== </span><span class="default">$v</span><span class="keyword">)<br /> return </span><span class="default">true</span><span class="keyword">;<br /> }<br /> return </span><span class="default">false</span><span class="keyword">;<br />}<br /></span><span class="default">?><br /></span><br />In my tests it runs about twice as fast as Michael/Gabriel's array_reduce() method.<br /><br />(Speaking of which: Gabriel's version doesn't work as written; it reports associative arrays as numeric if only the first key is non-numeric, or if the keys are numeric but ordered backwards. Michael solves this problem by comparing array_reduce() to count(), but that costs another function call; it also works to just compare to -1 instead of 0, and therefore return -1 as the ternary else from the callback).</span></code></div> </div> </div> <div class="note" id="98156"> <div class="votes"> <div id="Vu98156"> <a href="/manual/vote-note.php?id=98156&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd98156"> <a href="/manual/vote-note.php?id=98156&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V98156" title="52% like this..."> 33 </div> </div> <a href="#98156" class="name"> <strong class="user"><em>hperrin at gmail dot com</em></strong></a><a class="genanchor" href="#98156"> ¶</a><div class="date" title="2010-05-28 10:02"><strong>14 years ago</strong></div> <div class="text" id="Hcom98156"> <div class="phpcode"><code><span class="html">I've found a faster way of determining an array. If you use is_array() millions of times, you will notice a *huge* difference. On my machine, this method takes about 1/4 the time of using is_array(). <br /> <br />Cast the value to an array, then check (using ===) if it is identical to the original. <br /> <br /><span class="default"><?php <br /></span><span class="keyword">if ( (array) </span><span class="default">$unknown </span><span class="keyword">!== </span><span class="default">$unknown </span><span class="keyword">) { <br /> echo </span><span class="string">'$unknown is not an array'</span><span class="keyword">; <br />} else { <br /> echo </span><span class="string">'$unknown is an array'</span><span class="keyword">; <br />} <br /></span><span class="default">?> <br /></span> <br />You can use this script to test the speed of both methods. <br /> <br /><pre> <br />What's faster for determining arrays? <br /> <br /><span class="default"><?php <br /> <br />$count </span><span class="keyword">= </span><span class="default">1000000</span><span class="keyword">; <br /> <br /></span><span class="default">$test </span><span class="keyword">= array(</span><span class="string">'im'</span><span class="keyword">, </span><span class="string">'an'</span><span class="keyword">, </span><span class="string">'array'</span><span class="keyword">); <br /></span><span class="default">$test2 </span><span class="keyword">= </span><span class="string">'im not an array'</span><span class="keyword">; <br /></span><span class="default">$test3 </span><span class="keyword">= (object) array(</span><span class="string">'im' </span><span class="keyword">=> </span><span class="string">'not'</span><span class="keyword">, </span><span class="string">'going' </span><span class="keyword">=> </span><span class="string">'to be'</span><span class="keyword">, </span><span class="string">'an' </span><span class="keyword">=> </span><span class="string">'array'</span><span class="keyword">); <br /></span><span class="default">$test4 </span><span class="keyword">= </span><span class="default">42</span><span class="keyword">; <br /></span><span class="comment">// Set this now so the first for loop doesn't do the extra work. <br /></span><span class="default">$i </span><span class="keyword">= </span><span class="default">$start_time </span><span class="keyword">= </span><span class="default">$end_time </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">; <br /> <br /></span><span class="default">$start_time </span><span class="keyword">= </span><span class="default">microtime</span><span class="keyword">(</span><span class="default">true</span><span class="keyword">); <br />for (</span><span class="default">$i </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">; </span><span class="default">$i </span><span class="keyword">< </span><span class="default">$count</span><span class="keyword">; </span><span class="default">$i</span><span class="keyword">++) { <br /> if (!</span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$test</span><span class="keyword">) || </span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$test2</span><span class="keyword">) || </span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$test3</span><span class="keyword">) || </span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$test4</span><span class="keyword">)) { <br /> echo </span><span class="string">'error'</span><span class="keyword">; <br /> break; <br /> } <br />} <br /></span><span class="default">$end_time </span><span class="keyword">= </span><span class="default">microtime</span><span class="keyword">(</span><span class="default">true</span><span class="keyword">); <br />echo </span><span class="string">'is_array : '</span><span class="keyword">.(</span><span class="default">$end_time </span><span class="keyword">- </span><span class="default">$start_time</span><span class="keyword">).</span><span class="string">"\n"</span><span class="keyword">; <br /> <br /></span><span class="default">$start_time </span><span class="keyword">= </span><span class="default">microtime</span><span class="keyword">(</span><span class="default">true</span><span class="keyword">); <br />for (</span><span class="default">$i </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">; </span><span class="default">$i </span><span class="keyword">< </span><span class="default">$count</span><span class="keyword">; </span><span class="default">$i</span><span class="keyword">++) { <br /> if (!(array) </span><span class="default">$test </span><span class="keyword">=== </span><span class="default">$test </span><span class="keyword">|| (array) </span><span class="default">$test2 </span><span class="keyword">=== </span><span class="default">$test2 </span><span class="keyword">|| (array) </span><span class="default">$test3 </span><span class="keyword">=== </span><span class="default">$test3 </span><span class="keyword">|| (array) </span><span class="default">$test4 </span><span class="keyword">=== </span><span class="default">$test4</span><span class="keyword">) { <br /> echo </span><span class="string">'error'</span><span class="keyword">; <br /> break; <br /> } <br />} <br /></span><span class="default">$end_time </span><span class="keyword">= </span><span class="default">microtime</span><span class="keyword">(</span><span class="default">true</span><span class="keyword">); <br />echo </span><span class="string">'cast, === : '</span><span class="keyword">.(</span><span class="default">$end_time </span><span class="keyword">- </span><span class="default">$start_time</span><span class="keyword">).</span><span class="string">"\n"</span><span class="keyword">; <br /> <br />echo </span><span class="string">"\nTested </span><span class="default">$count</span><span class="string"> iterations." <br /> <br /></span><span class="default">?> <br /></span></pre> <br /> <br />Prints something like: <br /> <br />What's faster for determining arrays? <br /> <br />is_array : 7.9920151233673 <br />cast, === : 1.8978719711304 <br /> <br />Tested 1000000 iterations.</span></code></div> </div> </div> <div class="note" id="86407"> <div class="votes"> <div id="Vu86407"> <a href="/manual/vote-note.php?id=86407&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd86407"> <a href="/manual/vote-note.php?id=86407&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V86407" title="53% like this..."> 32 </div> </div> <a href="#86407" class="name"> <strong class="user"><em>yousuf at philipz dot com</em></strong></a><a class="genanchor" href="#86407"> ¶</a><div class="date" title="2008-10-16 03:41"><strong>16 years ago</strong></div> <div class="text" id="Hcom86407"> <div class="phpcode"><code><span class="html">alex frase's example is fast but elanthis at awesomeplay dot com's example is faster and Ilgar's modification of alex's code is faulty (the part " || $_array[$k] !== $v"). Also, Ilgar's suggestion of giving a false return value when the variable isnt an array is not suitable in my opinion and i think checking if the array is empty would also be a suitable check before the rest of the code runs. <br /> <br />So here's the modified (is_vector) version <br /> <br /><span class="default"><?php <br /></span><span class="keyword">function </span><span class="default">is_vector</span><span class="keyword">( &</span><span class="default">$array </span><span class="keyword">) { <br /> if ( !</span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) || empty(</span><span class="default">$array</span><span class="keyword">) ) { <br /> return -</span><span class="default">1</span><span class="keyword">; <br /> } <br /> </span><span class="default">$next </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">; <br /> foreach ( </span><span class="default">$array </span><span class="keyword">as </span><span class="default">$k </span><span class="keyword">=> </span><span class="default">$v </span><span class="keyword">) { <br /> if ( </span><span class="default">$k </span><span class="keyword">!== </span><span class="default">$next </span><span class="keyword">) return </span><span class="default">true</span><span class="keyword">; <br /> </span><span class="default">$next</span><span class="keyword">++; <br /> } <br /> return </span><span class="default">false</span><span class="keyword">; <br />} <br /></span><span class="default">?> <br /></span> <br />and the modified (alex's is_assoc) version <br /> <br /><span class="default"><?php <br /></span><span class="keyword">function </span><span class="default">is_assoc</span><span class="keyword">(</span><span class="default">$_array</span><span class="keyword">) { <br /> if ( !</span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$_array</span><span class="keyword">) || empty(</span><span class="default">$array</span><span class="keyword">) ) { <br /> return -</span><span class="default">1</span><span class="keyword">; <br /> } <br /> foreach (</span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">$_array</span><span class="keyword">) as </span><span class="default">$k </span><span class="keyword">=> </span><span class="default">$v</span><span class="keyword">) { <br /> if (</span><span class="default">$k </span><span class="keyword">!== </span><span class="default">$v</span><span class="keyword">) { <br /> return </span><span class="default">true</span><span class="keyword">; <br /> } <br /> } <br /> return </span><span class="default">false</span><span class="keyword">; <br />} <br /></span><span class="default">?></span></span></code></div> </div> </div> <div class="note" id="86658"> <div class="votes"> <div id="Vu86658"> <a href="/manual/vote-note.php?id=86658&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd86658"> <a href="/manual/vote-note.php?id=86658&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V86658" title="52% like this..."> 29 </div> </div> <a href="#86658" class="name"> <strong class="user"><em>jim at akubo dot net</em></strong></a><a class="genanchor" href="#86658"> ¶</a><div class="date" title="2008-10-28 11:02"><strong>16 years ago</strong></div> <div class="text" id="Hcom86658"> <div class="phpcode"><code><span class="html">yousef's example was wrong because is_vector returned true instead of false if the key was found<br />here is the fixed version (only 2 lines differ)<br /><span class="default"><?php<br /></span><span class="keyword">function </span><span class="default">is_vector</span><span class="keyword">( &</span><span class="default">$array </span><span class="keyword">) {<br /> if ( !</span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) || empty(</span><span class="default">$array</span><span class="keyword">) ) {<br /> return -</span><span class="default">1</span><span class="keyword">;<br /> }<br /> </span><span class="default">$next </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">;<br /> foreach ( </span><span class="default">$array </span><span class="keyword">as </span><span class="default">$k </span><span class="keyword">=> </span><span class="default">$v </span><span class="keyword">) {<br /> if ( </span><span class="default">$k </span><span class="keyword">!== </span><span class="default">$next </span><span class="keyword">) return </span><span class="default">false</span><span class="keyword">;<br /> </span><span class="default">$next</span><span class="keyword">++;<br /> }<br /> return </span><span class="default">true</span><span class="keyword">;<br />}<br /></span><span class="default">?></span></span></code></div> </div> </div> <div class="note" id="73505"> <div class="votes"> <div id="Vu73505"> <a href="/manual/vote-note.php?id=73505&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd73505"> <a href="/manual/vote-note.php?id=73505&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V73505" title="52% like this..."> 24 </div> </div> <a href="#73505" class="name"> <strong class="user"><em>angelo [at] mandato <dot> com</em></strong></a><a class="genanchor" href="#73505"> ¶</a><div class="date" title="2007-02-26 01:32"><strong>17 years ago</strong></div> <div class="text" id="Hcom73505"> <div class="phpcode"><code><span class="html">The is_associative_array() and is_sequential_array() functions posted by 'rjg4013 at rit dot edu' are not accurate.<br /><br />The functions fail to recognize indexes that are not in sequence or in order. For example, array(0=>'a', 2=>'b', 1=>'c') and array(0=>'a', 3=>'b', 5=>'c') would be considered as sequential arrays. A true sequential array would be in consecutive order with no gaps in the indices.<br /><br />The following solution utilizes the array_merge properties. If only one array is given and the array is numerically indexed, the keys get re-indexed in a continuous way. The result must match the array passed to it in order to truly be a numerically indexed (sequential) array. Otherwise it can be assumed to be an associative array (something unobtainable in languages such as C).<br /><br />The following functions will work for PHP >= 4.<br /><br /><span class="default"><?php<br /> </span><span class="keyword">function </span><span class="default">is_sequential_array</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">)<br /> {<br /> return (</span><span class="default">array_merge</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">) === </span><span class="default">$var </span><span class="keyword">&& </span><span class="default">is_numeric</span><span class="keyword">( </span><span class="default">implode</span><span class="keyword">( </span><span class="default">array_keys</span><span class="keyword">( </span><span class="default">$var </span><span class="keyword">) ) ) );<br /> }<br /> <br /> function </span><span class="default">is_assoc_array</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">)<br /> {<br /> return (</span><span class="default">array_merge</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">) !== </span><span class="default">$var </span><span class="keyword">|| !</span><span class="default">is_numeric</span><span class="keyword">( </span><span class="default">implode</span><span class="keyword">( </span><span class="default">array_keys</span><span class="keyword">( </span><span class="default">$var </span><span class="keyword">) ) ) );<br /> }<br /></span><span class="default">?><br /></span><br />If you are not concerned about the actual order of the indices, you can change the comparison to == and != respectively.</span></code></div> </div> </div> <div class="note" id="115901"> <div class="votes"> <div id="Vu115901"> <a href="/manual/vote-note.php?id=115901&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd115901"> <a href="/manual/vote-note.php?id=115901&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V115901" title="56% like this..."> 14 </div> </div> <a href="#115901" class="name"> <strong class="user"><em>dmitry dot sushilov at gmail dot com</em></strong></a><a class="genanchor" href="#115901"> ¶</a><div class="date" title="2014-10-12 12:49"><strong>10 years ago</strong></div> <div class="text" id="Hcom115901"> <div class="phpcode"><code><span class="html">function is_associate_array($array)<br />{<br /> return $array === array_values($array);<br />}<br /><br />or you can add check is_array in functions</span></code></div> </div> </div> <div class="note" id="98305"> <div class="votes"> <div id="Vu98305"> <a href="/manual/vote-note.php?id=98305&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd98305"> <a href="/manual/vote-note.php?id=98305&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V98305" title="51% like this..."> 20 </div> </div> <a href="#98305" class="name"> <strong class="user"><em>JTS</em></strong></a><a class="genanchor" href="#98305"> ¶</a><div class="date" title="2010-06-08 04:22"><strong>14 years ago</strong></div> <div class="text" id="Hcom98305"> <div class="phpcode"><code><span class="html">I would change the order of the comparison, because if it is really an empty array, it is better to stop at that point before doing several 'cpu & memory intensive' function calls.<br /><br />In the end on a ratio of 3 not empty arrays to 1 empty array computed for 1000000 iterations it needed 10% less time.<br />Or the other way round:<br />It needed approx 3% to 4% more time if the array is not empty, but was at least 4 times faster on empty arrays.<br /><br />Additionally the memory consumption veritably lesser.<br /><br /><span class="default"><?php<br /></span><span class="keyword">function </span><span class="default">is_assoc</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) {<br /> return (</span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) && (</span><span class="default">count</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">)==</span><span class="default">0 </span><span class="keyword">|| </span><span class="default">0 </span><span class="keyword">!== </span><span class="default">count</span><span class="keyword">(</span><span class="default">array_diff_key</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">, </span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">))) )));<br />} <br /></span><span class="default">?></span></span></code></div> </div> </div> <div class="note" id="77744"> <div class="votes"> <div id="Vu77744"> <a href="/manual/vote-note.php?id=77744&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd77744"> <a href="/manual/vote-note.php?id=77744&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V77744" title="51% like this..."> 20 </div> </div> <a href="#77744" class="name"> <strong class="user"><em>Michael</em></strong></a><a class="genanchor" href="#77744"> ¶</a><div class="date" title="2007-09-11 12:37"><strong>17 years ago</strong></div> <div class="text" id="Hcom77744"> <div class="phpcode"><code><span class="html">A slight modification of what's below:<br /><br /><span class="default"><?php<br /><br /></span><span class="keyword">function </span><span class="default">is_assoc</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">)<br />{<br /> return </span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) && </span><span class="default">count</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) !== </span><span class="default">array_reduce</span><span class="keyword">(</span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">), </span><span class="string">'is_assoc_callback'</span><span class="keyword">, </span><span class="default">0</span><span class="keyword">);<br />}<br /><br />function </span><span class="default">is_assoc_callback</span><span class="keyword">(</span><span class="default">$a</span><span class="keyword">, </span><span class="default">$b</span><span class="keyword">)<br />{<br /> return </span><span class="default">$a </span><span class="keyword">=== </span><span class="default">$b </span><span class="keyword">? </span><span class="default">$a </span><span class="keyword">+ </span><span class="default">1 </span><span class="keyword">: </span><span class="default">0</span><span class="keyword">;<br />}<br /><br /></span><span class="default">?></span></span></code></div> </div> </div> <div class="note" id="122535"> <div class="votes"> <div id="Vu122535"> <a href="/manual/vote-note.php?id=122535&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd122535"> <a href="/manual/vote-note.php?id=122535&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V122535" title="57% like this..."> 6 </div> </div> <a href="#122535" class="name"> <strong class="user"><em>gwschnell at ewe dot net</em></strong></a><a class="genanchor" href="#122535"> ¶</a><div class="date" title="2018-03-22 04:58"><strong>6 years ago</strong></div> <div class="text" id="Hcom122535"> <div class="phpcode"><code><span class="html">An error will be thrown If is_array() is applied to a nonexisting varble.</span></code></div> </div> </div> <div class="note" id="63780"> <div class="votes"> <div id="Vu63780"> <a href="/manual/vote-note.php?id=63780&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd63780"> <a href="/manual/vote-note.php?id=63780&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V63780" title="51% like this..."> 19 </div> </div> <a href="#63780" class="name"> <strong class="user"><em>March</em></strong></a><a class="genanchor" href="#63780"> ¶</a><div class="date" title="2006-03-30 06:28"><strong>18 years ago</strong></div> <div class="text" id="Hcom63780"> <div class="phpcode"><code><span class="html">And here is another variation for a function to test if an array is associative. Based on the idea by mot4h.<br /><br /><span class="default"><?php<br /></span><span class="keyword">function </span><span class="default">is_associative</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">)<br />{<br /> if (!</span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) || empty(</span><span class="default">$array</span><span class="keyword">))<br /> return </span><span class="default">false</span><span class="keyword">;<br /><br /> </span><span class="default">$keys </span><span class="keyword">= </span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">);<br /> return </span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">$keys</span><span class="keyword">) !== </span><span class="default">$keys</span><span class="keyword">;<br />}<br /></span><span class="default">?></span></span></code></div> </div> </div> <div class="note" id="122613"> <div class="votes"> <div id="Vu122613"> <a href="/manual/vote-note.php?id=122613&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd122613"> <a href="/manual/vote-note.php?id=122613&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V122613" title="57% like this..."> 5 </div> </div> <a href="#122613" class="name"> <strong class="user"><em>rik at mmod dot co</em></strong></a><a class="genanchor" href="#122613"> ¶</a><div class="date" title="2018-04-12 05:39"><strong>6 years ago</strong></div> <div class="text" id="Hcom122613"> <div class="phpcode"><code><span class="html">Using is_array prior to an in_array within an if clause will safely escape a check against a variable that could potentially be a non-array when using in_array. For instance:<br /><br />NOTE: A real use case might be that we have a list of possible flags which in a database we have stored whether each of the flags are 0 or 1. We want a list of the flags which have the value of 1 to be returned. <br /><br />Our example here will not use so many technical artifacts, but will be based on similar logic just to get the point across.<br /><br /><span class="default"><?php<br /><br /></span><span class="comment">// We have a list of known values<br /></span><span class="default">$knownVars </span><span class="keyword">= [</span><span class="string">'apple'</span><span class="keyword">, </span><span class="string">'orange'</span><span class="keyword">];<br /><br /></span><span class="comment">// A list of values to check<br /></span><span class="default">$listToCheck </span><span class="keyword">= [</span><span class="string">'pear'</span><span class="keyword">, </span><span class="string">'banana'</span><span class="keyword">];<br /><br /></span><span class="comment">// And a method that takes a list of values to check and returns a new list<br />// of the items from said list that are found to be valid...<br /></span><span class="keyword">public function </span><span class="default">getValidItemsList</span><span class="keyword">( </span><span class="default">$listToCheck </span><span class="comment">/*['pear', 'banana']*/</span><span class="keyword">)<br />{<br /> </span><span class="default">$returnList </span><span class="keyword">= [];<br /> foreach(</span><span class="default">$listToCheck </span><span class="keyword">as </span><span class="default">$key </span><span class="keyword">=> </span><span class="default">$val</span><span class="keyword">)<br /> {<br /> if(</span><span class="default">in_array</span><span class="keyword">(</span><span class="default">$val</span><span class="keyword">, </span><span class="default">$knownVars</span><span class="keyword">))<br /> {<br /> </span><span class="default">array_push</span><span class="keyword">(</span><span class="default">$returnList</span><span class="keyword">, </span><span class="default">$val</span><span class="keyword">);<br /> }<br /> }<br /><br /> if(empty(</span><span class="default">$returnList</span><span class="keyword">))<br /> {<br /> </span><span class="comment">// We have a special case if there were no valid items found, which is the case we are going over<br /> </span><span class="keyword">return -</span><span class="default">1</span><span class="keyword">;<br /> }<br /><br /> </span><span class="comment">// Otherwise, normally returns a list of the items that were found to be valid<br /> </span><span class="keyword">return </span><span class="default">$returnList</span><span class="keyword">;<br />}<br /><br /></span><span class="comment">// Call the method and check for any valid items that can be used for some purpose<br /></span><span class="default">$validItemsList </span><span class="keyword">= </span><span class="default">getValidItemsList</span><span class="keyword">(</span><span class="default">$listToCheck</span><span class="keyword">);<br /><br /></span><span class="comment">// In this usage we could potentially get an exception because<br />// in_array() expects an array for argument #2, checking that the value != -1 does not escape the if statement:<br /></span><span class="keyword">if(isset(</span><span class="default">$validItemsList</span><span class="keyword">) && </span><span class="default">$validItemsList </span><span class="keyword">!= -</span><span class="default">1 </span><span class="keyword">&& </span><span class="default">in_array</span><span class="keyword">(</span><span class="string">'apple'</span><span class="keyword">, </span><span class="default">$validItemsList</span><span class="keyword">))<br />{<br /> </span><span class="comment">//...<br /></span><span class="keyword">}<br /><br /></span><span class="comment">// In this usage, we break free from the if statement safely:<br /></span><span class="keyword">if(isset(</span><span class="default">$validItemsList</span><span class="keyword">) && </span><span class="default">$validItemsList </span><span class="keyword">!= -</span><span class="default">1 </span><span class="keyword">&& </span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$validItemsList</span><span class="keyword">) && </span><span class="default">in_array</span><span class="keyword">(</span><span class="string">'apple'</span><span class="keyword">, </span><span class="default">$validItemsList</span><span class="keyword">))<br />{<br /> </span><span class="comment">//...<br /></span><span class="keyword">}<br /><br /></span><span class="default">?><br /></span><br />Hope that can help someone, I know it helped me.</span></code></div> </div> </div> <div class="note" id="122719"> <div class="votes"> <div id="Vu122719"> <a href="/manual/vote-note.php?id=122719&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd122719"> <a href="/manual/vote-note.php?id=122719&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V122719" title="55% like this..."> 4 </div> </div> <a href="#122719" class="name"> <strong class="user"><em>lrgbx0 at gmail dot com</em></strong></a><a class="genanchor" href="#122719"> ¶</a><div class="date" title="2018-05-10 08:48"><strong>6 years ago</strong></div> <div class="text" id="Hcom122719"> <div class="phpcode"><code><span class="html">is_assoc() Benchmark:<br /><br /><span class="default"><?php<br /><br /></span><span class="keyword">function </span><span class="default">is_assoc1</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) {<br /> if (!</span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">)) return </span><span class="default">false</span><span class="keyword">;<br /> </span><span class="default">$i </span><span class="keyword">= </span><span class="default">count</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">);<br /> while (</span><span class="default">$i </span><span class="keyword">> </span><span class="default">0</span><span class="keyword">) unset(</span><span class="default">$array</span><span class="keyword">[--</span><span class="default">$i</span><span class="keyword">]);<br /> return (bool)</span><span class="default">$array</span><span class="keyword">;<br />}<br /><br />function </span><span class="default">is_assoc2</span><span class="keyword">(&</span><span class="default">$array</span><span class="keyword">) {<br /> if (!</span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">)) return </span><span class="default">false</span><span class="keyword">;<br /> </span><span class="default">$i </span><span class="keyword">= </span><span class="default">count</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">);<br /> while (</span><span class="default">$i </span><span class="keyword">> </span><span class="default">0</span><span class="keyword">) {<br /> if (!isset(</span><span class="default">$array</span><span class="keyword">[--</span><span class="default">$i</span><span class="keyword">])) return </span><span class="default">true</span><span class="keyword">;<br /> }<br /> return </span><span class="default">false</span><span class="keyword">;<br />}<br /><br />function </span><span class="default">is_assoc3</span><span class="keyword">(&</span><span class="default">$array</span><span class="keyword">) {<br /> if (!</span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">)) return </span><span class="default">false</span><span class="keyword">;<br /> </span><span class="default">$i </span><span class="keyword">= </span><span class="default">count</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">);<br /> while (</span><span class="default">$i </span><span class="keyword">> </span><span class="default">0</span><span class="keyword">) {<br /> if (!</span><span class="default">array_key_exists</span><span class="keyword">(--</span><span class="default">$i</span><span class="keyword">, </span><span class="default">$array</span><span class="keyword">)) return </span><span class="default">true</span><span class="keyword">;<br /> }<br /> return </span><span class="default">false</span><span class="keyword">;<br />}<br /><br />function </span><span class="default">is_assoc4</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) {<br /> if (!</span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">)) return </span><span class="default">false</span><span class="keyword">;<br /> </span><span class="default">ksort</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">);<br /> foreach (</span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) as </span><span class="default">$k </span><span class="keyword">=> </span><span class="default">$v</span><span class="keyword">) {<br /> if (</span><span class="default">$k </span><span class="keyword">!== </span><span class="default">$v</span><span class="keyword">) return </span><span class="default">true</span><span class="keyword">;<br /> }<br /> return </span><span class="default">false</span><span class="keyword">;<br />}<br /><br />function </span><span class="default">is_assoc5</span><span class="keyword">(&</span><span class="default">$array</span><span class="keyword">) {<br /> return </span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) && </span><span class="default">array_diff_key</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">, </span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">));<br />}<br /><br /></span><span class="default">$arr1 </span><span class="keyword">= array(); </span><span class="comment">// not associative<br /></span><span class="default">$arr2 </span><span class="keyword">= </span><span class="default">$arr3 </span><span class="keyword">= array(</span><span class="string">'foo'</span><span class="keyword">, </span><span class="string">'bar'</span><span class="keyword">, </span><span class="string">'baz'</span><span class="keyword">, </span><span class="string">'foo'</span><span class="keyword">, </span><span class="string">'bar'</span><span class="keyword">, </span><span class="string">'baz'</span><span class="keyword">, </span><span class="string">'foo'</span><span class="keyword">, </span><span class="string">'bar'</span><span class="keyword">, </span><span class="string">'baz'</span><span class="keyword">, </span><span class="string">'foo'</span><span class="keyword">); </span><span class="comment">// not associative<br /></span><span class="default">asort</span><span class="keyword">(</span><span class="default">$arr3</span><span class="keyword">); </span><span class="comment">// not associative, shuffled keys<br /></span><span class="default">$arr4 </span><span class="keyword">= array(</span><span class="string">'foo'</span><span class="keyword">, </span><span class="string">'bar'</span><span class="keyword">, </span><span class="string">'baz'</span><span class="keyword">, </span><span class="string">'foo'</span><span class="keyword">, </span><span class="string">'bar'</span><span class="keyword">, </span><span class="default">null</span><span class="keyword">, </span><span class="string">'foo'</span><span class="keyword">, </span><span class="string">'bar'</span><span class="keyword">, </span><span class="string">'baz'</span><span class="keyword">, </span><span class="string">'foo'</span><span class="keyword">); </span><span class="comment">// not associative but is_assoc2() thinks it is<br /></span><span class="default">$arr5 </span><span class="keyword">= array(</span><span class="default">0 </span><span class="keyword">=> </span><span class="string">'foo'</span><span class="keyword">, </span><span class="default">1 </span><span class="keyword">=> </span><span class="string">'bar'</span><span class="keyword">, </span><span class="default">2 </span><span class="keyword">=> </span><span class="string">'baz'</span><span class="keyword">, </span><span class="default">3 </span><span class="keyword">=> </span><span class="string">'foo'</span><span class="keyword">, </span><span class="default">4 </span><span class="keyword">=> </span><span class="string">'bar'</span><span class="keyword">, </span><span class="default">5 </span><span class="keyword">=> </span><span class="string">'baz'</span><span class="keyword">, </span><span class="string">'foo3' </span><span class="keyword">=> </span><span class="string">'foo'</span><span class="keyword">, </span><span class="string">'bar3' </span><span class="keyword">=> </span><span class="string">'bar'</span><span class="keyword">, </span><span class="string">'baz3' </span><span class="keyword">=> </span><span class="string">'baz'</span><span class="keyword">, </span><span class="string">'foo4' </span><span class="keyword">=> </span><span class="string">'foo'</span><span class="keyword">); </span><span class="comment">// associative<br /><br /></span><span class="default">$i </span><span class="keyword">= </span><span class="default">$j </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">;<br /></span><span class="default">$time </span><span class="keyword">= array(</span><span class="default">0.0</span><span class="keyword">, </span><span class="default">0.0</span><span class="keyword">, </span><span class="default">0.0</span><span class="keyword">, </span><span class="default">0.0</span><span class="keyword">, </span><span class="default">0.0</span><span class="keyword">);<br /><br />for (</span><span class="default">$j </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">; </span><span class="default">$j </span><span class="keyword">< </span><span class="default">2000</span><span class="keyword">; </span><span class="default">$j</span><span class="keyword">++) { <br /> </span><span class="default">$time</span><span class="keyword">[</span><span class="default">0</span><span class="keyword">] -= </span><span class="default">microtime</span><span class="keyword">(</span><span class="default">true</span><span class="keyword">);<br /> for (</span><span class="default">$i </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">; </span><span class="default">$i </span><span class="keyword">< </span><span class="default">1000</span><span class="keyword">; </span><span class="default">$i</span><span class="keyword">++) {<br /> if (</span><span class="default">is_assoc1</span><span class="keyword">(</span><span class="default">$arr1</span><span class="keyword">) || </span><span class="default">is_assoc1</span><span class="keyword">(</span><span class="default">$arr2</span><span class="keyword">) || </span><span class="default">is_assoc1</span><span class="keyword">(</span><span class="default">$arr3</span><span class="keyword">) || </span><span class="default">is_assoc1</span><span class="keyword">(</span><span class="default">$arr4</span><span class="keyword">) || !</span><span class="default">is_assoc1</span><span class="keyword">(</span><span class="default">$arr5</span><span class="keyword">)) {<br /> echo </span><span class="string">'error'</span><span class="keyword">;<br /> break;<br /> }<br /> }<br /> </span><span class="default">$time</span><span class="keyword">[</span><span class="default">0</span><span class="keyword">] += </span><span class="default">microtime</span><span class="keyword">(</span><span class="default">true</span><span class="keyword">);<br /> </span><span class="default">$time</span><span class="keyword">[</span><span class="default">1</span><span class="keyword">] -= </span><span class="default">microtime</span><span class="keyword">(</span><span class="default">true</span><span class="keyword">);<br /> for (</span><span class="default">$i </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">; </span><span class="default">$i </span><span class="keyword">< </span><span class="default">1000</span><span class="keyword">; </span><span class="default">$i</span><span class="keyword">++) {<br /> if (</span><span class="default">is_assoc2</span><span class="keyword">(</span><span class="default">$arr1</span><span class="keyword">) || </span><span class="default">is_assoc2</span><span class="keyword">(</span><span class="default">$arr2</span><span class="keyword">) || </span><span class="default">is_assoc2</span><span class="keyword">(</span><span class="default">$arr3</span><span class="keyword">) || !</span><span class="default">is_assoc2</span><span class="keyword">(</span><span class="default">$arr4</span><span class="keyword">) || !</span><span class="default">is_assoc2</span><span class="keyword">(</span><span class="default">$arr5</span><span class="keyword">)) { </span><span class="comment">// $arr4 tweaked<br /> </span><span class="keyword">echo </span><span class="string">'error'</span><span class="keyword">;<br /> break;<br /> }<br /> }<br /> </span><span class="default">$time</span><span class="keyword">[</span><span class="default">1</span><span class="keyword">] += </span><span class="default">microtime</span><span class="keyword">(</span><span class="default">true</span><span class="keyword">);<br /> </span><span class="default">$time</span><span class="keyword">[</span><span class="default">2</span><span class="keyword">] -= </span><span class="default">microtime</span><span class="keyword">(</span><span class="default">true</span><span class="keyword">);<br /> for (</span><span class="default">$i </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">; </span><span class="default">$i </span><span class="keyword">< </span><span class="default">1000</span><span class="keyword">; </span><span class="default">$i</span><span class="keyword">++) {<br /> if (</span><span class="default">is_assoc3</span><span class="keyword">(</span><span class="default">$arr1</span><span class="keyword">) || </span><span class="default">is_assoc3</span><span class="keyword">(</span><span class="default">$arr2</span><span class="keyword">) || </span><span class="default">is_assoc3</span><span class="keyword">(</span><span class="default">$arr3</span><span class="keyword">) || </span><span class="default">is_assoc3</span><span class="keyword">(</span><span class="default">$arr4</span><span class="keyword">) || !</span><span class="default">is_assoc3</span><span class="keyword">(</span><span class="default">$arr5</span><span class="keyword">)) {<br /> echo </span><span class="string">'error'</span><span class="keyword">;<br /> break;<br /> }<br /> }<br /> </span><span class="default">$time</span><span class="keyword">[</span><span class="default">2</span><span class="keyword">] += </span><span class="default">microtime</span><span class="keyword">(</span><span class="default">true</span><span class="keyword">);<br /> </span><span class="default">$time</span><span class="keyword">[</span><span class="default">3</span><span class="keyword">] -= </span><span class="default">microtime</span><span class="keyword">(</span><span class="default">true</span><span class="keyword">);<br /> for (</span><span class="default">$i </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">; </span><span class="default">$i </span><span class="keyword">< </span><span class="default">1000</span><span class="keyword">; </span><span class="default">$i</span><span class="keyword">++) {<br /> if (</span><span class="default">is_assoc4</span><span class="keyword">(</span><span class="default">$arr1</span><span class="keyword">) || </span><span class="default">is_assoc4</span><span class="keyword">(</span><span class="default">$arr2</span><span class="keyword">) || </span><span class="default">is_assoc4</span><span class="keyword">(</span><span class="default">$arr3</span><span class="keyword">) || </span><span class="default">is_assoc4</span><span class="keyword">(</span><span class="default">$arr4</span><span class="keyword">) || !</span><span class="default">is_assoc4</span><span class="keyword">(</span><span class="default">$arr5</span><span class="keyword">)) {<br /> echo </span><span class="string">'error'</span><span class="keyword">;<br /> break;<br /> }<br /> }<br /> </span><span class="default">$time</span><span class="keyword">[</span><span class="default">3</span><span class="keyword">] += </span><span class="default">microtime</span><span class="keyword">(</span><span class="default">true</span><span class="keyword">);<br /> </span><span class="default">$time</span><span class="keyword">[</span><span class="default">4</span><span class="keyword">] -= </span><span class="default">microtime</span><span class="keyword">(</span><span class="default">true</span><span class="keyword">);<br /> for (</span><span class="default">$i </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">; </span><span class="default">$i </span><span class="keyword">< </span><span class="default">1000</span><span class="keyword">; </span><span class="default">$i</span><span class="keyword">++) {<br /> if (</span><span class="default">is_assoc5</span><span class="keyword">(</span><span class="default">$arr1</span><span class="keyword">) || </span><span class="default">is_assoc5</span><span class="keyword">(</span><span class="default">$arr2</span><span class="keyword">) || </span><span class="default">is_assoc5</span><span class="keyword">(</span><span class="default">$arr3</span><span class="keyword">) || </span><span class="default">is_assoc5</span><span class="keyword">(</span><span class="default">$arr4</span><span class="keyword">) || !</span><span class="default">is_assoc5</span><span class="keyword">(</span><span class="default">$arr5</span><span class="keyword">)) {<br /> echo </span><span class="string">'error'</span><span class="keyword">;<br /> break;<br /> }<br /> }<br /> </span><span class="default">$time</span><span class="keyword">[</span><span class="default">4</span><span class="keyword">] += </span><span class="default">microtime</span><span class="keyword">(</span><span class="default">true</span><span class="keyword">);<br />}<br /><br />echo </span><span class="string">'is_assoc1(): ' </span><span class="keyword">. </span><span class="default">$time</span><span class="keyword">[</span><span class="default">0</span><span class="keyword">] . </span><span class="string">"\n"</span><span class="keyword">;<br />echo </span><span class="string">'is_assoc2(): ' </span><span class="keyword">. </span><span class="default">$time</span><span class="keyword">[</span><span class="default">1</span><span class="keyword">] . </span><span class="string">"\n"</span><span class="keyword">;<br />echo </span><span class="string">'is_assoc3(): ' </span><span class="keyword">. </span><span class="default">$time</span><span class="keyword">[</span><span class="default">2</span><span class="keyword">] . </span><span class="string">"\n"</span><span class="keyword">;<br />echo </span><span class="string">'is_assoc4(): ' </span><span class="keyword">. </span><span class="default">$time</span><span class="keyword">[</span><span class="default">3</span><span class="keyword">] . </span><span class="string">"\n"</span><span class="keyword">;<br />echo </span><span class="string">'is_assoc5(): ' </span><span class="keyword">. </span><span class="default">$time</span><span class="keyword">[</span><span class="default">4</span><span class="keyword">] . </span><span class="string">"\n"</span><span class="keyword">;<br /><br /></span><span class="default">?><br /></span><br />is_assoc1() - uses unset(), a bit slow, but mem friendly and no function calls<br />is_assoc2() - uses isset(), fastest one, but returns TRUE whenever array contains NULL<br />is_assoc3() - fixed is_assoc2(), uses array_key_exists(), fast and memory friendly, and much smarter than the following (no need to check all those keys)<br />is_assoc4() - alex' version with proper check and key sorting<br />is_assoc5() - fixed a bit JTS' version, really nice one, but uses too many functions and checks all keys<br /><br />Results:<br /><br />is_assoc1(): 2.1628699302673<br />is_assoc2(): 1.1079933643341<br />is_assoc3(): 1.7120850086212<br />is_assoc4(): 3.9194552898407<br />is_assoc5(): 1.9509885311127</span></code></div> </div> </div> <div class="note" id="90929"> <div class="votes"> <div id="Vu90929"> <a href="/manual/vote-note.php?id=90929&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd90929"> <a href="/manual/vote-note.php?id=90929&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V90929" title="51% like this..."> 11 </div> </div> <a href="#90929" class="name"> <strong class="user"><em>Anonymous</em></strong></a><a class="genanchor" href="#90929"> ¶</a><div class="date" title="2009-05-16 02:18"><strong>15 years ago</strong></div> <div class="text" id="Hcom90929"> <div class="phpcode"><code><span class="html">Using empty() in the previous example posted by Anonymous will result in a "Fatal error: Can't use function return value in write context". I suggest using count() instead:<br /><br /><span class="default"><?php<br /></span><span class="keyword">function </span><span class="default">is_assoc</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) {<br /> return (</span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) && </span><span class="default">0 </span><span class="keyword">!== </span><span class="default">count</span><span class="keyword">(</span><span class="default">array_diff_key</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">, </span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">)))));<br />}<br /></span><span class="default">?></span></span></code></div> </div> </div> <div class="note" id="119784"> <div class="votes"> <div id="Vu119784"> <a href="/manual/vote-note.php?id=119784&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd119784"> <a href="/manual/vote-note.php?id=119784&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V119784" title="53% like this..."> 3 </div> </div> <a href="#119784" class="name"> <strong class="user"><em>mbrodala at pagemachine dot de</em></strong></a><a class="genanchor" href="#119784"> ¶</a><div class="date" title="2016-08-24 01:22"><strong>8 years ago</strong></div> <div class="text" id="Hcom119784"> <div class="phpcode"><code><span class="html">Notice that the benchmark results from hperrin at gmail dot com have changed in the meantime:<br /><br />is_array : 0.31888604164124<br />cast, === : 0.58448791503906<br /><br />(Using PHP 5.6.24, I expect other results with PHP 7)</span></code></div> </div> </div> <div class="note" id="124039"> <div class="votes"> <div id="Vu124039"> <a href="/manual/vote-note.php?id=124039&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd124039"> <a href="/manual/vote-note.php?id=124039&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V124039" title="50% like this..."> 0 </div> </div> <a href="#124039" class="name"> <strong class="user"><em>Daniel</em></strong></a><a class="genanchor" href="#124039"> ¶</a><div class="date" title="2019-07-14 06:54"><strong>5 years ago</strong></div> <div class="text" id="Hcom124039"> <div class="phpcode"><code><span class="html">The next post is not correct because has problems with blank array index:<br /><a href="https://www.php.net/manual/es/function.is-array.php#89332" rel="nofollow" target="_blank">https://www.php.net/manual/es/function.is-array.php#89332</a><br /><br />The next code use the above link php code<br /><br /><span class="default"><?php<br /></span><span class="keyword">function </span><span class="default">is_assoc</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">)<br />{<br /> return </span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">) && </span><span class="default">array_diff_key</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">,</span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">)));<br />}<br /><br />function </span><span class="default">test</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">)<br />{<br /> echo </span><span class="default">is_assoc</span><span class="keyword">(</span><span class="default">$var</span><span class="keyword">) ? </span><span class="string">"I'm an assoc array.\n" </span><span class="keyword">: </span><span class="string">"I'm not an assoc array.\n"</span><span class="keyword">;<br />}<br /><br /></span><span class="comment">// an assoc array<br /></span><span class="default">$a </span><span class="keyword">= array(</span><span class="string">"a"</span><span class="keyword">=></span><span class="string">"aaa"</span><span class="keyword">,</span><span class="string">"b"</span><span class="keyword">=></span><span class="default">1</span><span class="keyword">,</span><span class="string">"c"</span><span class="keyword">=></span><span class="default">true</span><span class="keyword">);<br /></span><span class="default">test</span><span class="keyword">(</span><span class="default">$a</span><span class="keyword">);<br /><br /></span><span class="comment">// maybe assoc array?<br /></span><span class="default">$b </span><span class="keyword">= array(</span><span class="default">0 </span><span class="keyword">=> </span><span class="string">"aaa"</span><span class="keyword">, </span><span class="default">1 </span><span class="keyword">=> </span><span class="default">1</span><span class="keyword">, </span><span class="default">3 </span><span class="keyword">=> </span><span class="default">true</span><span class="keyword">); </span><span class="comment">// Index 2 not exist<br /></span><span class="default">test</span><span class="keyword">(</span><span class="default">$b</span><span class="keyword">);<br /></span><span class="default">?><br /></span><br /># Output<br />I'm an assoc array.<br />I'm an assoc array.<br /><br />"Associative arrays are arrays that use named keys that you assign to them."<br /><a href="https://www.w3schools.com/php/php_arrays.asp" rel="nofollow" target="_blank">https://www.w3schools.com/php/php_arrays.asp</a><br /><br />Solution:<br /><br /><span class="default"><?php<br /></span><span class="keyword">function </span><span class="default">is_assoc</span><span class="keyword">(array </span><span class="default">$array</span><span class="keyword">)<br />{<br /> return </span><span class="default">count</span><span class="keyword">(</span><span class="default">array_filter</span><span class="keyword">(</span><span class="default">array_keys</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">), </span><span class="string">'is_string'</span><span class="keyword">)) > </span><span class="default">0</span><span class="keyword">;<br />}<br />function </span><span class="default">test</span><span class="keyword">(array </span><span class="default">$array</span><span class="keyword">)<br />{<br /> echo </span><span class="default">is_assoc</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">) ? </span><span class="string">"I'm an assoc array.\n" </span><span class="keyword">: </span><span class="string">"I'm not an assoc array.\n"</span><span class="keyword">;<br />}<br /></span><span class="comment">// an assoc array<br /></span><span class="default">$a </span><span class="keyword">= array(</span><span class="string">"a"</span><span class="keyword">=></span><span class="string">"aaa"</span><span class="keyword">,</span><span class="string">"b"</span><span class="keyword">=></span><span class="default">1</span><span class="keyword">,</span><span class="string">"c"</span><span class="keyword">=></span><span class="default">true</span><span class="keyword">);<br /></span><span class="default">test</span><span class="keyword">(</span><span class="default">$a</span><span class="keyword">);<br /></span><span class="comment">// an array<br /></span><span class="default">$b </span><span class="keyword">= array(</span><span class="default">0</span><span class="keyword">=></span><span class="string">"aaa"</span><span class="keyword">,</span><span class="default">1</span><span class="keyword">=></span><span class="default">1</span><span class="keyword">,</span><span class="default">3</span><span class="keyword">=></span><span class="default">true</span><span class="keyword">);<br /></span><span class="default">test</span><span class="keyword">(</span><span class="default">$b</span><span class="keyword">);<br /></span><span class="default">?><br /></span><br /># Output<br />I'm an assoc array.<br />I'm not an assoc array.<br /><br />If you want check pure assoc. array replace > 0 by === count($array)</span></code></div> </div> </div> <div class="note" id="66811"> <div class="votes"> <div id="Vu66811"> <a href="/manual/vote-note.php?id=66811&page=function.is-array&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd66811"> <a href="/manual/vote-note.php?id=66811&page=function.is-array&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V66811" title="50% like this..."> 2 </div> </div> <a href="#66811" class="name"> <strong class="user"><em>jupiter at nospam dot com</em></strong></a><a class="genanchor" href="#66811"> ¶</a><div class="date" title="2006-05-29 11:42"><strong>18 years ago</strong></div> <div class="text" id="Hcom66811"> <div class="phpcode"><code><span class="html">Will check a Multi-Dimentional Array to any specified level. This is a fix to 11/16/05 submission, which would break since you must supply a foreach with an array. Beware recursive functions shouldn't go over 100 deep or could break the memory stack on server.<br /><br /><span class="default"><?php<br /></span><span class="comment">// checks for multiarray to defined depth level recursively<br />// original $level must be 2 or more, else will instantly return true<br /></span><span class="keyword">function </span><span class="default">isDeepMultiArray</span><span class="keyword">(</span><span class="default">$multiarray</span><span class="keyword">, </span><span class="default">$level </span><span class="keyword">= </span><span class="default">2</span><span class="keyword">) { </span><span class="comment">// default is simple multiarray<br /> </span><span class="keyword">if (</span><span class="default">is_array</span><span class="keyword">(</span><span class="default">$multiarray</span><span class="keyword">)) { </span><span class="comment">// confirms array<br /> </span><span class="keyword">if (</span><span class="default">$level </span><span class="keyword">== </span><span class="default">1</span><span class="keyword">) { </span><span class="comment">// $level reaches 1 after specified # of recursions <br /> </span><span class="keyword">return </span><span class="default">true</span><span class="keyword">; </span><span class="comment">// returns true to recursive function conditional<br /> </span><span class="keyword">} </span><span class="comment">// end conditional<br /> </span><span class="keyword">foreach (</span><span class="default">$multiarray </span><span class="keyword">as </span><span class="default">$array</span><span class="keyword">) { </span><span class="comment">// goes one level deeper into array<br /> </span><span class="keyword">if (</span><span class="default">isDeepMultiArray</span><span class="keyword">(</span><span class="default">$array</span><span class="keyword">, </span><span class="default">$level </span><span class="keyword">- </span><span class="default">1</span><span class="keyword">)) { </span><span class="comment">// check subarray<br /> </span><span class="default">$message </span><span class="keyword">= </span><span class="string">"I'm a multiarray"</span><span class="keyword">; </span><span class="comment">// optional message<br /> </span><span class="keyword">return </span><span class="default">$message</span><span class="keyword">; </span><span class="comment">// best if $message = true so function returns boolean<br /> </span><span class="keyword">} </span><span class="comment">// end recursive function<br /> </span><span class="keyword">} </span><span class="comment">// end loop<br /> </span><span class="keyword">} else { </span><span class="comment">// not an array at specified level<br /> </span><span class="keyword">return </span><span class="default">false</span><span class="keyword">; </span><span class="comment">// is also used recursively so can't change to message<br /> </span><span class="keyword">} <br />}<br /><br />if (</span><span class="default">isDeepMultiArray</span><span class="keyword">(array(array()), </span><span class="default">2</span><span class="keyword">)); </span><span class="comment">// beware this returns true eventhough arrays are empty<br /><br /></span><span class="default">?><br /></span>BTW my notation is consistent with the PEAR manual on coding standards, which is what php.net says to follow. I hope a function like this gets included in PHP6.</span></code></div> </div> </div></div> <div class="foot"><a href="/manual/add-note.php?sect=function.is-array&repo=en&redirect=https://www.php.net/manual/en/function.is-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="ref.var.php">Variable handling Functions</a> <ul class='child-menu-list'> <li class=""> <a href="function.boolval.php" title="boolval">boolval</a> </li> <li class=""> <a href="function.debug-zval-dump.php" title="debug_​zval_​dump">debug_​zval_​dump</a> </li> <li class=""> <a href="function.doubleval.php" title="doubleval">doubleval</a> </li> <li class=""> <a href="function.empty.php" title="empty">empty</a> </li> <li class=""> <a href="function.floatval.php" title="floatval">floatval</a> </li> <li class=""> <a href="function.get-debug-type.php" title="get_​debug_​type">get_​debug_​type</a> </li> <li class=""> <a href="function.get-defined-vars.php" title="get_​defined_​vars">get_​defined_​vars</a> </li> <li class=""> <a href="function.get-resource-id.php" title="get_​resource_​id">get_​resource_​id</a> </li> <li class=""> <a href="function.get-resource-type.php" title="get_​resource_​type">get_​resource_​type</a> </li> <li class=""> <a href="function.gettype.php" title="gettype">gettype</a> </li> <li class=""> <a href="function.intval.php" title="intval">intval</a> </li> <li class="current"> <a href="function.is-array.php" title="is_​array">is_​array</a> </li> <li class=""> <a href="function.is-bool.php" title="is_​bool">is_​bool</a> </li> <li class=""> <a href="function.is-callable.php" title="is_​callable">is_​callable</a> </li> <li class=""> <a href="function.is-countable.php" title="is_​countable">is_​countable</a> </li> <li class=""> <a href="function.is-double.php" title="is_​double">is_​double</a> </li> <li class=""> <a href="function.is-float.php" title="is_​float">is_​float</a> </li> <li class=""> <a href="function.is-int.php" title="is_​int">is_​int</a> </li> <li class=""> <a href="function.is-integer.php" title="is_​integer">is_​integer</a> </li> <li class=""> <a href="function.is-iterable.php" title="is_​iterable">is_​iterable</a> </li> <li class=""> <a href="function.is-long.php" title="is_​long">is_​long</a> </li> <li class=""> <a href="function.is-null.php" title="is_​null">is_​null</a> </li> <li class=""> <a href="function.is-numeric.php" title="is_​numeric">is_​numeric</a> </li> <li class=""> <a href="function.is-object.php" title="is_​object">is_​object</a> </li> <li class=""> <a href="function.is-real.php" title="is_​real">is_​real</a> </li> <li class=""> <a href="function.is-resource.php" title="is_​resource">is_​resource</a> </li> <li class=""> <a href="function.is-scalar.php" title="is_​scalar">is_​scalar</a> </li> <li class=""> <a href="function.is-string.php" title="is_​string">is_​string</a> </li> <li class=""> <a href="function.isset.php" title="isset">isset</a> </li> <li class=""> <a href="function.print-r.php" title="print_​r">print_​r</a> </li> <li class=""> <a href="function.serialize.php" title="serialize">serialize</a> </li> <li class=""> <a href="function.settype.php" title="settype">settype</a> </li> <li class=""> <a href="function.strval.php" title="strval">strval</a> </li> <li class=""> <a href="function.unserialize.php" title="unserialize">unserialize</a> </li> <li class=""> <a href="function.unset.php" title="unset">unset</a> </li> <li class=""> <a href="function.var-dump.php" title="var_​dump">var_​dump</a> </li> <li class=""> <a href="function.var-export.php" title="var_​export">var_​export</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 © 2001-2024 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&f=/js/ext/jquery-3.6.0.min.js"></script> <script src="/cached.php?t=1723177202&f=/js/ext/FuzzySearch.min.js"></script> <script src="/cached.php?t=1707321815&f=/js/ext/mousetrap.min.js"></script> <script src="/cached.php?t=1707321815&f=/js/ext/jquery.scrollTo.min.js"></script> <script src="/cached.php?t=1733296801&f=/js/search.js"></script> <script src="/cached.php?t=1732876201&f=/js/common.js"></script> <script type="module" src="/cached.php?t=1733276402&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>