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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:25:29+00:00 2026-06-18T12:25:29+00:00

What is the best way to check if an array is recursive in PHP

  • 0

What is the best way to check if an array is recursive in PHP ?

Given the following code:

<?php 
$myarray = array('test',123); 
$myarray[] = &$myarray; 
print_r($myarray); 
?> 

From the PHP Manual:

The print_r() will display RECURSION when it gets to the third
element of the array.

There doesn’t appear to be any other way to scan an array for
recursive references, so if you need to check for them, you’ll have to
use print_r() with its second parameter to capture the output and look
for the word RECURSION.

Is there more elegant way of checking ?

PS. This is how I check and get the recursive array keys using regex and print_r()

$pattern = '/\n            \[(\w+)\] => Array\s+\*RECURSION\*/';
preg_match_all($pattern, print_r($value, TRUE), $matches);
$recursiveKeys =  array_unique($matches[1]);

Thanks

  • 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-06-18T12:25:30+00:00Added an answer on June 18, 2026 at 12:25 pm

    It’s always fun to try solving “impossible” problems!

    Here’s a function that will detect recursive arrays if the recursion happens at the top level:

    function is_recursive(array &$array) {
        static $uniqueObject;
        if (!$uniqueObject) {
            $uniqueObject = new stdClass;
        }
    
        foreach ($array as &$item) {
            if (!is_array($item)) {
                continue;
            }
    
            $item[] = $uniqueObject;
            $isRecursive = end($array) === $uniqueObject;
            array_pop($item);
            if ($isRecursive) {
                return true;
            }
        }
    
        return false;
    }
    

    See it in action.

    Detecting recursion at any level would obviously be more tricky, but I think we can agree that it seems doable.

    Update

    And here is the recursive (pun not intended but enjoyable nonetheless) solution that detects recursion at any level:

    function is_recursive(array &$array, array &$alreadySeen = array()) {
        static $uniqueObject;
        if (!$uniqueObject) {
            $uniqueObject = new stdClass;
        }
    
        $alreadySeen[] = &$array;
    
        foreach ($array as &$item) {
            if (!is_array($item)) {
                continue;
            }
    
            $item[] = $uniqueObject;
            $recursionDetected = false;
            foreach ($alreadySeen as $candidate) {
                if (end($candidate) === $uniqueObject) {
                    $recursionDetected = true;
                    break;
                }
            }
    
            array_pop($item);
    
            if ($recursionDetected || is_recursive($item, $alreadySeen)) {
                return true;
            }
        }
    
        return false;
    }
    

    See it in action.

    Of course this can also be written to work with iteration instead of recursion by keeping a stack manually, which would help in cases where a very large recursion level is a problem.

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

Sidebar

Related Questions

I have an array @test . What's the best way to check if each
What is the best way to check that I'm logged in to Yii from
1. What the best way to check array or object with JQuery undefined +
What would be the best way to remove an item from an observable array
What is the best way to check if an multi-dimension array contains 2 or
What is the best way to check if an array/tuple/list only contains elements in
What is the best way to check that all numbers in the array (or
What's the best/easiest way to check if an array has any values set? I
I have an array with strings. What is the best way to check if
Possible Duplicate: PHP Arrays: A good way to check if an array is associative

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.