2012-07-20 58 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

相关问题