2012-05-24 118 views
0

是否有可能使if($array1[0]=>somevalue == $array2[0]=>somevalue){echo true;}else{echo false;}?因爲我不能讓它工作,請你幫我,如果數組相等數組PHP

例如: $ array1([0] =>'喬',[2] =>'對'); $ array2([0] =>'info'=> array([0] =>'joe'));

foreach($array2->info as $info){ 
    foreach($array1 as $name){ 
     if($name == $info[0]){ 
      echo 'true'; 
      } 
      else{ 
      echo 'false'; 
      } 
     } 
    } 

而這纔是真正的代碼,

的陣列:

Array 
(
[0] => SimpleXMLElement Object 
    (
     [companyLocationInfo] => Array 
      (
       [0] => SimpleXMLElement Object 
        (
         [companyName] => AVIS 
         [name] => NYCC07 
         [line1] => 420 EAST 90TH STREET 
        ) 
       [2] => SimpleXMLElement Object 
        (
         [companyName] => AVIS 
         [name] => NYCC06 
         [line1] => 310 EAST 64TH STREET 
        ) 
       [3] => SimpleXMLElement Object 
        (
         [companyName] => AVIS 
         [name] => NYCC01 
         [line1] => 68 EAST 11TH STREET 
        ) 

      ) 

     [rates] => Array 
      (
       [0] => SimpleXMLElement Object 
        (
         [pickupDropoffLocations] => Array 
          (
           [0] => SimpleXMLElement Object 
            (
             [companyName] => AVIS 
             [name] => NYCC07 
            ) 
          ) 
         [vehicleRentalPrefType] => CCAR 
         [rateAmount] => 83.99 
         [rateCurrency] => USD 
        ) 
       [2] => SimpleXMLElement Object 
        (
         [pickupDropoffLocations] => Array 
          (
           [0] => SimpleXMLElement Object 
            (
             [companyName] => AVIS 
             [name] => NYCC06 
            ) 
          ) 
         [vehicleRentalPrefType] => CCAR 
         [rateAmount] => 110.54 
         [rateCurrency] => USD 
        ) 
       [3] => SimpleXMLElement Object 
        (
         [pickupDropoffLocations] => Array 
          (
           [0] => SimpleXMLElement Object 
            (
             [companyName] => AVIS 
             [name] => NYCC01 
            ) 
          ) 
         [vehicleRentalPrefType] => CCAR 
         [rateAmount] => 210.65 
         [rateCurrency] => USD 
        ) 

      ) 

    ) 

,這是代碼:

$results_array = array(); 

foreach($result[0]->rates as $rate) { 
    foreach($result[0]->companyLocationInfo as $info) { 
     if($info->name == $rate->pickupDropoffLocations[0]->name) { 
      $results_array[] = array(
       'line1' => $info->line1, 
       'name' => $info->locationDetails->name, 
       'companyName' => $info->companyName, 
       'vehicleRentalPrefType' => $rate->vehicleRentalPrefType 
      ); 
     } 
    } 
} 
print_r($results_array); 

謝謝。

+0

這個問題與XML或SOAP有什麼關係? – Flukey

+0

因爲我的代碼是用SimpleXML – thegrede

回答

2

你想要做的事情應該是可能的。如果上面的代碼是您實際嘗試運行的內容,那麼您的第一個問題是,您正在比較"Joe""joe"的值,這些值不相同。

如果您希望區分大小寫的比較,請使用if (strcasecmp($var1, $var2) == 0)(如果兩個字符串相同,則不返回大小寫,則返回零)。

+0

Yups,這對我來說非常合適,謝謝 – thegrede