0

I'm trying to create an array to convert JSON. My data is queried from database. My problem is I have to check condition for array. If $item->verified == 1, my 'isVerified' will be true, my email will be in verified and opposite.

Here is what I did, I check condition and create 2 array for it. Can I just use 1 array for condition:

if( ($item->verified) == 1)
{
    $data[] = [
        'name'       => $item->fullname,
        'address'    => $item->address,
        'isVerified' => true,
        'email'      => [
            'verified'   => $item->email,
            'unverified' => []
        ]
    ];
}
else
{
    $data[] = [
        'name'       => $item->fullname,
        'address'    => $item->address,
        'isVerified' => false,
        'email'      => [
            'verified'   => [],
            'unverified' => $item->email
        ]
    ];
}

1 Answer 1

2

You can use ternary operator.

$data[] = [
    'name'       => $item->fullname,
    'address'    => $item->address,
    'isVerified' => $item->verified == 1,
    'email'      => [
        'verified'   => $item->verified == 1 ? $item->email : [],
        'unverified' => $item->verified == 0 ? $item->email : [],
    ]
];
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.