2015-10-06 222 views
0

我做了這個功能:比較一個字符串數組PHP

<?php 
function check() { 
    if ($GLOBALS['a'] == $GLOBALS['tools']) { 

    echo "HETZELFDE! <br>"; 
    var_dump($GLOBALS['a']); 
    echo "<br>"; 
    var_dump($GLOBALS['tools']); 

} 
else { 

    $GLOBALS['tools']; 
    echo "NIET HETZELFDE <br>"; 

    $GLOBALS['a'] = strtoupper(array2string($GLOBALS['a'])); 
    var_dump($GLOBALS['a'] ); 
    echo "<br>"; 
    //array2string($GLOBALS['a']); 
    var_dump($GLOBALS['tools'] ); 

} 
} 

check() ?> 

結果是:

NIET HETZELFDE 
string(63) "[0] => RED [1] => BLUE [2] => BLACK [3] => ORANGE [4] => GREEN " 
string(58) "[RED] => [BLUE] => [BLACK] => [ORANGE] => [GREEN] => " 

我需要的是知道如果$ a == $工具(如果是與工具一樣)

我該如何正確地做到這一點?

回答

1

我想你可以使用array_diff來知道數組是否有任何區別。和array_diff()的

<?php 
$array1 = array("a" => "green", "red", "blue", "red"); 
$array2 = array("b" => "green", "yellow", "red"); 
$result = array_diff($array1, $array2); 

print_r($result); 
?> 

其結果是: 陣列 ( 並[b] =>棕色 並[c] =>藍色 [0] =>紅色 )

如果要比較數組索引,可以使用array_diff_assoc()

<?php 
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); 
$array2 = array("a" => "green", "yellow", "red"); 
$resultado = array_diff_assoc($array1, $array2); 
print_r($resultado); 
?> 

結果

陣列 ( 並[b] =>棕色 並[c] =>藍色 [0] =>紅色 )