php数组没有给出我期待的结果

时间:2012-07-20 08:55:37

标签: php mysql arrays array-difference

我真的陷入了困境,我尝试了很多不同的方法,但是我无法得到我需要的东西才能使这项工作成功。拜托,你能告诉我什么是错误的,以便让我能够正常工作。

在尝试构建属性网站时,上传了一个文件(.blm),我需要将AGENT_REF从这个文件中获取到一个数组中,这样我就可以与数据库进行比较并显示数组差异了...... blm文件包含信息AGENT_REF ^ ADDRESS_1 ^ ADDRESS_2 ^ POSTCODE1 ^ POSTCODE2 ...

我确信,为了获得我需要的结果,代理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>
    ';
}

我非常感谢你提供任何帮助,这真让我感到困惑。 非常感谢你的时间。

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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 */

} 
相关问题