James James - 6 months ago 9
PHP Question

PHP Array conditions not working

Still learning PHP etc... im getting an error on the following:

'payment_status' => ( 'P' == $order_info['status'] || 'C' == $order_info['status'] || 'O' == $order_info['status'] ) ? 'paid' : 'paid' : 'unpaid',


which is from part of my array but is giving an error at that line...

First attempt at this kind of condition within an array so was likely would of broke but in case dont know what i was aiming for...


if order_info status = P to set paid

if order_info status = C to set paid

if order_info status = O to set unpaid


Any ideas?

Answer

The problem is that you have too many : 'paid's.

Change the end to this:

? 'paid' : 'unpaid'

You could also make this a bit more readable:

$paid_statuses = array('P', 'C'); // O is unpaid
...
'payment_status' => (in_array($order_info['status'], $paid_statuses) ? 'paid' : 'unpaid')`