I am wondering how I can conditionally either have an array element or not at all using the following style. I realise in a general sense to do this would be trivial... So What I would really like is to be able to not have a particular element at all based on some condition, so far I can make it one value or another.
The actual motivation behind my question is to conditionally show or not show an [ActionColumn][1] in Yii2. But am more curious in a general sense now.
Thanks!
<?php
$middle_name = "James";
$full_name = [
'Robert',
(!empty($middle_name)) ? $middle_name : 'NA',
'Fischer',
];
?>
ifafter that will include this information if a condition is met, or modify$middle_nameto'NA'beforehand.null. The inline condition has to return something, it cannot result as void. You can instead make a condition to define the array as 2 or 3 elements depending on the content of$middle_name, or you can simply insert$middle_namethere and then usearray_filter()to remove empty elements from the array.