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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:03:43+00:00 2026-05-13T10:03:43+00:00

I have a multidimensional array that looks like this: Array ( [0] => Array

  • 0

I have a multidimensional array that looks like this:

Array
(
[0] => Array
    (
        [0] => Array
            (
                [id] => 3
            )

        [1] => Array
            (
                [id] => 1
            )

        [2] => Array
            (
                [id] => 2
            )

        [3] => Array
            (
                [id] => 5
            )

        [4] => Array
            (
                [id] => 4
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [id] => 1
            )

        [1] => Array
            (
                [id] => 3
            )

        [2] => Array
            (
                [id] => 4
            )

        [3] => Array
            (
                [id] => 5
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [id] => 3
            )

    )

)

I need to find a way to return the intersecting value(s). In this case that would be

[id] => 3

The length of the array could be different, so I can’t just use array_intersect().

  • 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-13T10:03:43+00:00Added an answer on May 13, 2026 at 10:03 am

    This would be simple if your arrays contained only integers, but as they contain an another array, it gets a bit more complicated. But this should do it:

    function custom_intersect($arrays) {
        $comp = array_shift($arrays);
        $values = array();
    
        // The other arrays are compared to the first array:
        // Get all the values from the first array for comparison
        foreach($comp as $k => $v) {
            // Set amount of matches for value to 1.
            $values[$v['id']] = 1;
        }
    
        // Loop through the other arrays
        foreach($arrays as $array) {
            // Loop through every value in array
            foreach($array as $k => $v) {
                // If the current ID exists in the compare array
                if(isset($values[$v['id']])) {
                    // Increase the amount of matches
                    $values[$v['id']]++;
                }
            }
        }
    
        $result = array();
    
        // The amount of matches for certain value must be
        // equal to the number of arrays passed, that's how
        // we know the value is present in all arrays.
        $n = count($arrays) + 1;
        foreach($values as $k => $v) {
            if($v == $n) {
                // The value was found in all arrays,
                // thus it's in the intersection
                $result[] = $v;
            }
        }
        return $result;
    }
    

    Usage:

    $arrays = array(
        array(array('id' => 3), array('id' => 1), array('id' => 2), array('id' => 5), array('id' => 4)),
        array(array('id' => 1), array('id' => 3), array('id' => 4), array('id' => 5)),
        array(array('id' => 3))
    );
    
    print_r(custom_intersect($arrays));
    

    Result:

    Array
    (
        [0] => 3
    )
    

    This function isn’t perfect: if you have duplicate ID’s in one array, it will not work. That would require a bit more code to first make the array values unique, but this will probably work in your case.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For item #1: Out of the box there’s no built… May 13, 2026 at 9:09 pm
  • Editorial Team
    Editorial Team added an answer Depending on whether or not you already use AspectJ in… May 13, 2026 at 9:09 pm
  • Editorial Team
    Editorial Team added an answer The main problem is you are trying to view the… May 13, 2026 at 9:09 pm

Related Questions

I've only dealt with one-to-one relationships in php so far, but I'm stuck on
Ok, I have some C# code that looks like this and I was wondering
I'm aware of how include? works, however not really clear using it with multidimensional
I'm starting to work with oop to build a site of user generated data.
I have a class to parse a matrix that keeps the result in an

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.