我有一个数组 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[]
中,这样 -
FilteredUsers[0]
应该添加到 BatchAllUsers[1]
因为 BatchAllUsers [0]
已有包含元素名称 John 的数组- 同样
FilteredUsers[1]
应该添加到 BatchAllUsers[0]
- 任何 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/