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 506957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:45:16+00:00 2026-05-13T06:45:16+00:00

I have a function that has to accept an array of points, or an

  • 0

I have a function that has to accept an array of points, or an array of array of points (a 2 or 3 dimensional array). I’m looking for an reliable way to detect whether it has 2 or 3 levels. The thing is, I cannot count on the keys of the arrays to do the checking, so this wont work:

$levels = isset($array[0][0]) && is_array($array[0][0]) ? 3 : 2;

..as the first key might not be 0. It usually is, but I don’t want to rely on this. And anyways, it’s an crappy and a close minded way to do that. Optimally, I would like to check for any number of levels without having to loop through the whole array.

Here’s what the arrays might look like:

array(5) {
    [2] => array(2) {
        [x] => 3
        [y] => 6
    }
    [3] => array(2) {
        [x] => 4
        [y] => 8
    }
    ...

And a three dimensional array would contain these arrays.

Some notes:

  • The arrays are large, so completely looping through the arrays is not a very good option
  • The arrays are numerically and sequentially indexed (with the exception of the last level, which has x and y)
  • The array keys might or might not start from 0

While writing this, I came up with a solution that might be feasible; a recursive function that checks the first item of an array, if it is, then call itself on the newly found array etc.

Are there any better, cleaner ideas? Bonus points for supporting arrays that might have both scalar values and arrays (eg. the first item of an array might be a string, but the next is an array).

  • 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. Editorial Team
    Editorial Team
    2026-05-13T06:45:17+00:00Added an answer on May 13, 2026 at 6:45 am

    If you expect a complete array or a complete array of arrays then you could try:-

    if (isset $points[0][0][0])
    

    If however your array is sparse its more difficult.
    The basic problem is that a php “array” is actually a one dimensional hash. The trick being that a value can be another “array”. So you need to access to second level to determine whether its a value or an array.

    Again if you expect a given array to contain only point values, or only other arrays you only need to check one entry so:

    if ( is_array(current(current($points))) )
    

    Should get you what you want: the current() function returns the current array pointer (which defaults to the first – so it will always be set to something), so the inside current($points) would get you $points[0] or the first entry with an actual value, likwise the outside current will get you something like $points[0][0].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 380k
  • Answers 380k
  • 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 Can't believe it after carefully debugging the app i figured… May 14, 2026 at 9:58 pm
  • Editorial Team
    Editorial Team added an answer See Inverse Cotangent. For some values, the inverse would be… May 14, 2026 at 9:58 pm
  • Editorial Team
    Editorial Team added an answer try: $(".gallery span.zoom").click(function() { var imgPath = $(this).siblings('img').attr("src"); alert(imgPath); return… May 14, 2026 at 9:58 pm

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.