假设我有一个这样的数组:
$array1 =array(
'1' => array(
'a' => 'value 1a'
),
'2' => array(
'b' => 'value 2b'
),
'4' => array(
'c' => 'value 4a'
),
'9' => array(
'd' => 'value 9c'
),
'12' => array(
'e' => 'value 12e'
)
);
我只是想组合一个数组,它只包含密钥差异小于或等于2
数组键示例:
2-1 =1 **OK**
4-2 =2 **OK**
9-2 =7 **NOT OK**
12-9 =3 **NOT OK**
最终结果($array1中的$array2)看起来像:
$array2 =array(
'1' =>array(
'a' => 'value 1a',
'b' => 'value 2b',
'c' => 'value 4c'
),
'9' => array(
'd' => 'value 9c'
),
'12' => array(
'e' => 'value 12e'
);
我尝试过一些方法,但还是没有成功。。如何使用php或使用php数组函数更容易??
感谢您的帮助
尝试使用
$yourarray =array(
'1' => array(
'a' => 'value 1a'
),
'2' => array(
'b' => 'value 2b'
),
'4' => array(
'c' => 'value 4a'
),
'9' => array(
'd' => 'value 9c'
),
'12' => array(
'e' => 'value 12e'
)
);
$previousKey = null;
$arraykey_to_merge = null;
$madearray = array();
ksort($yourarray);//no need for current scenario....
foreach($yourarray as $key => $newarray)
{
if($previousKey == null)
{
$madearray[$key] = $newarray;
$previousKey = $key;
continue;
}
$difference = (int)$key - (int)$previousKey;
if( $difference <= 2 )
{
if($arraykey_to_merge == NULL)
{
if(!isset($madearray[$key]) || $madearray[$key] == NULL)
{
$arraykey_to_merge = $previousKey;
}
$madearray[$previousKey] = array_merge($madearray[$previousKey] , $newarray);
$previousKey = $key;
}
else
{
$madearray[$arraykey_to_merge] = array_merge($madearray[$arraykey_to_merge] , $newarray);
$arraykey_to_merge = $previousKey;
$previousKey = $key;
}
continue;
}
$madearray[$key] = $newarray;
$previousKey = $key;
}
echo "<pre>";
print_r($madearray);
echo "</pre>";
die();
它一团糟,但它会为你做好工作…:)
一个匹配请求的解决方案,只组合与差值仅为1
或2
:的键对应的值
// Compute the outcome in $results
$result = array();
// Remember the last key we updated in $results
$lastKey = NULL;
foreach ($array1 as $key => $item) {
if (! isset($lastKey)) {
// No $lastKey; this is the first item of $array1
// Copy item directly to $results
$result[$key] = $item;
$lastKey = $key;
} elseif ($key - $lastKey <= 2) {
// Difference is 1 or 2 (given the keys are sorted ascending)
// Merge item into the previous one
$result[$lastKey] = array_merge($result[$lastKey], $item);
} else {
// Difference is bigger; won't merge. Create a new item in $results
$result[$key] = $item;
$lastKey = $key;
}
}
print_r($result);
正如我在评论中所指出的,您的预期输出与组合与差为1
或2
的键对应的值的请求不匹配。只要键从一个项目到下一个项目的增加不超过2
,它就会组合所有连续值。
以下代码使用此规则进行组合(组合任意数量的连续值,只要键从一个项目到下一个项目的增加不超过2
):
$result = array();
$lastKey = NULL;
$prevKey = NULL;
foreach ($array1 as $key => $item) {
if (! isset($lastKey)) {
// This is the first item in $result; there is no other item to merge into
$result[$key] = $item;
$lastKey = $key;
} elseif ($key - $prevKey <= 2) { // Compare with previous element in $array
// Merge into the last item created in $results
$result[$lastKey] = array_merge($result[$lastKey], $item);
} else {
// Cannot merge, create a new item
$result[$key] = $item;
$lastKey = $key;
}
$prevKey = $key;
}
备注
如果列表总是按关键字排序(因为这就是数据来自数据库的方式,例如),或者关键字的当前顺序正确且不得更改,则问题中没有提到这一点。
如果密钥不能保证按升序排列,但必须按升序处理,则必须将对ksort($array1)
的调用放在foreach()
循环之前,才能获得预期结果。
备注#2
通过合并条件,可以将if
语句中的两个相同赋值块(第一个和第三个)组合起来,使代码编写得更加紧凑(在下面的列表中,它看起来更大,因为它充满了注释):
// Compute the outcome in $results
$result = array();
// Remember the last key of $results; we need to merge new items into it
$lastKey = NULL;
// Remember the previous key of $array1; we compare the current key with it
$prevKey = NULL;
// Process $array1, one item at a time
foreach ($array1 as $key => $item) {
// Compare with the previous item if there is a previous item
// (i.e. skip the comparison on the first iteration)
if (isset($prevKey) && ($key - $prevKey <= 2)) {
// Merge into the previous group
$result[$lastKey] = array_merge($result[$lastKey], $item);
} else {
// This is the first item (! isset($prevKey)) or ...
// ... the difference of keys is larger than 2 (! ($key - $prevKey <= 2))
// Do not merge; copy to the results
$result[$key] = $item;
$lastKey = $key;
}
// Remember $key; it will be the previous key on the next iteration
$prevKey = $key;
}