检查是否设置了数组值

Check if an array value is set or not

我正在从 table

中获取 practice_string_id 和 program_string_id
$project_type = DB::table('project')
              ->where('code',$asset_request->project_code)
              ->select('practice_string_id','program_string_id')
              ->first();
print_r($project_type); //output 

输出:

stdClass Object ( [practice_string_id] => PRACTICE0028 [program_string_id] => )

我想检查 $project_type->程序是否设置在 if 条件下

if(isset($project_type->program_string_id)){
    //nothing in $project_type->program but reached here now 
}

我想知道如何检查 php 中是否设置了值,如果 condition.now if(isset($project_type->program_string_id)) 通过,如果条件是 working.I 想跳过条件.

由于这是一个数据库查询,而您 select 字段 program_string_id,它将始终随时设置。问题是,是否有任何价值。所以你可能想使用 empty 作为检查:

if (!empty($project_type->program_string_id)) {
    // ...
}
if(isset($project_type-> program_string_id) && !empty($project_type-> program_string_id)  ) 
}

你必须同时使用 isset()empty() 否则在某些情况下它可能会抛出错误

$project_type = DB::table('project')
              ->where('code',$asset_request->project_code)
              ->select('practice_string_id','program_string_id')
              ->first();
if(count($project_type) > 0 && $project_type != ""){

    echo $project_type->program_string_id;
}