检查二维数组中的值是否为空

时间:2016-08-29 09:03:30

标签: php arrays

如何检查单个变量或单个值是否为空 是数组或二维数组

喜欢这个数组

$resFirst=array(
    array(
        "course_name"   =>"a",
        "duration"      =>"b",
        "eligibility"   =>"",
        "recognition"   =>"d",
        "affiliation"   =>"c",
        "certificate"   =>"asd",
        "category"      =>"asd",
        "type"          =>"xcv",
        "school_batch"  =>"zv"
    ),
    array(
        "course_name"   =>"a",
        "duration"      =>"b",
        "eligibility"   =>"",
        "recognition"   =>"d",
        "affiliation"   =>"c",
        "certificate"   =>"asd",
        "category"      =>"asd",
        "type"          =>"xcv",
        "school_batch"  =>"zv"
    )
);

或从数据库中检索的数组

对此,我想检查一下$value['course_name'] == "" 没有多少代码

 foreach ($resFirst as $key => $value) {
        echo"
        <h6> name:</h6><p>".        $value['course_name']."</p>
        <h6>Duration:</h6>".        $value['duration']."
        <h6> eligiblity:</h6><p>".  $value['eligibility']."</p>
        <h6>recognition:</h6><p>".  $value['recognition']."</p>
        <h6>Affiliation:</h6>".     $value['affiliation']."
        <h6> Certification:</h6>".  $value['certificate']."
        <h6>Category:</h6>".        $value['category']."     
        <h6>Type:</h6>".            $value['type']."
        <h6>Category:</h6>".        $value['school_batch'];
    }

4 个答案:

答案 0 :(得分:0)

您可以使用empty检查变量是否为空,如下所示:

if (empty($value['course_name'])) {
    // Variable's empty!
} else {
    // Variable's not empty...
}

或者,正如Object Manipulator指出的那样,只需使用if ($value['course_name'] == '') { }

答案 1 :(得分:0)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    _cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

答案 2 :(得分:0)

我的主张是:

将映射键定义到标题行。例如:

$mapKeyToTitle = array(
        'course_name' => 'name',
        'duration' => 'Duration',
        ...
    );

下一步:更改负责输出数据的代码:

foreach ($resFirst as $row) {
         foreach ($row as $key => $column) {
             if (!empty($mapKeyToTitle[$key] && !empty($column)) {
                 echo "<h6> " . $mapKeyToTitle[$key] . ":</h6><p>". $column . "</p>";
             }
         }
     }

多数人

答案 3 :(得分:0)

如果您是PHP 5.5+,则可以使用array_column获取数组中的所有sudo su tomcat

course_name

然后,

$resFirst=array(
    array(
        "course_name"   =>"a",
        "duration"      =>"b",
        "eligibility"   =>"",
        "recognition"   =>"d",
        "affiliation"   =>"c",
        "certificate"   =>"asd",
        "category"      =>"asd",
        "type"          =>"xcv",
        "school_batch"  =>"zv"
    ),
    array(
        "course_name"   =>"a",
        "duration"      =>"b",
        "eligibility"   =>"",
        "recognition"   =>"d",
        "affiliation"   =>"c",
        "certificate"   =>"asd",
        "category"      =>"asd",
        "type"          =>"xcv",
        "school_batch"  =>"zv"
    )
);

$course_name = array_column($resFirst, 'course_name');

参考:

<强>更新

我认为我在PHP Docs中找到了comments之一的类似解决方案(它与我的答案非常相似,只是代码更少):

  

in(PHP 5&gt; = 5.5.0)您不必编写自己的函数   搜索多维数组

     

前:

// if array has any empty value(means any one 'course_name' is empty)
if(array_search("", $course_name) !== false){
    // your code
}
     

简单地说你可以使用这个

$userdb=Array
(
    (0) => Array
        (
            (uid) => '100',
            (name) => 'Sandra Shush',
            (url) => 'urlof100'
        ),

    (1) => Array
        (
            (uid) => '5465',
            (name) => 'Stefanie Mcmohn',
            (pic_square) => 'urlof100'
        ),

    (2) => Array
        (
            (uid) => '40489',
            (name) => 'Michael',
            (pic_square) => 'urlof40489'
        )
);