1

How to check if a single variable or single value is empty is array in a array or two dimensional array

Like this array

$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"
    )
);

Or an array retrieved from database

To this, i want to check if $value['course_name'] == "" without putting alot of codes

 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'];
    }
2
  • 1
    So, what's the problem? Just use your condition within if statement. Commented Aug 29, 2016 at 9:05
  • it would be so repetative Commented Aug 29, 2016 at 9:08

4 Answers 4

2

My proposition is:

Define mapping key to title row. For example:

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

Next step: change code responsible for out data:

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

Thats all

Sign up to request clarification or add additional context in comments.

1 Comment

Alright thanks This is it.and this will be mine foreach ($resFirst as $key => $value) { foreach($value as $key => $value2){ if($value2==""){ $value[$key]="Field Not Set"; } } }
0
if (!empty($value['course_name']))
{
   // do somthing
}
else
{
   //do something
}

1 Comment

thanks for the answer. this is very useful for single dimension array. need to be improved for two dimensional
0

You can use empty to check if a variable is empty, like this:

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

Or, as Object Manipulator pointed out, just use if ($value['course_name'] == '') { }.

1 Comment

thanks for the answer. this is very useful for single dimension array. need to be improved for two dimensional
0

If you are PHP 5.5+, you can use array_column to get all course_name in an array.

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

then,

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

Reference:

UPDATE:

I think I found a similar solution from one of the comments in PHP Docs(its very similar to my answer though, just less code):

in (PHP 5 >= 5.5.0) you don't have to write your own function to search through a multi dimensional array

ex :

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

simply u can use this

$key = array_search(40489, array_column($userdb, 'uid'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.