将条件语句添加到字段数组中


Adding a conditional statement to an array of fields?

我需要在下面的名为$fields的数组中包含一个条件语句。

$fields = [
    'program_id'     => [
        'type'        => 'select',
        'label'       => 'Program',
        'opts'        => ["One", "Two", "Three"],
    ],
    'name'           => [
        'label' => 'Job Name'
    ],
    'start_date'         => [
        'class' => 'date-picker',
        'label' => 'Job Starts' . $req,
        'val'   => $job->start_date ? dateToPicker($job->start_date) : null
    ],
    'end_date'           => [
        'class' => 'date-picker',
        'label' => 'Job Ends' . $req,
        'val'   => $job->end_date ? dateToPicker($job->end_date) : null
    ],
    'section'            => [
        'type' => 'hidden',
        'val'  => 'details'
    ],
];
if (!$job->id && $program)
{
    $fields['job_copy'] = [
        'label'       => 'Copy Job From',
        'type'        => 'select',
        'textAsValue' => false,
        '_comment'    => 'Selecting a job here will copy all job information except the name.',
        'opts'        => array_replace([0 => '-- Select Job --'], $program->jobs()->lists('name', 'id')->all())
    ];
}
$fields[] = [
    'type'  => 'submit',
    'label' => "Save",
    'class' => 'btn btn-primary !important'
];

}

我需要将条件语句移到顶部,使其成为表单上显示的第一个内容。然而,当我将其移到顶部时,它就会消失。如何将条件检查集成到表单的顶部,而不是当前显示的底部?

$fields  = [];
if (!$job->id && $program)
{
    $fields['job_copy'] = [
        'label'       => 'Copy Job From',
        'type'        => 'select',
        'textAsValue' => false,
        '_comment'    => 'Selecting a job here will copy all job information except the name.',
        'opts'        => array_replace([0 => '-- Select Job --'], $program->jobs()->lists('name', 'id')->all())
    ];
}
$fields2 = [
    'program_id'     => [
        'type'        => 'select',
        'label'       => 'Program',
        'opts'        => ["One", "Two", "Three"],
    ],
    'name'           => [
        'label' => 'Job Name'
    ],
    'start_date'         => [
        'class' => 'date-picker',
        'label' => 'Job Starts' . $req,
        'val'   => $job->start_date ? dateToPicker($job->start_date) : null
    ],
    'end_date'           => [
        'class' => 'date-picker',
        'label' => 'Job Ends' . $req,
        'val'   => $job->end_date ? dateToPicker($job->end_date) : null
    ],
    'section'            => [
        'type' => 'hidden',
        'val'  => 'details'
    ],
];
$fields = array_merge($fields,$fields2);
$fields[] = [
    'type'  => 'submit',
    'label' => "Save",
    'class' => 'btn btn-primary !important'
];