Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 62083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:25:13+00:00 2026-05-10T18:25:13+00:00

What is the most efficient way to check if an array is a flat

  • 0
  1. What is the most efficient way to check if an array is a flat array of primitive values or if it is a multidimensional array?
  2. Is there any way to do this without actually looping through an array and running is_array() on each of its elements?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:25:13+00:00Added an answer on May 10, 2026 at 6:25 pm

    The short answer is no you can’t do it without at least looping implicitly if the ‘second dimension’ could be anywhere. If it has to be in the first item, you’d just do

    is_array($arr[0]); 

    But, the most efficient general way I could find is to use a foreach loop on the array, shortcircuiting whenever a hit is found (at least the implicit loop is better than the straight for()):

    $ more multi.php <?php  $a = array(1 => 'a',2 => 'b',3 => array(1,2,3)); $b = array(1 => 'a',2 => 'b'); $c = array(1 => 'a',2 => 'b','foo' => array(1,array(2)));  function is_multi($a) {     $rv = array_filter($a,'is_array');     if(count($rv)>0) return true;     return false; }  function is_multi2($a) {     foreach ($a as $v) {         if (is_array($v)) return true;     }     return false; }  function is_multi3($a) {     $c = count($a);     for ($i=0;$i<$c;$i++) {         if (is_array($a[$i])) return true;     }     return false; } $iters = 500000; $time = microtime(true); for ($i = 0; $i < $iters; $i++) {     is_multi($a);     is_multi($b);     is_multi($c); } $end = microtime(true); echo 'is_multi  took '.($end-$time).' seconds in $iters times\n';  $time = microtime(true); for ($i = 0; $i < $iters; $i++) {     is_multi2($a);     is_multi2($b);     is_multi2($c); } $end = microtime(true); echo 'is_multi2 took '.($end-$time).' seconds in $iters times\n'; $time = microtime(true); for ($i = 0; $i < $iters; $i++) {     is_multi3($a);     is_multi3($b);     is_multi3($c); } $end = microtime(true); echo 'is_multi3 took '.($end-$time).' seconds in $iters times\n'; ?>  $ php multi.php is_multi  took 7.53565130424 seconds in 500000 times is_multi2 took 4.56964588165 seconds in 500000 times is_multi3 took 9.01706600189 seconds in 500000 times 

    Implicit looping, but we can’t shortcircuit as soon as a match is found…

    $ more multi.php <?php  $a = array(1 => 'a',2 => 'b',3 => array(1,2,3)); $b = array(1 => 'a',2 => 'b');  function is_multi($a) {     $rv = array_filter($a,'is_array');     if(count($rv)>0) return true;     return false; }  var_dump(is_multi($a)); var_dump(is_multi($b)); ?>  $ php multi.php bool(true) bool(false) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the most efficient way in C# 2.0 to check each character in
I'm trying to find out the most efficient (best performance) way to check date
What's the most efficient way to convert the output of this function from a
Which is the most reliable way to check if a character array is empty?
What's the most efficient way to concatenate strings?
What's the most efficient way to resize large images in PHP? I'm currently using
What is the most efficient way to determine how many comments a particular blog
Whats the most efficient way of selecting total number of records from a large
What would be the most efficient way of recording to a log (.txt) from
What is the most efficient way to get the default constructor (i.e. instance constructor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.