2013-03-07 60 views
0

我與PHP的工作,並有一個數組的數組,看起來是這樣的:如何合併與同類鍵一個鍵的值不同的數組的數組元素只

Array(
    [0] => Array(
      [id] =>1, 
      [name]=>"edward", 
      [asset]=>"somesong.mp3" 
      ), 
    [1] => Array(
      [id] =>1, 
      [name]=>"edward", 
      [asset]=>"somemovie.mov" 
      ), 
    [2] => Array(
      [id] =>2, 
      [name]=>"sally", 
      [asset]=>"anothersong.mp3" 
      ), 
    [3] => Array(
      [id] =>2, 
      [name]=>"sally", 
      [asset]=>"anothermovie.mov" 
      ), 
... 
) 

正如你可以看到,他們的主陣列中每個元素之間的相似性,僅與[asset]鍵的值不同。我想合併主陣列中的元件以便每個[資產]的兩個值被保持在新的元件,這樣

FinalArray(
    [0] => Array(
      [id] =>1, 
      [name]=>"edward", 
      [song]=>"somesong.mp3", 
      [movie]=>"somemovie.mov" 
      ), 
    [1] => Array(
      [id] =>2, 
      [name]=>"sally", 
      [song]=>"anothersong.mp3", 
      [movie]=>"anothermovie.mov" 
      ) 
... 
) 

我開始使用內和外foreach()環的組合使用,探索結構

// $person and $person02 are copies of the same array 
foreach($person as $key=>$value){ 
    // grab an element in this loop 
    $currElement=$person[$key]; 
    foreach($person2 as $key2=>$value2){ 
     $currElement2=$person2[$key2]; 
     // compare $currElement to $currElement2 
     if($currElement['id']==$currElement2['id']){ 
      // determine if [asset] is an mp3 or mov 
      $currAsset2=$currElement2['asset']; 
      $currAsset =$currElement['asset']; 

      $ext = substr(strrchr($currAsset,'.'),1) 
      if($ext=='mp3'){ 
       // then we have a song and should store it 
       $song=$currAsset['asset']; 
       $movie=$currAsset2['asset']; 
      }else{ 
       // switch sides if you like 
       $song=$currAsset2['asset']; 
       $movie=$currAsset['asset']; 
      } 
     } 

     // create a new array and add it to the result array 
     $newArrEl = array(
      'id' =>$currElement['id'], 
      'name' =>$currElement['id'], 
      'song' => $song, 
      'movie' => $movie 
     ); 
     $resultArray.push(); // add to final array 
    } 
} 
} 

事情是,我探索了一堆PHP數組函數的組合,似乎無法得到它很正確。所以我希望這裏的某個人也能幫助我解決這個問題。我怎樣才能獲得原始數據與類似的值合併成新的元素添加到最終數組?

回答

0

看起來你的主號標識是id,我會做與id作爲密鑰的目標磁盤陣列,通過原始數組迭代只是一次,像這樣:

<?php 

$original_array = array(
    array(
     'id' => 1, 
     'name' => 'edwards', 
     'asset' => "somesong.mp3" 
    ), 
    array(
     'id' => 1, 
     'name' => 'edwards', 
     'asset' => "somemovie.mov" 
    ), 
    array(
     'id' => 2, 
     'name' => 'sally', 
     'asset' => "anothersong.mp3" 
    ), 
    array(
     'id' => 2, 
     'name' => 'sally', 
     'asset' => "anothermovie.mov" 
    ) 
); 

$result = array(); 

$fields_map = array(
    '/\.mp3$/iS' => 'song', 
    '/\.mov$/iS' => 'movie' 
); 

foreach($original_array as $item) 
{ 
    $id = $item['id']; 

    if (!isset($result[$id])) 
    { 
     // initial population 
     $result[$id] = array(
      'id' => $id, 
      'name' => $item['name'] 
     ); 
    } 

    if (isset($item['asset'])) 
    { 
     foreach($fields_map as $re => $field_name) 
     { 
      if (preg_match($re, $item['asset'])) 
      { 
       $result[$id][$field_name] = $item['asset']; 
       break; 
      } 
     } 
    } 
} 

var_dump($result); 

注:我使用的是正則表達式因爲我假設你可能有更多的擴展需要支持相同的字段(例如mp3,ogg,wav,flac所有映射到字段'歌曲'),而不是簡單的hashmap。正則表達式可以很容易地添加更多的字段。

+0

剛剛更新了答案,使其可以運行與提供的樣本含量 – 2013-03-07 03:31:39

0

試試這個:

$array = array(
    array(
     'id' => 1, 
     'name' => 'edwards', 
     'asset' => "somesong.mp3" 
    ), 
    array(
     'id' => 1, 
     'name' => 'edwards', 
     'asset' => "somemovie.mov" 
    ), 
    array(
     'id' => 2, 
     'name' => 'sally', 
     'asset' => "anothersong.mp3" 
    ), 
    array(
     'id' => 2, 
     'name' => 'sally', 
     'asset' => "anothermovie.mov" 
    ) 
); 

$res = array(); 

foreach($array as $val){ 
    $res[$val['id']]['id'] = $val['id']; 
    $res[$val['id']]['name'] = $val['name']; 
    if(preg_match('/mp3$/',$val['asset'])){ 
    $res[$val['id']]['song'] = $val['asset']; 
    } 
    if(preg_match('/mov$/',$val['asset'])){ 
    $res[$val['id']]['movie'] = $val['asset']; 
    } 
} 
$res = array_values($res); 

echo "<pre>"; 
print_r($res);