为什么多维数组总是返回键“0”?

时间:2021-12-08 21:21:31

I have an array like this

我有一个这样的数组

$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/>";
    }
}

and I want the result is like this only

我希望结果是这样的。

sumber has f1, f2,

sumber f1,f2,

but the result is like this

结果是这样的

sumber has f1, f2,

sumber f1,f2,

0 has f5, f6,

0的f5、f6

please help me how to display the key "sumber" only??? thanks.

请帮助我如何显示关键的“sumber”???谢谢。

4 个解决方案

#1


3  

Use triple conditional === in the if($key=="sumber") condition:

在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/>";
    }
}

Otherwise the if() condition is also accomplished when key is empty/0.

否则,当键为空/0时,if()条件也将完成。

#2


2  

you can simply used this code :

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

$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']);
}

#3


1  

Have a look at the PHP Manual on comparing values: http://www.php.net...comparison.php

看看PHP手册关于比较价值:http://www.php.net.on.php

   <?php
   var_dump(0 == "a"); // 0 == 0 -> true
   var_dump("1" == "01"); // 1 == 1 -> true
   var_dump(0 === "01"); // false <- what you need
   ?>

#4


0  

Just refer to that specific element to start with?

只是要从这个特定的元素开始?

<?php
    if (!empty($test["sumber"])) {
        foreach ($test["sumber"] as $values) {
            echo "sumber has " . implode(", ", $values) . "<br />";
        }
    }
?>

#1


3  

Use triple conditional === in the if($key=="sumber") condition:

在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/>";
    }
}

Otherwise the if() condition is also accomplished when key is empty/0.

否则,当键为空/0时,if()条件也将完成。

#2


2  

you can simply used this code :

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

$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']);
}

#3


1  

Have a look at the PHP Manual on comparing values: http://www.php.net...comparison.php

看看PHP手册关于比较价值:http://www.php.net.on.php

   <?php
   var_dump(0 == "a"); // 0 == 0 -> true
   var_dump("1" == "01"); // 1 == 1 -> true
   var_dump(0 === "01"); // false <- what you need
   ?>

#4


0  

Just refer to that specific element to start with?

只是要从这个特定的元素开始?

<?php
    if (!empty($test["sumber"])) {
        foreach ($test["sumber"] as $values) {
            echo "sumber has " . implode(", ", $values) . "<br />";
        }
    }
?>