2016-10-04 52 views
2

array_diff()功能無法顯示正確的結果: -和array_diff()函數不表示正確的結果

第一陣列: -

 Array(
    [designation_id] => 1 
    [name] => Y 
    [fathers_name] => Z 
    [mothers_name] => F 
    [spouse_name] => d 
    [gender] => F 
    [last_achieved_degree] => 2 
    [date_of_birth] => 1960-10-17 
    [date_of_joining] => 1987-02-04 
) 

第二陣列: -

Array 
    (
     [designation_id] => 9 
     [name] => M 
     [fathers_name] => N 
     [mothers_name] => O 
     [spouse_name] => 
     [gender] => M 
     [last_achieved_degree] => 1 
     [date_of_birth] => 1967-11-17 
     [date_of_joining] => 2016-01-01 
    ) 

輸出:

Array 
(
    [name] => Y 
    [fathers_name] => Z 
    [mothers_name] => F 
    [spouse_name] => d 
    [gender] => F 
    [last_achieved_degree] => 2 
    [date_of_birth] => 1960-10-17 
    [date_of_joining] => 1987-02-04 
) 

desi輸出結果中沒有顯示gnation id列。 如果指定ID值爲1,則此索引不會在輸出中顯示,否則顯示。它是錯誤還是別的?

+0

爲什麼不顯示?分享你的原始腳本。 –

+0

你顯示的輸入是兩個不同的數組? –

+0

那麼應該輸出什麼正確的?也許'array_diff'不是你真正需要的。你試驗的代碼在哪裏? – Ghost

回答

2

由於您的數組是關聯數組,所以你需要使用array_diff_assoc: -

<?php 

$a = Array(
    'designation_id' =>1, 
    'name' => 'Y', 
    'fathers_name' => 'Z', 
    'mothers_name' => 'F', 
    'spouse_name' => 'd', 
    'gender' => 'F', 
    'last_achieved_degree' => 2, 
    'date_of_birth' => '1960-10-17', 
    'date_of_joining' => '1987-02-04' 
); 

$b =Array 
(
    'designation_id' => 9, 
    'name' => 'M', 
    'fathers_name' => 'N', 
    'mothers_name' => 'O', 
    'spouse_name' => '', 
    'gender' => 'M', 
    'last_achieved_degree' => 1, 
    'date_of_birth' => '1967-11-17', 
    'date_of_joining' => '2016-01-01', 
); 

echo "<pre/>";print_r(array_diff_assoc($a,$b)); 

輸出: - https://eval.in/654658

,檢查更多的描述和實例: - http://sg2.php.net/manual/en/function.array-diff-assoc.php

爲什麼array_diff()不工作: - https://stackoverflow.com/a/4742438/4248328

+0

謝謝。有用。但問題是爲什麼當array_dif()的值爲1時不顯示指定id索引,否則顯示正確。 – Feroz

+0

@Feroz我也很驚訝,但我期待着它。並將在這裏更新時,我會得到一個答案。謝謝標記雖然。:) –

+0

@Feroz我已經添加了鏈接爲什麼'array_diff()'不working.check,如果不明白你讓我知道。 –

1

array_diff計算不區分鍵的差異。在第二個數組中,您有[last_achieved_degree] => 1刪除[designation_id] => 1
嘗試array_diff_assoc

+0

正確的catch。 +1 –