如何从这个函数获得Variable $ idc和$ type?

时间:2015-10-06 07:38:00

标签: php

我创建了php函数来调用clear_url,但我想得到变量$ idc和$ type,但是我不能这样做,有人能告诉我该怎么做吗?

function clear_url($f1,$f2,$table){     
    include('db.php');  
    $sql=$Con->query("SELECT {$f1},{$f2} FROM {$table}");

    $array=array();
    while($rowt=mysqli_fetch_object($sql)){
        $name1 = trim($rowt->$f1);
        $name1 = strtolower($name1);
        $name1 = stripslashes($name1);
        $name1 = str_replace(" ","-",$name1);
        $name1 = str_replace(".","-",$name1);
        $name1 = str_replace("  ","-",$name1);
        $name1 = str_replace("(","",$name1);
        $name1 = str_replace(")","",$name1);
        $name1 = str_replace("/","-",$name1);           
        $name1 = str_replace(",","-",$name1);
        $name1 = str_replace("'","-",$name1);                       
        $name1 = str_replace("&","-",$name1);
        $name1 = str_replace("---","-",$name1); 
        $array[]=$name1.','.$rowt->$f2;
    }
    $leng=count($array);
    for($i=0;$i<$leng;$i++){        
        $array[$i]; 

        $str=$array[$i];
        $sp=explode(",",$str);
        if($sp[0]==$_GET['name']){
            $idc=$sp[1];
            $type=$sp[0];
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您只能从函数返回单个变量;因此,返回两个值的最简单方法是将其放入数组中。

//编辑 - 如果你选择在你没有确定你已经得到结果的时候返回;你需要一个有条件的实际检查。

if (isset($type) && isset($idc)) {
    return array(
        'idc' => $idc,
        'type' => $type
    );
} else {
    return false;
}

然后当你调用函数

$sample = clear_url($f1,$f2,$table);
$idc = $sample['idc'];
$type = $sample['type'];