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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:08:15+00:00 2026-05-26T03:08:15+00:00

Possible Duplicate: recursive array_diff()? I have a static multidimensional array which is always going

  • 0

Possible Duplicate:
recursive array_diff()?

I have a static multidimensional array which is always going to be in the same form. E.g. it will have the same keys & hierarchy.

I want to check a posted array to be in the same ‘form’ as this static array and if not error.

I have been trying various methods but they all seem to end up with a lot of if…else components and are rather messy.

Is there a succinct way to achieve this?


In response to an answer from dfsq:

$etalon = array(
    'name' => array(),
    'income' => array(
        'day'   => '',
        'month' => array(),
        'year'  => array()
    ),
    'message' => array(),
);

$test = array(
    'name' => array(),
    'income' => array(
        'day'   => '',
        'month' => array(),
        'year'  => array()
    ),
    'message' => array(),
);

// Tests
$diff = array_diff_key_recursive($etalon, $test);
var_dump(empty($diff));
print_r($diff);

And the results from that are

bool(false) 
Array ( [name] => 1 [income] => Array ( [month] => 1 [year] => 1 ) [message] => 1 ) 
  • 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-26T03:08:16+00:00Added an answer on May 26, 2026 at 3:08 am

    Author needs a solution which would test if the structure of the arrays are the same. Next function will make a job.

    /**
     * $a1 array your static array.
     * $a2 array array you want to test.
     * @return array difference between arrays. empty result means $a1 and $a2 has the same structure.
     */ 
    function array_diff_key_recursive($a1, $a2)
    {
        $r = array();
    
        foreach ($a1 as $k => $v)
        {
            if (is_array($v))
            {
                if (!isset($a2[$k]) || !is_array($a2[$k]))
                {
                    $r[$k] = $a1[$k];
                }
                else
                {
                    if ($diff = array_diff_key_recursive($a1[$k], $a2[$k]))
                    {
                        $r[$k] = $diff;
                    }
                }
            }
            else
            {
                if (!isset($a2[$k]) || is_array($a2[$k]))
                {
                    $r[$k] = $v;
                }
            }
        }
    
        return $r;
    }
    

    And test it:

    $etalon = array(
        'name' => '',
        'income' => array(
            'day'   => '',
            'month' => array(),
            'year'  => array()
        ),
        'message' => ''
    );
    
    $test = array(
        'name' => 'Tomas Brook',
        'income' => array(
            'day'   => 123,
            'month' => 123,
            'year'  => array()
        )
    );
    
    // Tests
    $diff = array_diff_key_recursive($etalon, $test);
    var_dump(empty($diff));
    print_r($diff);
    

    This will output:

    bool(false)
    Array
    (
        [income] => Array
        (
            [month] => Array()
    
        )
    
        [message] => 
    )
    

    So checking for emptiness of $diff array will tell you if arrays have the same structure.

    Note: if you need you can also test it in other direction to see if test array has some extra keys which are not present in original static array.

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

Sidebar

Related Questions

Possible Duplicate: Can every recursion be converted into iteration? Can always or which cases
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: What is a RECURSIVE Function in PHP? What is a recursive function
Possible Duplicate: Solution to a recursive problem (code kata) give an algorithm to find
Possible Duplicate: [F#] How to have two methods calling each other? Hello all, I
Possible Duplicate: Can a recursive function be inline? I think that recursive function defined
Possible Duplicate: Write a recursive function that reverses the input Recently, I've been reading
Possible Duplicate: Design patterns for converting recursive algorithms to iterative ones I can only
Possible Duplicate: Magento Packaging an Extension Hi, I have created a wireframe theme for

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.