3

AllUsers我有一个数组

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
                  )
         )
)

我有另一个FilteredUsers数组

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
         )
)

现在我想要的是添加FilteredUsers[]inAllUsers[]这样的每个元素 -

  1. FilteredUsers[0]应该添加到Batch AllUsers[1]中,因为Batch AllUsers[0]中已经有元素名称为 John 的数组
  2. 同样FilteredUsers[1]应该添加到批处理 AllUsers[0]
  3. 任何批次(如AllUsers[0], AllUsers[1])都不能超过 3 个元素。如果所有批次都已满,则应创建一个新批次,但其中的每个元素FilteredUsers[]都应容纳在某个批次中。

所以更新后的AllUsers数组应该是这样的 -

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
                  )
         )
)
4

2 回答 2

2

这是工作代码:

我还为您创建了一个代码 pastebin: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);

?>

注意:您在上面的输出中错过了数组( [0] => John [1] => john@yahoo.com )。

但我的代码输出也正确。

谢谢

于 2013-01-17T19:11:48.383 回答
1

接受挑战!!!

##### BORROWED FROM OMG
//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');

# using PHP's unique continue statement!    

# loop through filtered users
foreach($filteredUsers as $fUser) {
     #loop through all users batches
     foreach($allUsers as $key=>$aUsers) {
        # is it full?
        if(isset($aUsers[2]))continue;
        # any user with same name?
        foreach($aUsers as $aUser)
            if($aUser['name']==$fUser['name'])continue 2;
        # push in current batch
        $allUsers[$key][]=$fUser;
        continue 2;
    }
    # new batch needed
    $allUsers[]=$fUser;
}
var_dump($allUsers);

如果您不知道,continue接受一个“参数”,表示要冒泡的控制流的数量。

工作代码可以在这里找到。

于 2013-01-17T19:35:53.373 回答