2013-02-08 123 views
1

我對PHP數組和我想要做的一些比較有點麻煩。對不起,因爲我的英語不好:(比較所有鍵的多個陣列

我正在建立一個圖片管理系統,用戶可以一次選擇多張圖片,並編輯所有選定圖片的信息,但有些字段已經填入 - 如果一個特定的字段被多個圖片的不同信息填充,那麼就想向用戶顯示'*'字符。

我的問題是以下內容: - 我有一個二維數組,包含所有mySQL查詢結果選擇的圖片(這個數字當然是不確定的) 下面是一個結果數組的例子:

Array 
(

[0] => Array 
    (
     [Folder_id] => 40 
     [Title_fr] => test 
     [Title_en] => 
     [Desc_fr] => 
     [Desc_en] => 
     [Place_fr] => Koksijde, Belgique 
     [Place_en] => Koksijde, Belgium 
     [Place_ICAO] => EBBE 
     [Place_city] => 
     [Place_country] => be 
    ) 

[1] => Array 
    (
     [Folder_id] => 40 
     [Title_fr] => test 
     [Title_en] => 
     [Desc_fr] => 
     [Desc_en] => 
     [Place_fr] => Koksijde, Belgique 
     [Place_en] => Koksijde, Belgium 
     [Place_ICAO] => EBBE 
     [Place_city] => 
     [Place_country] => be 
    ) 

[2] => Array 
    (
     [Folder_id] => 40 
     [Title_fr] => test 
     [Title_en] => 
     [Desc_fr] => 
     [Desc_en] => 
     [Place_fr] => Koksijde, Belgique 
     [Place_en] => Koksijde, Belgium 
     [Place_ICAO] => EBBE 
     [Place_city] => 
     [Place_country] => be 
    ) 

[3] => Array 
    (
     [Folder_id] => 51 
     [Title_fr] => test 
     [Title_en] => 
     [Desc_fr] => 
     [Desc_en] => 
     [Place_fr] => FRANC 
     [Place_en] => ANGLE 
     [Place_ICAO] => ENGL 
     [Place_city] => Münich2 
     [Place_country] => de 
    ) 

正如您所看到的,一些字段對於所有記錄可能是通用的,有些字段可能不同。例如,[Place_fr]對於所有記錄都不相同,但是[Desc_fr]是。

我想編寫一個函數,返回一個數組,其中所有字段都作爲關鍵字,如果此字段包含記錄之間的不同值,並且值爲'*',並且值爲所有記錄共有的值。

例如,這裏的結果將是:

Array 
( 
     [Folder_id] => * 
     [Title_fr] => test 
     [Title_en] => 
     [Desc_fr] => 
     [Desc_en] => 
     [Place_fr] => * 
     [Place_en] => * 
     [Place_ICAO] => * 
     [Place_city] => * 
     [Place_country] => * 
) 

我知道這可能和array_diff_assoc使用來完成()。使用兩個數組很容易。問題是我必須傳遞無限數量的參數給這個函數,我不知道如何解決這個問題。

對不起noob問題 - 我只是一個beginer當談到陣列。

感謝您的幫助!

回答

1

這應做到:

// get common keys => values 
$common = call_user_func_array('array_intersect_assoc', $arr); 

// and append missing keys to them, with "*" values 
$common += array_fill_keys(array_keys($arr[0]), '*'); 

(假設$arr高於你的陣列)

+0

我其實覺得你的答案更好。 +1 – Antony 2013-02-08 16:43:37

0
$arrays = array (
    array (
     "Folder_id" => 40, 
     "Title_fr" => "test", 
     "Title_en" => "", 
     "Desc_fr" => "", 
     "Desc_en" => "", 
     "Place_fr" => "Koksijde, Belgique", 
     "Place_en" => "Koksijde, Belgium", 
     "Place_ICAO" => "EBBE", 
     "Place_city" => "", 
     "Place_country" => "be" 
    ), 
    array (
     "Folder_id" => 40, 
     "Title_fr" => "test", 
     "Title_en" => "", 
     "Desc_fr" => "", 
     "Desc_en" => "", 
     "Place_fr" => "Koksijde, Belgique", 
     "Place_en" => "Koksijde, Belgium", 
     "Place_ICAO" => "EBBE", 
     "Place_city" => "", 
     "Place_country" => "be" 
    ), 
    array (
     "Folder_id" => 40, 
     "Title_fr" => "test", 
     "Title_en" => "", 
     "Desc_fr" => "", 
     "Desc_en" => "", 
     "Place_fr" => "Koksijde, Belgique", 
     "Place_en" => "Koksijde, Belgium", 
     "Place_ICAO" => "EBBE", 
     "Place_city" => "", 
     "Place_country" => "be" 
    ), 
    array (
     "Folder_id" => 51, 
     "Title_fr" => "test", 
     "Title_en" => "", 
     "Desc_fr" => "", 
     "Desc_en" => "", 
     "Place_fr" => "FRANC", 
     "Place_en" => "ANGLE", 
     "Place_ICAO" => "ENGL", 
     "Place_city" => "Münich2", 
     "Place_country" => "de" 
    ) 
); 

function compare($arrays) { 
    foreach ($arrays as $array) { 
     foreach ($array as $key => $value) { 
      if (!isset($result[$key])) { 
       $result[$key] = $value; 
      } else if ($result[$key] !== "*") { 
       if ($value !== $result[$key]) { 
        $result[$key] = "*"; 
       } 
      } 
     } 
    } 
    return $result; 
} 

echo "<pre>"; 
print_r(compare($arrays)); 
echo "</pre>"; 
+0

即點上我想要的東西來實現! 非常感謝您的幫助,非常感謝! – 2013-02-08 16:42:03

+0

@ClémentChristomanos你應該考慮一個詭計小馬的答案。 – Antony 2013-02-08 16:43:10