PHP 多维数组操作

标签 php arrays multidimensional-array

我有一个数组 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. 任何 Batch(如 AllUsers[0]AllUsers[1])的元素不能超过 3 个。如果所有 Batches 都已满,则应创建一个新批处理,但 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
                  )
         )
)

最佳答案

这是工作代码:

我也为您创建了一个代码 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] => 约翰 [1] => john@yahoo.com ) 在上面的输出中。

但我的代码输出也正确。

谢谢

关于PHP 多维数组操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14382037/

相关文章:

php - 将 Javascript 变量抓取到 PHP

objective-c - c/objective-c 中的数组指针

php - 如果数据库中的表不断变化,我应该如何更新

php - 用socket_write传递socket.io房间?

php - 具有缓存但没有 Smarty 的简约 PHP 模板引擎?

arrays - 在 Perl 中,是否有内置方法来比较两个数组是否相等?

java - 二维数组的问题

java - 为什么数组的长度没有更新,导致索引越界错误?

c++ - 通过 MPI 发送和接收二维数组

vb.net - 计算多维数组中的项目