2

我有一个这样的数组

$test = array("sumber"=>array("f1","f2"),"ukraina"=>array("f3","f4"),"0"=>array("f5","f6"));

foreach($test as $key => $value){
    if($key=="sumber"){
    $a='';
    for($i=0;$i<count($value);$i++){
        $a.=$value[$i].", ";
    }
    echo $key." has ".$a."<br/>";
    }
}

我想要的结果只是这样

数有 f1, f2,

但结果是这样的

数有 f1, f2,

0 有 f5, f6,

请帮助我如何仅显示“数字”键???谢谢。

4

4 回答 4

3

===在条件中使用三重if($key=="sumber")条件:

$test = array("sumber"=>array("f1","f2"),"ukraina"=>array("f3","f4"),"0"=>array("f5","f6"));

foreach($test as $key => $value){
    if($key==="sumber"){
    $a='';
    for($i=0;$i<count($value);$i++){
        $a.=$value[$i].", ";
    }
    echo $key." has ".$a."<br/>";
    }
}

否则当为空/0if()时条件也成立。key

于 2013-10-07T08:13:27.907 回答
2

您可以简单地使用此代码:

$test = array("sumber"=>array("f1","f2"),"ukraina"=>array("f3","f4"),"0"=>array("f5","f6"));

if(array_key_exists("sumber",$test) && !empty($test['sumber'])) {
    echo "sumber has".implode(",",$test['sumber']);
}
于 2013-10-07T08:20:37.173 回答
1

查看有关比较值的 PHP 手册: http://www.php.net...comparison.php

   <?php
   var_dump(0 == "a"); // 0 == 0 -> true
   var_dump("1" == "01"); // 1 == 1 -> true
   var_dump(0 === "01"); // false <- what you need
   ?>
于 2013-10-07T08:21:53.650 回答
0

只是参考那个特定的元素开始?

<?php
    if (!empty($test["sumber"])) {
        foreach ($test["sumber"] as $values) {
            echo "sumber has " . implode(", ", $values) . "<br />";
        }
    }
?>
于 2013-10-07T08:25:54.667 回答