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

时间:2016-07-21 20:24:06

标签: php arrays conditional

我需要在以下数组中包含一个条件语句,称为$ 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'
];

}

我需要将条件语句移到顶部,这样才能在表单上显示第一个内容。但是,当我将它移到顶部时它会消失。如何将条件检查集成到表单顶部而不是当前显示的底部?

1 个答案:

答案 0 :(得分:1)

$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'
];