PHP循环遍历页面ID和位置

时间:2015-11-03 15:37:49

标签: php loops

我正在尝试构建一个循环,以根据页面ID显示不同的位置名称。我正在尝试for循环,但不知道如何获得2个结果,我尝试了foreach,但仍然迷路了。

$location = array(  "Frisco" => "1507",  "McKinney" => "1509");
$count = count($location);
for($i = 0; $i < $count; $i++){
    if(is_page($location[$i])) {
        $location = "";
    }
}

Need results = 
    // Add Shortcode
    function dynamicLocation(){
    if(is_page('1')){
        $location = "McKinney";
    }
    else if(is_page()){
        $location....
    }
}

1 个答案:

答案 0 :(得分:2)

忘记for,请使用foreach

foreach ($location as $key=>$value){
    //here $key is the location and $value is the ID
    //e.g. $key="Frisco", $value="1507"
}