2010-01-24 55 views
1

我需要編寫功能,搜索&替換陣列鍵在多維陣列中按以下方式替換項:搜索和使用PHP和CakePHP集類

輸入數組:

[0] => Array 
    (
     [Foo] => sometext 
     [Foo2] => Array 
      (
       [lorem] => 1 
       [Avatar2] => Array 
      (
       [meta_key] => avatar2 
       [meta_value] => Array 
        (
         [small] => 65606449bb5b6d10c2dfce9ecb88fdbd-24-small.jpeg 
         [medium] => 65606449bb5b6d10c2dfce9ecb88fdbd-45-medium.jpeg 
         [large] => 65606449bb5b6d10c2dfce9ecb88fdbd-75-large.jpeg 
         [source] => 65606449bb5b6d10c2dfce9ecb88fdbd.jpg 
        ) 

      ) 
      ) 
     [Avatar] => Array 
      (
       [meta_key] => avatar 
       [meta_value] => Array 
        (
         [small] => 65606449bb5b6d10c2dfce9ecb88fdbd-24-small.jpeg 
         [medium] => 65606449bb5b6d10c2dfce9ecb88fdbd-45-medium.jpeg 
         [large] => 65606449bb5b6d10c2dfce9ecb88fdbd-75-large.jpeg 
         [source] => 65606449bb5b6d10c2dfce9ecb88fdbd.jpg 
        ) 

      ) 
    ) 

[1] => Array 
    (

     [Wallpaper] => Array 
      (
       [meta_key] => wallpaper 
       [meta_value] => Array 
        (
         [small] => 65606449bb5b6d10c2dfce9ecb88fdbd-24-small.jpeg 
         [medium] => 65606449bb5b6d10c2dfce9ecb88fdbd-45-medium.jpeg 
         [large] => 65606449bb5b6d10c2dfce9ecb88fdbd-75-large.jpeg 
         [source] => 65606449bb5b6d10c2dfce9ecb88fdbd.jpg 
        ) 

      ) 
    ) 

輸出數組應該像這:

 [0] => Array 
    (
     [Foo] => sometext 
     [Foo2] => Array 
      (
       [lorem] => 1 
       [Avatar2] => Array (
         [small] => 65606449bb5b6d10c2dfce9ecb88fdbd-24-small.jpeg 
         [medium] => 65606449bb5b6d10c2dfce9ecb88fdbd-45-medium.jpeg 
         [large] => 65606449bb5b6d10c2dfce9ecb88fdbd-75-large.jpeg 
         [source] => 65606449bb5b6d10c2dfce9ecb88fdbd.jpg 
        ) 

      ) 
     [Avatar] => Array 
      (
       [small] => 65606449bb5b6d10c2dfce9ecb88fdbd-24-small.jpeg 
       [medium] => 65606449bb5b6d10c2dfce9ecb88fdbd-45-medium.jpeg 
       [large] => 65606449bb5b6d10c2dfce9ecb88fdbd-75-large.jpeg 
       [source] => 65606449bb5b6d10c2dfce9ecb88fdbd.jpg 
      ) 
    ) 

[1] => Array 
    (

     [Wallpaper] => Array 
      (

       [small] => 65606449bb5b6d10c2dfce9ecb88fdbd-24-small.jpeg 
       [medium] => 65606449bb5b6d10c2dfce9ecb88fdbd-45-medium.jpeg 
       [large] => 65606449bb5b6d10c2dfce9ecb88fdbd-75-large.jpeg 
       [source] => 65606449bb5b6d10c2dfce9ecb88fdbd.jpg 


      ) 
    ) 

如你所見,我需要重新格式化meta_key/meta_value部分。任何建議(原始PHP或使用CakePHP集類),歡迎:)

回答

1

你defenetly需要使用遞歸的:

function replace_inner_meta_keys($arr) { 
    if (array_key_exists('meta_value', $arr)) { 
     // return meta value instead of original array 
     return $arr['meta_value']; 
    } 
    else { 
     foreach ($arr as $key => $value) { 
      if (is_array($value)) { 
       // travel through array recursivly 
       $arr[$key] = replace_inner_meta_keys($value); 
      } 
     } 
     return $arr; 
    } 
} 
+0

哇!我印象深刻 - 這就是訣竅!大thx! – grzes 2010-01-24 16:44:34

+1

順便說一句,如果它有幫助,你可以接受答案 – 2010-01-24 19:26:39