skip to Main Content

How can i put "if" inside array in php?
mean:

 return [
'cars' => [
   'test' => $arrayTest,
   'test1' => $arrayTest1,
],
'session' => [
   'sessionkey' => "$xyz",
   'lastlogintime' => $sess,
   if ($xyzp->available > 0){
     'availablenow' => true;
} else{
   'wait' => '7_days_waiting';
   ]
];

problem is propably in using "=>"

Thanks

3

Answers


  1. You can’t add control structures inside of an array assignment. You’d have to use an array for the partial result, and then add the keys conditionally afterwards.

    $res = [
    'cars' => [
       'test' => $arrayTest,
       'test1' => $arrayTest1,
    ],
    'session' => [
       'sessionkey' => "$xyz",
       'lastlogintime' => $sess,
    ]
    ];
    
    if ($xyzp->available > 0){
       $res['availablenow'] => true;
    } else{
       $res['wait'] => '7_days_waiting';
    }
    
    return $res;
    

    The other suggestions in the comments about using a ternary would work, but not necessarily cleanly since you’re setting different keys based on the conditional. Using the ternary both keys would exist in the resulting array, but the "other" key would be set to NULL or another "not set" value of your choosing. Eg:

     return [
    'cars' => [
       'test' => $arrayTest,
       'test1' => $arrayTest1,
    ],
    'session' => [
       'sessionkey' => "$xyz",
       'lastlogintime' => $sess,
       'availablenow' => ($xyzp->available > 0) ? true : false,
       'wait' => ($xyzp->available <= 0) ? '7_days_waiting' : NULL
    ]
    ];
    
    Login or Signup to reply.
  2. Put the array in a variable, then use an if statement to add the conditional keys and values.

    $result = [
        'cars' => [
            'test' => $arrayTest,
            'test1' => $arrayTest1,
        ],
        'session' => [
            'sessionkey' => "$xyz",
            'lastlogintime' => $sess,
        ]
    ];
    
    if ($xyzp->available > 0){
         $result['session']['availablenow'] = true;
    } else {
       $result['session']['wait'] = '7_days_waiting';
    }
    return $result;
    

    If the key were constant and you had both an if and else value for it, you could have used a ternary in the value of that key.

    Login or Signup to reply.
  3. Use the ternary operator – as suggested in the other answers for the value – also for the key:

    $array = [
      'cars'    => [
        'test'  => $arrayTest,
        'test1' => $arrayTest1,
      ],
      'session' => [
        'sessionkey'                                   => "$xyz",
        'lastlogintime'                                => $sess,
        $xyzp->available > 0 ? 'availablenow' : 'wait' => $xyzp->available > 0 ? true : '7_days_waiting'
      ]
    ];
    

    Or:

    $isAvailable = $xyzp->available > 0;
    
    $array = [
      'cars'    => [
        'test'  => $arrayTest,
        'test1' => $arrayTest1,
      ],
      'session' => [
        'sessionkey'                           => "$xyz",
        'lastlogintime'                        => $sess,
        $isAvailable ? 'availablenow' : 'wait' => $isAvailable ? true : '7_days_waiting'
      ]
    ];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search