2012-07-20 57 views
0

我真的被這個困住了,我嘗試了很多不同的方式,我無法得到我需要的東西來完成這項工作。請,你能告訴我什麼是錯誤的,爲了讓我得到這個工作。php陣列沒有給出我期待的結果

在試圖建立一個屬性網站上傳一個文件(.blm)時,我需要從這個文件中獲取AGENT_REF到一個數組中,以便我可以與數據庫進行比較並顯示數組差異... .blm文件包含信息AGENT_REF^ADDRESS_1^ADDRESS_2^POSTCODE1^POSTCODE2 ...

我確信它是AGENT REF,它無法正常工作以獲得我需要的結果。

請幫我解決這個問題。

<?php 
$rmdata = $rmparser->getPropertyData(); 

$properties = array(); 

foreach ($rmdata as $properties) { 
$fields = array(); 
$values = array(); 
$blmarArray = array(); 

    foreach ($properties as $field=>$value) { 
     if (!$value) continue; 
     $blmarArray = $values[0]; 
     $agentref = $values[0]; 
     $fields[] = $field;  
     $values[] = "'".$value."'";  
    } 

    $sql_archeck = mysql_query("SELECT `AGENT_REF` FROM `eprentals`"); 
    $sqlarArray = array(); 
    while($row = mysql_fetch_array($sql_archeck)) { 
     $sqlarArray[] = $row['AGENT_REF']; 
    } 

    $combyArrayDiff = array_diff($sqlarArray, $blmarArray); 

    echo ' 
    <div style="background-color:#ffd7d7;border:#bcbcbc solid 1px;padding:6px;margin- bottom:6px;"> 
    <span style="padding:2px;"><p><b>SQL list:</b> ' . implode(', ', $sqlarArray) . '</p></span> 
    <p><b>Uploaded list:</b> ' . implode(', ', $blmarArray) . '</p> 
    <p><b>Diff list:</b> ' . implode(', ', $combyArrayDiff) . '</p> 
    </div> 
    '; 
} 

我非常感謝任何幫助,這真的讓我感到莫名其妙。 非常感謝你的時間。

+0

當你的var_dump($行)您能得到什麼? – 2012-07-20 08:59:44

+1

'$ rmdata'看起來像什麼,請介意添加該值的var_dump($ rmdata)或var_export($ rmdata) – DonSeba 2012-07-20 08:59:50

+0

您應該在提問之前重新研究您的代碼。 看看第二條foreach指令: foreach($ properties as $ field => $ value){ if(!$ value)continue; $ blmar = $ values [0]; } 爲什麼你會檢索$字段中的密鑰,然後不使用它們? $ blmar是作爲一個數組創建的,但是在那個循環中,您不斷重新定義它的內容。 也許你想做點像$ blmar [$ field] = $ value – Salepate 2012-07-20 08:59:50

回答

1

我想這是你想要做什麼:

/* create 2 empty arrays */ 
$rm_agentrefs = array(); 
$db_agentrefs = array(); 

/* fetch rmdata */ 
$rmdata = $rmparser->getPropertyData(); 

/* foreach rmdata */ 
foreach($rmdata as $current_row) 
{ 
    /* store the Agent Ref in the rm-array */ 
    $rm_agentrefs[] = $current_row['AGENT_REF']; 
} 


/* define a database query for fetching agent ref from database */ 
$db_query = "SELECT `AGENT_REF` FROM `eprentals`"; 

/* run the database query */ 
$db_resource = mysql_query($db_query); 

/* fetch each line from the resource */ 
while($current_row = mysql_fetch_array($db_resource)) 
{ 
    /* store each agent ref in the db-array */ 
    $db_agentrefs[] = $current_row['AGENT_REF']; 
} 

/* compare db and rm arrays (missing = db - rm) */ 
$missing_agentrefs = array_diff($db_agentrefs, $rm_agentrefs); 
+0

YES!謝謝!所以非常多Puggen,如果你看看鏈接,它顯示出需要的結果,雖然更多,但它有需要顯示的結果非常感謝你,我非常感謝你的幫助和幫助。 – Steve 2012-07-20 10:02:08

+0

所有排序的Puggen,再次請讓我感謝您的時間和協助。我非常感謝你的幫助!謝謝! – Steve 2012-07-20 10:10:55

0

這是什麼現在你的代碼劑量:

/* load rmdata */ 
$rmdata = $rmparser->getPropertyData(); 

/* make $properties an empty array */ 
$properties = array(); 

/* foreach rmdata, replace $properties with the current rmdata */ 
foreach ($rmdata as $properties) 
{ 
    /* replace 3 variables with empty arrays */ 
    $fields = array(); 
    $values = array(); 
    $blmarArray = array(); 

    /* for each property */ 
    foreach ($properties as $field=>$value) 
    { 
     /* if the propery don't have a value */ 
     if (!$value) 
      /* skip this property */ 
      continue; 

     /* if the propery have a value */ 
     /* replace $blmarArray and $agentref with the first row in $values, always empty for first property */ 
     $blmarArray = $values[0]; 
     $agentref = $values[0]; 

     /* add current field to the fields array */ 
     $fields[] = $field;  

     /* add current value (suronded by ') to the values array */ 
     $values[] = "'".$value."'";  
    } 

    /* if there was at least 2 properties with values */ 
    /* $blmarArray and $agentref have the value of the first of them */ 

} 
+0

Hi Puggen,I已經更新,現在顯示大部分代碼:) – Steve 2012-07-20 09:09:46

+0

取代了我的意見,以匹配您的更新代碼 – 2012-07-20 09:19:28

+0

嗨Puggen,感謝您的更新...正如我所看到的$ blmarArray應該有每個屬性的AGENT_REF的值。但我無法獲得數組,所以我可以做一個數組差異..從MySQL數組結果適當的領域和這就是爲什麼我認爲$ blmarArray數組沒有返回我需要的結果blm文件。 – Steve 2012-07-20 09:34:17

相關問題