3

大家好,

这是我的代码:

if ($value==0)
{
        $array = array(
        "NAMES"            => array("John", "Sara", "Mark"), 
        "LASTNAMES"      => array ("Smith", "Lockwood", "Grant")
        );
}
else 
{
        $array = array(
        "NAMES"            => array("John", "Sara", "Mark"), 
        "LASTNAMES"      => array ("Smith", "Lockwood", "Grant"),
        "ITEMPURCHASED"      => array ("DVD", "Book", "Comic")
         );   
}

如您所见,我只是因为 ITEMPURCHASED 元素而重复整个数组代码。

有没有办法不重复整个数组代码?可以在仅添加 ITEMPURCHASED 元素的数组内使用 if 吗?如果是这样,怎么做?

万分感谢

4

8 回答 8

11
$array = array(
    "NAMES" => array("John", "Sara", "Mark"), 
    "LASTNAMES" => array ("Smith", "Lockwood", "Grant")
);

if ($value != 0){
    $array['ITEMPURCHASED'] = array ("DVD", "Book", "Comic");
}

或者,如果 $value 不等于 0,您可以使用三元运算符将值设置为 NULL 。

$array = array(
    "NAMES" => array("John", "Sara", "Mark"), 
    "LASTNAMES" => array ("Smith", "Lockwood", "Grant"),
    "ITEMPURCHASED" => ($value != 0) ? array ("DVD", "Book", "Comic") : null
);
于 2013-07-19T08:55:23.070 回答
1
    $array = array(
    "NAMES"            => array("John", "Sara", "Mark"), 
    "LASTNAMES"      => array ("Smith", "Lockwood", "Grant")
    );
    if($value != 0) {
        $array["ITEMPURCHASED"] = array ("DVD", "Book", "Comic");
    }
于 2013-07-19T08:55:56.873 回答
1

像这样试试

$array = array(
              "NAMES"        => array("John", "Sara", "Mark"), 
              "LASTNAMES"    => array ("Smith", "Lockwood", "Grant")
         );
if($val != 0) {
    $array['ITEMPURCHASED'] = => array ("DVD", "Book", "Comic");
}

只需将公共数组置于循环之外并检查条件,如果为真,则将剩余的基于 ITEMPURCHASED 的数组附加到主数组。

于 2013-07-19T08:56:17.973 回答
1

是的,您可以通过以下代码做到这一点:

$array = $array = array(
        "NAMES"            => array("John", "Sara", "Mark"), 
        "LASTNAMES"      => array ("Smith", "Lockwood", "Grant")
      );
if ($value != 0)
        $array["ITEMPURCHASED"] = array ("DVD", "Book", "Comic");
}

或者你可以使用array_mergePHP 的函数。那么代码将是:

if ($value != 0)
           $arrar1 = array("ITEMPURCHASED" => array ("DVD", "Book", "Comic"));
            $array = array_merge($array,$array1);
 }
于 2013-07-19T08:56:22.207 回答
0

首先,您创建数组,如 if 的第一部分(在 if 之前复制该代码)

然后使用:

if ($value != 0) {
  $array['ITEMPURCHASED'] = ...;
}
于 2013-07-19T08:57:35.530 回答
0

你可以这样做

$array = array(
   "NAMES" => array("John", "Sara", "Mark"),
   "LASTNAMES" => array ("Smith", "Lockwood", "Grant")
);

if ($value!=0)
{
    $array["ITEMPURCHASED"] = array ("DVD", "Book", "Comic");
}
于 2013-07-19T08:57:38.193 回答
0
$array = array(
   "NAMES" => array("John", "Sara", "Mark"),
   "LASTNAMES" => array ("Smith", "Lockwood", "Grant")
);

if ($value!=0)
{
    $array["ITEMPURCHASED"] = array ("DVD", "Book", "Comic");
}
于 2013-07-19T08:56:34.260 回答
0

不,如果在数组内,您不能使用。您可以根据数组构造函数中的某些条件使用三元运算符来更改值,但是由于您根本不想设置该项目,因此也不是一个选项。你可以这样做:

$array = array(
  "NAMES"          => array("John", "Sara", "Mark"), 
  "LASTNAMES"      => array ("Smith", "Lockwood", "Grant")
);

if ($value ! =0)
  $array['ITEMPURCHASED'] = array ("DVD", "Book", "Comic");
于 2013-07-19T08:56:38.927 回答