lleoun
lleoun

Reputation: 477

PHP: array with an if statement inside

Hi all,

Here's my code:

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

As you can see I'm reapeating the whole array code just because the ITEMPURCHASED element.

Is there a way not to repeat the whole array code? Can an if be used inside the array which adds just the ITEMPURCHASED element? If so, how?

Thanks a ton

Upvotes: 3

Views: 18389

Answers (8)

user399666
user399666

Reputation: 19879

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

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

Alternatively, you could set the value to NULL if $value is not equal to 0 by using a ternary operator.

$array = array(
    "NAMES" => array("John", "Sara", "Mark"), 
    "LASTNAMES" => array ("Smith", "Lockwood", "Grant"),
    "ITEMPURCHASED" => ($value != 0) ? array ("DVD", "Book", "Comic") : null
);

Upvotes: 12

Code Lღver
Code Lღver

Reputation: 15603

Yes, You can do that by following code:

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

Or you can use array_merge function of PHP. then code will be:

if ($value != 0)
           $arrar1 = array("ITEMPURCHASED" => array ("DVD", "Book", "Comic"));
            $array = array_merge($array,$array1);
 }

Upvotes: 1

som
som

Reputation: 4656

You can do like this

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

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

Upvotes: 0

kms
kms

Reputation: 389

First you create the array, as in the first part of the if (copy that code before the if)

Then use:

if ($value != 0) {
  $array['ITEMPURCHASED'] = ...;
}

Upvotes: 0

Patrick Kostjens
Patrick Kostjens

Reputation: 5105

No you cannot use if inside the array. You could use ternary operators to alter the value depending on some conditions inside the array constructor, but since you don't want to set the item at all that won't be an option either. You can do it like this though:

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

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

Upvotes: 0

maximkou
maximkou

Reputation: 5332

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

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

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

Try like this

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

Just put the common array out of the loop and check the condition and if true then append the remaining ITEMPURCHASED based array to the main array.

Upvotes: 2

RiaD
RiaD

Reputation: 47620

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

Upvotes: 1

Related Questions