不幸的是,这是不可能的。
如果有项目但是NULL值是可以的,请使用:
$anArray = array(
"theFirstItem" => "a first item",
"conditionalItem" => $condition ? "it may appear base on the condition" : NULL,
"theLastItem" => "the last item"
);
否则你必须这样做:
$anArray = array(
"theFirstItem" => "a first item",
"theLastItem" => "the last item"
);
if($condition) {
$anArray['conditionalItem'] = "it may appear base on the condition";
}
如果这个顺序很重要,那就更难了
$anArray = array("theFirstItem" => "a first item");
if($condition) {
$anArray['conditionalItem'] = "it may appear base on the condition";
}
$anArray['theLastItem'] = "the last item";
你可以使它变得更可读:
$anArray = array();
$anArray['theFirstItem'] = "a first item";
if($condition) {
$anArray['conditionalItem'] = "it may appear base on the condition";
}
$anArray['theLastItem'] = "the last item";
本文探讨了如何在编程中处理数组中的NULL值,给出了多种情况下的代码示例,并强调了保持代码可读性的最佳实践。通过实例展示了如何根据条件插入元素和调整数组顺序。
292

被折叠的 条评论
为什么被折叠?



