如何使用PHP合并键差等于1或等于2的两个或多个数组?
假设我有这样一个数组:如何使用PHP合并键差等于1或等于2的两个或多个数组?,php,Php,假设我有这样一个数组: $array1 =array( '1' => array( 'a' => 'value 1a' ), '2' => array( 'b' => 'value 2b' ), '4' => array( 'c' =>
$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();
$yourray=array(
“1”=>数组(
'a'=>'值1a'
),
“2”=>数组(
“b”=>“值2b”
),
“4”=>数组(
‘c’=>‘值4a’
),
'9'=>数组(
'd'=>'值9c'
),
“12”=>数组(
“e”=>“值12e”
)
);
$previousKey=null;
$arraykey\u to\u merge=null;
$madarray=array();
ksort($yourray)//不需要当前场景。。。。
foreach($yourarray作为$key=>$newarray)
{
如果($previousKey==null)
{
$madarray[$key]=$newarray;
$previousKey=$key;
继续;
}
$difference=(int)$key-(int)$previousKey;
如果($difference是一种与请求相匹配的解决方案,该解决方案仅组合与差值仅为1
或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;
}
评论
问题中未提及列表是否始终按键排序(因为这是数据如何来自数据库,f.e.)或键的当前顺序是否正确且不得更改
如果不能保证键按升序排列,但必须按升序排列,则必须在foreach()
循环前面调用,以获得预期结果
备注#2
通过合并条件,将if
语句(第一个和第三个)中相同赋值的两个块组合在一起,代码可以编写得更加紧凑(在下面的列表中看起来更大,因为它充满了注释):
//以$results计算结果
$result=array();
//记住$results的最后一个键;我们需要将新项目合并到其中
$lastKey=NULL;
//记住$array1的前一个键;我们将当前键与它进行比较
$prevKey=NULL;
//处理$array1,一次处理一个项目
foreach($array1作为$key=>$item){
//如果有上一项,则与上一项进行比较
//(即跳过第一次迭代的比较)
如果(isset($prevKey)和($key-$prevKey首先我认为9-2=7**NOT OK**
将9-4=5**NOT OK**
并且您尝试了什么?1)9-2=7
似乎与其他模式不匹配2)您的OK和NOT OK与您预期的输出不匹配。3)最重要的问题是:你到底想做什么?!我得到的唯一一件事是,如果下一个数组键是@Rizier123,我想Ok
意味着合并数据,notok
意味着不合并数据。我是根据他想要的输出来告诉你的。@Rizier123,实际上,如果连续差与当前值相同,他想做什么t索引然后合并。问题是不明确的。文本中说,仅当键之间的差异为1
或2
时,才希望合并值,但在示例中,您合并了与键1
和4
相关的值(使用2
作为粘合剂)。上次我检查时,4-1
仍然是3
。如果你不开始,其他一切都是徒劳的。@axiac感谢你的建议什么ksort()
在这里可以吗?请解释一下……您的代码假设数组键已按升序排序。这可能是因为在问题中发布的示例中它们就是这样排序的。我将通过调用ksort()开始代码
只是为了确保键确实已排序。否则代码将失败。再想想,如果项目已排序(来自数据库,f.e.)或必须使用当前顺序处理,则调用ksort()
不会有帮助。@axiac如果您认为我的答案可以改进,请编辑:)@Praveen Kumar非常感谢您的建议。它工作正常,但在应用到我的应用程序时仍然会出现提示和警告。它工作正常。谢谢您的帮助,我喜欢您的代码看起来更简单,我会尝试在我的应用程序中应用……谢谢
$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;
}
// 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;
}