How can I add a condition inside a php array?

前端 未结 8 941
小鲜肉
小鲜肉 2020-12-10 00:49

Here is the array

$anArray = array(
   \"theFirstItem\" => \"a first item\",
   if(True){
     \"conditionalItem\" => \"it may appear base on the condi         


        
相关标签:
8条回答
  • 2020-12-10 01:05

    Your can do it like this:

    $anArray = array(1 => 'first');
    if (true) $anArray['cond'] = 'true';
    $anArray['last'] = 'last';
    

    However, what you want is not possible.

    0 讨论(0)
  • 2020-12-10 01:05

    Its pretty simple. Create array with essential elements. Then add conditional elements to the array. Now add other elements if required.

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

    You modify this code to make it even more shorter,, i have just given elaborated code to make it self explantory. No NULL element, no empty string, put you item anywhere you want, no hassel.

    0 讨论(0)
  • 2020-12-10 01:08

    You can do it like this:

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

    unset the EMPTY array item if the condition is false

    unset($anArray['EMPTY']);
    
    0 讨论(0)
  • 2020-12-10 01:14

    Try this if you have associative array with different keys:

    $someArray = [
        "theFirstItem" => "a first item",
    ] + 
    $condition 
        ? [
            "conditionalItem" => "it may appear base on the condition"
          ] 
        : [ /* empty array if false */
    ] + 
    [
        "theLastItem" => "the last item",
    ];
    

    or this if array not associative

    $someArray = array_merge(
        [
            "a first item",
        ],
        $condition 
            ? [
                "it may appear base on the condition"
              ] 
            : [ /* empty array if false */
        ], 
        [
            "the last item",
        ]
    );
    
    0 讨论(0)
  • 2020-12-10 01:18

    You can assign all values and filter empty keys from the array at once like this:

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

    This allows you avoid the extra conditional after the fact, maintain key order, and imo it's fairly readable. The only caveat here is that if you have other falsy values (0, false, "", array()) they will also be removed. In that case you may wish to add a callback to explicitly check for NULL. In the following case theLastItem won't get unintentionally filtered:

    $anArray = array_filter([
        "theFirstItem" => "a first item",
        "conditionalItem" => $condition ? "it may appear base on the condition" : NULL,
        "theLastItem"  => false,
    ], function($v) { return $v !== NULL; });
    
    0 讨论(0)
  • 2020-12-10 01:22

    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.

    0 讨论(0)
提交回复
热议问题