Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 列出除关联数组中的一个键以外的所有键_Php - Fatal编程技术网

Php 列出除关联数组中的一个键以外的所有键

Php 列出除关联数组中的一个键以外的所有键,php,Php,这应该很简单,但我没有得到预期的结果。浏览器要么列出数组中的所有元素(当我包含“!”操作符时),要么不列出任何元素(当我不包含“!”操作符时)。我只是试着列出除了一个元素以外的所有元素,或者只列出那个元素。我两条路都不能上班 $features = array( 'winter' => 'Beautiful arrangements for any occasion.', 'spring' => 'It must be spring! Delicate daf

这应该很简单,但我没有得到预期的结果。浏览器要么列出数组中的所有元素(当我包含“!”操作符时),要么不列出任何元素(当我不包含“!”操作符时)。我只是试着列出除了一个元素以外的所有元素,或者只列出那个元素。我两条路都不能上班

    $features = array(
    'winter' => 'Beautiful arrangements for any occasion.',
    'spring' => 'It must be spring! Delicate daffodils are here.',
    'summer' => "It's summer, and we're in the pink.",
    'autumn' => "Summer's over, but our flowers are still a riot of colors."
    );

    <h1>Labeling Array Elements</h1>
    <?php 
    foreach ($features as $feature) {
    if(array_key_exists('autumn', $features)) { 
    continue;
   } 
   echo "<p>$feature</p>";
   }    
   ?>
$features=array(
“冬季”=>“任何场合的完美安排”,
“春天”=>“一定是春天了!娇嫩的水仙花在这里。”,
“夏天”=>“现在是夏天,我们很开心。”,
“秋天”=>“夏天结束了,但我们的花仍然五彩缤纷。”
);
标记数组元素

当您在循环中执行
continue
时,仅仅因为它存在于数组中,它在第一次迭代时停止。这总是正确的

相反,您需要这样做:

foreach ($features as $season => $description) {
    if ($season == 'autumn') {
        continue;
    }
    echo $description;
}   

对于这种方法,您也可以使用数组过滤器:

$features = array(
    'winter' => 'Beautiful arrangements for any occasion.',
    'autumn' => "Summer's over, but our flowers are still a riot of colors.",
    'spring' => 'It must be spring! Delicate daffodils are here.',
    'summer' => "It's summer, and we're in the pink.",
);

print_r(array_filter($features, function ($key) {
    return $key != 'autumn';
}, ARRAY_FILTER_USE_KEY));

现场演示:

但逻辑可用于索引数组。我的小提琴:main.xfiddle.com/de5f2f17/index\u array\u conditional.php。