假设我有单个数组数组。数组数组中的每个元素彼此相似,因为它们具有相似的键,但可能其中一个键具有唯一值,或者数组数组中的一个数组具有附加/不同键。例如...
$masterArray = array(
[0]=>array(id=>'123', url=>"http://xyz.com", data=>"something", data2=>"else"),
[1]=>array(id=>'123', url=>"http://xyz.com", data=>"something", data3=>"baby"),
[2]=>array(id=>'456', url=>"http://abc.com", data=>"something", data2=>"completely"),
[3]=>array(id=>'456', url=>"http://abc.com", data=>"something", data3=>"different"),
[4]=>array(id=>'789', url=>"http://def.com", data=>"something", data2=>"is not quite"),
[5]=>array(id=>'789', url=>"http://def.com", data=>"something", data3=>"right")
);
我需要由 $masterArray 创建一个新数组,该数组将基于每个单独数组中匹配的键值对将数组数组中的各个键合并到新数组中的一个新键中。所以最终的数组会看起来像这样......
$finalArray = array(
[0]=>array(id=>'123', url=>"http://xyz.com", data=>"something", data2=>"else", data3=>"baby"),
[1]=>array(id=>'456', url=>"http://abc.com", data=>"something", data2=>"completely", data3=>"different"),
[2]=>array(id=>'789', url=>"http://def.com", data=>"something", data2=>"is not quite", data3=>"right"),
);
有相当多的 php 函数数组(不是双关语)可以对数组或值进行排序/合并,但我不知道要使用哪些或如何实现它!
任何人都可以找到解决方案吗?将不胜感激。
注意:要注意,我做了以下
我尝试过的是以下算法:
- 创建 $masterArray 的两个副本(称它们为 $m1 和 $m2)
- 使用两个 for..each 循环。第一个 for...each 循环通过 $m1,第二个通过 $m2
- 从外部循环中获取 $m1 中的当前元素
- 从内部循环比较 $m2 中的当前元素
- 如果 $m1[outerloopindex] 通过它们的大部分键值匹配 $m2[innerloopindex],则创建一个新的数组元素 $x,合并它们的键值对。
- 将 $x 推送或添加到 $newArray
- (这可能有效也可能无效)从 $m1 和 $m2 中删除比较的元素(因为我们使用了它们,我们不再需要它们。假设不再匹配)
- 如果 $m1[outerloopindex] 不匹配$m2[innerloopindex] ,只需转到下一个 $m2[innerloopindex] 值并再次与 $m1[outerloopindex] 进行比较,直到匹配(或不匹配)
- 如果 $m2 中没有元素与 $m1 中比较的元素匹配,则保留 $m1[outerloopindex]
- 再次循环到 $m1[outerloopindex+1] 等。
但不幸的是,它似乎没有返回正确数量的结果:(
示例代码如下。
foreach($artistData as $key=>&$asset){
// 2. examine each element, its value is itself an array.
// $key returns the position
// reset the $artistData array
$artistData=array_values($artistData);
$currElement = $artistData[$key];
// 3. check for this secondary array's uid.
// loop through each element in this secondary array
$currElUid = $currElement['uid'];
$currElid = $currElement['id'];
$currElThumbUrl = $currElement['thumbUrl'];
// 4. then proceed down the remaining array and compare the remaining indice's array's uid with the current one being explored
$artistCount=0;
// reset the second array $artistData02;
$artistData02 = array_values($artistData02);
foreach($artistData02 as $key02=>&$asset02){
// clear our temporary new element
unset($resultElement);
$resultElement=array();
// grab the new element to compare to.
// make sure it's not the same as the original
$newCurrElement = $artistData02[$key02];
$newCurrElUid = $newCurrElement['uid'];
$newCurrElid = $newCurrElement['id'];
$newCurrElThumbUrl = $newCurrElement['thumbUrl'];
// if I already compared this element, then skip it entirely
// We also don't want to compare the element to itself.
if ($key!=$key02){
// make sure the uids match
if($currElUid==$newCurrElUid){
// make sure the thumb URLs are different
if($currElThumbUrl!=$newCurrElThumbUrl){
// create the new merged element
// grab the filetype of $currElement
switch($currElement['filetype']){
case 1:
$resultElement['imageLge']=$currElement['publicUrl'];
$resultElement['imageSml']=$currElement['thumbUrl'];
break;
case 3:
$resultElement['song']=$currElement['publicUrl'];
break;
}
// then we compare the newCurrElement and add our information
switch($newCurrElement['filetype']){
case 1:
$resultElement['imageLge']=$newCurrElement['publicUrl'];
$resultElement['imageSml']=$newCurrElement['thumbUrl'];
break;
case 3:
$resultElement['song']=$newCurrElement['publicUrl'];
break;
}
// for the remaining values, we will pass as is
$resultElement['title']=$currElement['title'];
$resultElement['uid']=$currElement['uid'];
// take the resultant $resultElement and merge it to the main array (before $resultELement is recreated)
array_push($resultArr,$resultElement);
// this will reflow the elements, and change the indexing(so the echo merge statement will change). but comparisons will be reduced
unset($artistData[$key]);
$artistData = array_values($artistData);
unset($artistData02[$key02]);
$artistData02 = array_values($artistData02);
}else{
//echo "The thumbURLs are the same. skipping... <br />";
}
}else{
//echo "not the same uids. skipping... <br />";
}
}else{
//echo "this is the same element. skipping... <br />";
}
$artistCount++;
// end inner for...each loop
}
// 7. loop and check through the remaining elements in main array again.
}
/* end outer for each loop */
// add another element - the artist count at the end
$resultArr['artistCount']= $artistCount-1;
// build a JSON object that contains the artists, the total number
echo $resultArr;
再次感谢任何帮助...