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 ')'
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 ?
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' →
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).
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.
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";
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With