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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:13:52+00:00 2026-06-14T00:13:52+00:00

My first array is Array ( [0] => Array ( [user_id] => 1 [name]

  • 0

My first array is

Array (
 [0] => Array
     (
         [user_id] => 1
         [name] => name1
     )

 [1] => Array
     (
        [user_id] => 2
        [name] => name2
    )
)

My Secondarray is

Array (
 [0] => Array
     (
         [user_id] => 2
         [name] => name2
     )

 [1] => Array
     (
        [user_id] => 3
        [name] => name3
    )
)

The common array is the array with user_id=2 how to select that array ?
I want the intersection of that two arrays.

  • 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-14T00:13:54+00:00Added an answer on June 14, 2026 at 12:13 am
    <?php
    $array1 = array(
        array(
            'user_id' => 1,
            'name'    => 'foo'
         ), 
        array(
            'user_id' => 2,
            'name'    => 'foobar'
        )
    );
    $array2 = array(
        array(
            'user_id' => 2,
            'name'    => 'foobar'
        ), 
        array(
           'user_id' => 3,
           'name'    => 'baz'
        )
    );
    
    /**
     * Return the common sub_array of tow arrays with tow dimensions
     *
     * @param $arrayA - the first tow dimension array
     * @param $arrayB - the second tow dimension array
     *
     * @return array - an empty array in case the tow arrays don't
     * have any common elements, otherwise an tow dimension array
     * containing the common elements
     */
    function get_common_array($arrayA, $arrayB) {
        $result = array();
    
        foreach($arrayA as $keyA=>$sub_arrayA) {
            foreach($sub_arrayA as $metadata=>$data) {
                if('user_id' === $metadata) {
                    foreach($arrayB as $keyB=>$sub_arrayB){
                       if(in_array($data, $sub_arrayB)) {
                            $result[] = $sub_arrayB;
                        }
                    }
                }
            } 
        }
    
        return $result;
    }
    
    var_dump(get_common_array($array1, $array2));
    

    This is an workaround, but if i where you i consider changing the geometry of the tow arrays in such a way to be able to use dedicated function implemented in PHP witch are more faster then this.

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

Sidebar

Related Questions

i have two arrays like this first array Array ( [0228] => Array (
I have an array that looks like $numbers = array('first', 'second', 'third'); I want
Suppose I have two arrays like the following: $arr2 = array(array(first, second), array(third, fourth));
I was puzzled when this query failed on me: $results = $this->Event->find('first', array('conditions'=>array( 'user_id'
I'm trying to implement a simple mysql insert that takes the user's first name
I have the following code: $selectColumns= array('user_id.user_email', 'user_details.first_name', 'user_details.last_name', 'user_details.zip', 'user_details.store_id'); $result = $handle->select()->from('user_id')
// Show the members that are in the Database $query = SELECT user_id, membership_id,
I have the code: $getCookieData = $this->Cookie->read('data'); $getUser = $this->User->find('first', array('conditions' => array('User.username' =>
The first array is called $related_docs and the second on is $all_docs. I'm trying
I'm using PostgreSQL 9.1.13 Here is what I've done so far: /* First Array

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.