I have two multidimensional arrays like this:
我有两个这样的多维数组:
$original = Array (
[0] => Array
(
[time] => 1364690340
[memberid] => 90
[type] => single
)
[1] => Array
(
[time] => 1364690341
[memberid] => 92
[type] => fixed
)
[2] => Array
(
[time] => 1364690342
[memberid] => 96
[type] => single
)
)
and second one like this
第二个是这样的
$new = Array (
[0] => Array
(
[time] => 1364825750
[memberid] => 90
[type] => single
)
[1] => Array
(
[time] => 1364825751
[memberid] => 92
[type] => single
)
[2] => Array
(
[time] => 1364825752
[memberid] => 96
[type] => single
)
[3] => Array
(
[time] => 1364825753
[memberid] => 111
[type] => single
)
)
My problem is: I want to search $original array for matches based on memberid and type keys and if memberid and type ARE NOT the same -> I want to remove that array from $original array. So in this case I want to keep [0] Array and [2] Array as in $new array I have same memberid and same type as in original, but I would want to remove [1] Array as memberid is the same, but type is different. So my final $original array will look like this:
我的问题是:我想基于memberid和类型键搜索$ original数组的匹配,如果memberid和type不相同 - >我想从$ original数组中删除该数组。所以在这种情况下我想保持[0]数组和[2]数组,因为在$ new数组中我有相同的memberid和相同的类型,但是我想删除[1]数组,因为memberid是相同的,但是类型不同。所以我的最终$ original数组将如下所示:
$original = Array (
[0] => Array
(
[time] => 1364690340
[memberid] => 90
[type] => single
)
[1] => Array
(
[time] => 1364690342
[memberid] => 96
[type] => single
)
)
2 个解决方案
#1
2
Here you go, just tested it and it works as expected.
在这里,你去测试它,它按预期工作。
// Presuming your two arrays are still called $new & $original
$original = array(); // your data
$new = array(); // your data
$newArray = array();
foreach($original AS $key => $val){
$newArray[$val['memberid'] . '-' . $val['type']] = $val;
}
$original = array();
foreach($new AS $key => $val){
if(isset($newArray[$val['memberid'] . '-' . $val['type']])){
$original[] = $newArray[$val['memberid'] . '-' . $val['type']];
}
}
print_r($original);
#2
-1
Without making any assumptions about your data, here's an inefficient solution, O(m * n) if m and n are the lengths of your arrays:
在不对数据做任何假设的情况下,这是一个效率低下的解决方案,如果m和n是数组的长度,则为O(m * n):
$new_original = array();
foreach ($original as $elem) {
// let's see if $new has something with the same type and memberid
foreach ($new as $candidate) {
if ($candidate['type'] == $elem['type'] &&
$candidate['memberid'] == $elem['memberid']) {
// it does! let's keep $elem
$new_original[] = $elem;
}
}
}
// reassign it to $original if desired
$original = $new_original;
However, it would be much cooler to do more efficient lookups. For example, if we can assume there is at most one element with a given memberid in $new
:
但是,进行更有效的查找会更酷。例如,如果我们可以假设在$ new中最多只有一个元素具有给定的memberid:
// turn $new into a map
$new_as_map = array();
foreach ($new as $candidate) {
$new_as_map[$candidate['memberid']] = $candidate;
}
$new_original = array();
foreach ($original as $elem) {
if (isset($new_as_map[$elem['memberid']])) {
$candidate = $new_as_map[$elem['memberid']];
if ($candidate['type'] == $elem['type']) {
$new_original[] = $elem;
}
}
}
// reassign it to $original if desired
$original = $new_original;
#1
2
Here you go, just tested it and it works as expected.
在这里,你去测试它,它按预期工作。
// Presuming your two arrays are still called $new & $original
$original = array(); // your data
$new = array(); // your data
$newArray = array();
foreach($original AS $key => $val){
$newArray[$val['memberid'] . '-' . $val['type']] = $val;
}
$original = array();
foreach($new AS $key => $val){
if(isset($newArray[$val['memberid'] . '-' . $val['type']])){
$original[] = $newArray[$val['memberid'] . '-' . $val['type']];
}
}
print_r($original);
#2
-1
Without making any assumptions about your data, here's an inefficient solution, O(m * n) if m and n are the lengths of your arrays:
在不对数据做任何假设的情况下,这是一个效率低下的解决方案,如果m和n是数组的长度,则为O(m * n):
$new_original = array();
foreach ($original as $elem) {
// let's see if $new has something with the same type and memberid
foreach ($new as $candidate) {
if ($candidate['type'] == $elem['type'] &&
$candidate['memberid'] == $elem['memberid']) {
// it does! let's keep $elem
$new_original[] = $elem;
}
}
}
// reassign it to $original if desired
$original = $new_original;
However, it would be much cooler to do more efficient lookups. For example, if we can assume there is at most one element with a given memberid in $new
:
但是,进行更有效的查找会更酷。例如,如果我们可以假设在$ new中最多只有一个元素具有给定的memberid:
// turn $new into a map
$new_as_map = array();
foreach ($new as $candidate) {
$new_as_map[$candidate['memberid']] = $candidate;
}
$new_original = array();
foreach ($original as $elem) {
if (isset($new_as_map[$elem['memberid']])) {
$candidate = $new_as_map[$elem['memberid']];
if ($candidate['type'] == $elem['type']) {
$new_original[] = $elem;
}
}
}
// reassign it to $original if desired
$original = $new_original;