如何在php数组中添加条件?

时间:2011-04-17 13:22:00

标签: php arrays syntax

这是数组

$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 ')'

8 个答案:

答案 0 :(得分:38)

不幸的是,根本不可能。

如果项目但具有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";

答案 1 :(得分:4)

你可以这样做:

$anArray = array(1 => 'first');
if (true) $anArray['cond'] = 'true';
$anArray['last'] = 'last';

然而,你想要的是不可能的。

答案 2 :(得分:4)

如果要创建纯关联数组,并且键的顺序无关紧要,则可以始终使用三元运算符语法有条件地命名键。

$anArray = array(
    "theFirstItem" => "a first item",
    (true ? "conditionalItem" : "") => (true ? "it may appear base on the condition" : ""),
    "theLastItem" => "the last item"
);

这样,如果满足条件,则密钥与数据一起存在。如果没有,它只是一个空字符串值的空键。但是,鉴于已有其他答案的清单,可能有更好的选择来满足您的需求。这不是很干净,但是如果你正在处理一个拥有大型数组的项目,那么它可能比打破数组然后再添加更容易;特别是如果数组是多维的。

答案 3 :(得分:1)

这里没有任何魔力可以提供帮助。你能做的最好的就是:

$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

答案 4 :(得分:1)

如果你有一个具有不同键的关联数组,请尝试这个:

$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",
    ]
);

答案 5 :(得分:0)

您可以像这样分配所有值并从阵列中过滤空键:

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

答案 6 :(得分:0)

您可以这样做:

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

答案 7 :(得分:0)

非常简单。用基本元素创建数组。然后将条件元素添加到数组。现在,根据需要添加其他元素。

$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元素,没有空字符串,可以将您放置在所需的任何位置,没有麻烦。