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

  • SEARCH
  • Home
  • 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

Ask A Question

Stats

  • Questions 206k
  • Answers 206k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Each call to getchar() will wait for a character to… May 12, 2026 at 9:05 pm
  • Editorial Team
    Editorial Team added an answer After running this through IE8's debugger (Press F12, it's actually… May 12, 2026 at 9:05 pm
  • Editorial Team
    Editorial Team added an answer That should do it: memcpy(&b, &a, sizeof(a)); EDIT: By the… May 12, 2026 at 9:05 pm

Related Questions

For my server app, I need to check if an ip address is in
What is the most efficient way to monitor a queue. The follwoing piece of
As a follow up question related to my question regarding efficient way of storing
I need to write some code that performs an HTML highlight on specific keywords

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.