1

配列の単一の配列があるとします。配列の配列内の各要素は、類似したキーを持つため互いに類似していますが、キーの 1 つに一意の値があるか、配列の配列内の配列の 1 つに追加の/異なるキーがある可能性があります。例えば...

$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 関数のかなりの配列 (しゃれは意図されていません) がありますが、どれを使用するか、またはどのように実装するかを理解できません!

誰でもこれに対する解決策を見つけることができますか? それは大歓迎です。

注:注意するために、私は次のことを行いました

私が試したのは、次のアルゴリズムです。

  1. $masterArray の 2 つのコピーを作成します (それらを $m1 および $m2 と呼びます)
  2. 2 つの for..each ループを使用します。最初の for...each ループは $m1 を通過し、2 番目のループは $m2 を通過します
  3. 外側のループから $m1 の現在の要素を取得します
  4. 内側のループから $m2 の現在の要素を比較します
  5. $m1[outerloopindex] が $m2[innerloopindex] とキー値の大部分で一致する場合、新しい配列要素 $x を作成し、それらのキーと値のペアをマージします。
  6. $x を $newArray にプッシュまたは追加
  7. (これは機能する場合と機能しない場合があります) $m1 と $m2 から比較された要素を削除します (それらを使用したので、もう必要ありません。これ以上一致しないと仮定します)
  8. $m1[outerloopindex] が $m2[innerloopindex] と一致しない場合は、単純に次の $m2[innerloopindex] 値に移動し、一致するまで (または一致しないまで) $m1[outerloopindex] と再度比較します。
  9. $m2 の要素が $m1 の要素と一致しない場合、$m1[outerloopindex] を維持します。
    1. $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;

繰り返しますが、助けていただければ幸いです...

4

1 に答える 1

1

元の質問に答えるために、このコードは masterArray を取り込み、finalArray を出力します。

// sample array just for completeness
$masterArray = array(
        array('id'=>'123', 'url'=>"http://xyz.com", 'data'=>"something", 'data2'=>"else"),
        array('id'=>'123', 'url'=>"http://xyz.com", 'data'=>"something", 'data3'=>"baby"),
        array('id'=>'456', 'url'=>"http://abc.com", 'data'=>"something", 'data2'=>"completely"),
        array('id'=>'456', 'url'=>"http://abc.com", 'data'=>"something", 'data3'=>"different"),
        array('id'=>'789', 'url'=>"http://def.com", 'data'=>"something", 'data2'=>"is not quite"),
        array('id'=>'789', 'url'=>"http://def.com", 'data'=>"something", 'data3'=>"right")
);

// here's where the code actually begins
$finalArray = array();
foreach( $masterArray as $m )
{
    if( !isset( $finalArray[$m['id']] ) )
        $finalArray[$m['id']] = $m;
    else
        $finalArray[$m['id']] = array_merge( $finalArray[$m['id']], $m );
}
$finalArray = array_values( $finalArray );

$finalArray を var_dump すると、取得したいものと一致します。これはidに基づくマッチングです。

あなたのサンプル コードを十分に読んでいないのですが、id に基づいて値を集計するだけで十分ですか? もしそうなら、答えは上にあります。;)

于 2013-03-24T02:38:05.193 に答える