[英]How can I add a condition inside a php array?
這是數組
$anArray = array(
"theFirstItem" => "a first item",
if(True){
"conditionalItem" => "it may appear base on the condition",
}
"theLastItem" => "the last item"
);
但是我得到了 PHP Parse 錯誤,為什么我可以在數組中添加一個條件,發生了什么??:
PHP Parse error: syntax error, unexpected T_IF, expecting ')'
不幸的是,這根本不可能。
如果有該項目但值為 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";
如果您正在制作一個純粹的關聯數組,並且鍵的順序無關緊要,您始終可以使用三元運算符語法有條件地命名鍵。
$anArray = array(
"theFirstItem" => "a first item",
(true ? "conditionalItem" : "") => (true ? "it may appear base on the condition" : ""),
"theLastItem" => "the last item"
);
這樣,如果滿足條件,則密鑰與數據一起存在。 如果沒有,它只是一個帶有空字符串值的空白鍵。 但是,鑒於已經有很多其他答案,可能會有更好的選擇來滿足您的需求。 這並不完全干凈,但是如果您正在處理具有大型數組的項目,它可能比打破數組然后添加更容易; 特別是如果數組是多維的。
你可以這樣做:
$anArray = array(1 => 'first');
if (true) $anArray['cond'] = 'true';
$anArray['last'] = 'last';
然而,你想要的卻是不可能的。
如果你有不同鍵的關聯數組,試試這個:
$someArray = [
"theFirstItem" => "a first item",
] +
$condition
? [
"conditionalItem" => "it may appear base on the condition"
]
: [ /* empty array if false */
] +
[
"theLastItem" => "the last item",
];
或者如果數組不關聯
$someArray = array_merge(
[
"a first item",
],
$condition
? [
"it may appear base on the condition"
]
: [ /* empty array if false */
],
[
"the last item",
]
);
沒有任何魔法可以幫助這里。 你能做的最好的是:
$anArray = array("theFirstItem" => "a first item");
if (true) {
$anArray["conditionalItem"] = "it may appear base on the condition";
}
$anArray["theLastItem"] = "the last item";
如果您不特別關心項目的順序,它會變得更容易忍受:
$anArray = array(
"theFirstItem" => "a first item",
"theLastItem" => "the last item"
);
if (true) {
$anArray["conditionalItem"] = "it may appear base on the condition";
}
或者,如果訂單確實很重要並且條件項不止一對,您可以這樣做,這可能被認為更具可讀性:
$anArray = array(
"theFirstItem" => "a first item",
"conditionalItem" => "it may appear base on the condition",
"theLastItem" => "the last item",
);
if (!true) {
unset($anArray["conditionalItem"]);
}
// Unset any other conditional items here
您可以像這樣一次分配所有值並從數組中過濾空鍵:
$anArray = array_filter([
"theFirstItem" => "a first item",
"conditionalItem" => $condition ? "it may appear base on the condition" : NULL,
"theLastItem" => "the last item"
]);
這允許您在事后避免額外的條件,維護密鑰順序,並且它具有相當的可讀性。 這里唯一需要注意的是,如果您有其他虛假值( 0, false, "", array()
),它們也將被刪除。 在這種情況下,您可能希望添加一個回調來顯式檢查NULL
。 在以下情況下, theLastItem
不會被無意過濾:
$anArray = array_filter([
"theFirstItem" => "a first item",
"conditionalItem" => $condition ? "it may appear base on the condition" : NULL,
"theLastItem" => false,
], function($v) { return $v !== NULL; });
你可以這樣做:
$anArray = array(
"theFirstItem" => "a first item",
(true ? "conditionalItem" : "EMPTY") => (true ? "it may appear base on the condition" : "EMPTY"),
"theLastItem" => "the last item"
);
如果條件為假,則取消設置 EMPTY 數組項
unset($anArray['EMPTY']);
它很簡單。 創建具有基本元素的數組。 然后將條件元素添加到數組中。 如果需要,現在添加其他元素。
$anArray = array(
"theFirstItem" => "a first item"
);
if(True){
$anArray+=array("conditionalItem" => "it may appear base on the condition");
}
$more=array(
"theLastItem" => "the last item"
);
$anArray+=$more;
您修改此代碼以使其更短,我剛剛給出了詳細的代碼以使其自我解釋。 沒有 NULL 元素,沒有空字符串,把你的項目放在你想要的任何地方,沒有麻煩。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.