Php 而foreach循环输出两次

Php 而foreach循环输出两次,php,loops,foreach,Php,Loops,Foreach,我希望循环只输出一次。相反,它输出两次。代码如下: $results = mysql_query($query); while ($c = mysql_fetch_array($results)){ $individualPostcode = explode(",", $c['postcode']); foreach($individualPostcode as $val){ $val = trim($val); //Get ri

我希望循环只输出一次。相反,它输出两次。代码如下:

$results = mysql_query($query);
    while ($c = mysql_fetch_array($results)){
        $individualPostcode = explode(",", $c['postcode']);
        foreach($individualPostcode as $val){ 
            $val = trim($val); //Get rid of spaces
            if($val === $postcode){
                echo $c['url']."<br>";
            }
        }
    }
}
我已经尝试取出foreach循环,但我需要根据用户输入检查该数组

以下是$postcode的初始化:

$userInput = $_POST["input"];
if(strlen($userInput) < 4) 
    echo "User Input : ".$userInput."<br>";
else //Below gets the first three chars of the users string
    echo "User Input : $userInput<br>What is being used : ".mb_substr($userInput, 0, 3)."<br>";

$postcode = mb_substr($userInput, 0, 3);    
$userInput=$\u POST[“input”];
if(strlen($userInput)<4)
回显“用户输入:.”$userInput.“
”; 下面的else//获取users字符串的前三个字符 echo“用户输入:$userInput
正在使用的内容:”.mb_substr($userInput,0,3)。“
”; $postcode=mb_substr($userInput,0,3);
mysql\u fetch\u array
为每个返回的结果返回关联数组和索引数组。
foreach
循环将在两者上循环并输出两次。尝试使用
mysql\u fetch\u assoc()

更好的是,尝试移动到
mysqli
类。它速度更快,
mysql
被去除了污点


通过检查URL是否已放入数组,您始终可以创建一个URL数组,以防止它们重复:

$results = mysql_query($query);
$urlsArr = array();
while ($c = mysql_fetch_array($results)){
    $individualPostcode = explode(",", $c['postcode']);
    foreach($individualPostcode as $val){ 
        $val = trim($val); //Get rid of spaces
        if($val === $postcode){
            if (!in_array($c['url'], $urlsArr)) echo $c['url']."<br>";
            $urlsArr[] = $c['url'];
        }
    }
}
$results=mysql\u query($query);
$urlsArr=array();
而($c=mysql\u fetch\u数组($results)){
$individualPostcode=explode(“,”,$c['postcode']);
foreach($val的个人邮政编码){
$val=trim($val);//去掉空格
如果($val==$postcode){
如果(!in_数组($c['url'],$urlsArr))回显$c['url']。“
”; $urlsArr[]=$c['url']; } } }
mysql.*
语法是邪恶的!!
$postcode
的值是多少?若两条记录的邮政编码列表相同,则使用逗号分隔。它将重复第二个记录和第一个!嗯。。。你是说我写代码的方式吗?$postcode是一种格式化的用户输入,因此它的前三个字符(即E10 78P)将变为E10。我已尝试将mysql_fetch_数组更改为mysql_fetch assoc,但输出仍然相同。没问题:)很高兴它有所帮助
$results = mysql_query($query);
$urlsArr = array();
while ($c = mysql_fetch_array($results)){
    $individualPostcode = explode(",", $c['postcode']);
    foreach($individualPostcode as $val){ 
        $val = trim($val); //Get rid of spaces
        if($val === $postcode){
            if (!in_array($c['url'], $urlsArr)) echo $c['url']."<br>";
            $urlsArr[] = $c['url'];
        }
    }
}