PHP多维数组操作



我有一个数组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[]的每一个元素都加到AllUsers[]中,这样-

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

所以更新后的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
                  )
         )
)

工作代码:

我已经为你创建了一个代码粘贴库: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] =>约翰[1] => john@yahoo.com)。

但是我的代码也正确地输出了

谢谢

接受挑战!!

##### 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接受一个"参数",表示要冒泡的控制流的数量。

工作代码可以在这里找到

相关内容

  • 没有找到相关文章

最新更新