Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a condition inside a php array?

Tags:

arrays

syntax

php

Here is the array

$anArray = array(
   "theFirstItem" => "a first item",
   if(True){
     "conditionalItem" => "it may appear base on the condition",
   }
   "theLastItem"  => "the last item"

);

But I get the PHP Parse error, why I can add a condition inside the array, what's happen??:

PHP Parse error:  syntax error, unexpected T_IF, expecting ')'
like image 461
Tattat Avatar asked Apr 17 '11 13:04

Tattat


People also ask

How do you add condition to an array?

If you are making a purely associative array, and order of keys does not matter, you can always conditionally name the key using the ternary operator syntax. $anArray = array( "theFirstItem" => "a first item", (true ?

How do you write an IF condition inside an array?

For example, if condition «a» is an array of Booleans (true or false values), it returns an array with the same index, containing «b» or «c» as appropriate: Variable X := -2 .. 2. If X > 0 THEN 'Positive' ELSE IF X < 0 THEN 'Negative' ELSE 'Zero' →

Can you add to an array in PHP?

The array_push() function inserts one or more elements to the end of an array. Tip: You can add one value, or as many as you like. Note: Even if your array has string keys, your added elements will always have numeric keys (See example below).

Is inside array PHP?

The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.


2 Answers

Unfortunately that's not possible at all.

If having the item but with a NULL value is ok, use this:

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

Otherwise you have to do it like that:

$anArray = array(    "theFirstItem" => "a first item",    "theLastItem"  => "the last item" );  if($condition) {    $anArray['conditionalItem'] = "it may appear base on the condition"; } 

If the order matters, it'll be even uglier:

$anArray = array("theFirstItem" => "a first item"); if($condition) {    $anArray['conditionalItem'] = "it may appear base on the condition"; } $anArray['theLastItem'] = "the last item"; 

You could make this a little bit more readable though:

$anArray = array(); $anArray['theFirstItem'] = "a first item"; if($condition) {    $anArray['conditionalItem'] = "it may appear base on the condition"; } $anArray['theLastItem'] = "the last item"; 
like image 175
ThiefMaster Avatar answered Oct 17 '22 07:10

ThiefMaster


If you are making a purely associative array, and order of keys does not matter, you can always conditionally name the key using the ternary operator syntax.

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

This way, if the condition is met, the key exists with the data. If not, it's just a blank key with an empty string value. However, given the great list of other answers already, there may be a better option to fit your needs. This isn't exactly clean, but if you're working on a project that has large arrays it may be easier than breaking out of the array and then adding afterwards; especially if the array is multidimensional.

like image 34
Adam Kelso Avatar answered Oct 17 '22 08:10

Adam Kelso