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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:29:04+00:00 2026-06-17T11:29:04+00:00

I have an array AllUsers as Array AllUsers ( [0] => Array ( [0]

  • 0

I have an array AllUsers as

Array AllUsers
(
    [0] => Array
         (
             [0] => Array
                  (
                      [0] => Tim
                      [1] => tim@gmail.com
                  )
             [1] => Array
                  (
                      [0] => John
                      [1] => john@gmail.com
                  )
         )
    [1] => Array
         (
             [0] => Array
                  (
                      [0] => Mike
                      [1] => mike@gmail.com
                  )
             [1] => Array
                  (
                      [0] => Aron
                      [1] => aron@gmail.com
                  )
         )
)

I have another array FilteredUsers as

Array FilteredUsers
(
    [0] => Array
         (
             [0] => John
             [1] => john@yahoo.com
         )
    [1] => Array
         (
             [0] => Mike
             [1] => mike@yahoo.com
         )
    [2] => Array
         (
             [0] => Mike
             [1] => mike@outlook.com
         )
)

Now what I want is to add every element of FilteredUsers[] in AllUsers[] such that –

  1. FilteredUsers[0] should get added to Batch AllUsers[1] because Batch AllUsers[0] already has array with element name John in it
  2. Similarly FilteredUsers[1] should get added to Batch AllUsers[0]
  3. Any Batch (like AllUsers[0], AllUsers[1]) can’t have more than 3 elements. If all Batches are full, then a new batch shall be created but every element in FilteredUsers[] should be accommodated in some Batch.

So the updated AllUsers array should be something like this –

Array AllUsers
(
    [0] => Array
         (
             [0] => Array
                  (
                      [0] => Tim
                      [1] => tim@gmail.com
                  )
             [1] => Array
                  (
                      [0] => John
                      [1] => john@gmail.com
                  )
             [2] => Array
                  (
                      [0] => Mike
                      [1] => mike@yahoo.com
                  )
         )
    [1] => Array
         (
             [0] => Array
                  (
                      [0] => Mike
                      [1] => mike@gmail.com
                  )
             [1] => Array
                  (
                      [0] => Aron
                      [1] => aron@gmail.com
                  )
             [2] => Array
                  (
                      [0] => John
                      [1] => john@yahoo.com
                  )
         )
    [2] => Array
         (
             [0] => Array
                  (
                      [0] => Mike
                      [1] => mike@outlook.com
                  )
         )
)
  • 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-17T11:29:04+00:00Added an answer on June 17, 2026 at 11:29 am

    Here is the working code:

    I have created a code pastebin also for you at: http://codepad.org/iyZUpYxc

    <?php
    
    //PHP Multidimensional Array Manipulation
    
    $allUsers = array();
    $allUsers[] = array(array('name'=>'Tim', 'email'=>'tim@gmail.com'), array('name'=>'John','email'=>'john@gmail.com'));
    $allUsers[] = array(array('name'=>'Mike', 'email'=>'mike@gmail.com'), array('name'=>'Aron','email'=>'aron@gmail.com'));
    
    $filteredUsers = array();
    $filteredUsers[] = array('name'=>'John', 'email'=>'john@yahoo.com');
    $filteredUsers[] = array('name'=>'Mike', 'email'=>'mike@yahoo.com');
    $filteredUsers[] = array('name'=>'Mike', 'email'=>'mike@outlook.com');
    
    //RULE: one batch cannot have duplicate user names
    
    //print_r($allUsers);
    //print_r($filteredUsers);
    
    foreach ($filteredUsers as $filteredUser) {
      //$filteredUser is a single dimensional arrray.
    
      $filteredUserAdded = 0;
      foreach ($allUsers as &$allUser) {
         //$allUser is a muldimentional array.
    
         //Check whether the current batch contains $filteredUser['name'] value
         $intersect = array_uintersect(array($filteredUser), $allUser, 'compareName');
         if (empty($intersect) && count($allUser) < 3) {
             //We can add filtereduser here
             $allUser[] = $filteredUser; 
             $filteredUserAdded = 1;
         }
    
      } // end foreach $allUsers
    
      if ($filteredUserAdded == 0) {
          //This filtered user was not added in any existing batch.
          //Hence add it in a new batch
          $allUsers[] = array($filteredUser);       
      }
    } // end foreach $filteredUsers
    
    
    //function to compare the 'name' column value of two arrays
    function compareName($array1, $array2)
    {
       return strcmp($array1['name'], $array2['name']);
    }
    
    //Note: http://in1.php.net/array_uintersect is the key used here for comparison
    
    print_r($allUsers);
    print_r($filteredUsers);
    
    ?>
    

    Note: You have missed the array
    (
    [0] => John
    [1] => john@yahoo.com
    ) in your Output above.

    But my code outputs that also correctly.

    Thanks

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

Sidebar

Related Questions

I want to see all users which following me or another user. I have
I have array INPUTFILES with n files INPUTFILES=( file_0 ... files_n-1 ) And i
I have array of hashes: @array = [{:id => 1, :status=>R}, {:id => 1,
I have array of select tag. <select id='uniqueID' name=status> <option value=1>Present</option> <option value=2>Absent</option> </select>
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have array of strings, String[] data and it's 10 elements has value P
Say we have array of arrays: tree_limbs = Array.new tree_limbs << %w(1 2 3
When I have array of objects, when I type the name of variable in
if i have array array[0] = jack; array[1] = jill; array[2] = lisa; array[2]

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.