Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an Multi-Dimensional Array as follows

Array
(
    [0] => Array
        (
            [featured] => Yes
            [job_code] => WT3
            [title] => Senior Test Engineer - dB Testing
            [no_of_pos] => 5
            [experience] => 3 to 5 years
            [job_desc] => Work with Dev counterparts to understand the implementation details.
Adhere to all quality standards from QA perspective.
Able to independently come up with test cases based on the analysis of functionality.
Test planning and Test Design (Creation of Test scenarios/Test Cases)
Preparation of Test environment
Execution of Test cases
Identification, reporting and tracking of defects
            [skillsets] => Should be experienced in testing on Mac OS X
Should be familiar with terms such as plist, starutp options on Mac, OS versions
Should be familiar with Terminal and Console on Mac OS X
Should be familiar with Apple Script Editor or Automator
Should be able to troubleshoot basic boot issues on Mac
            [keywords] => Black Box testing, Web Testing, e-Commerce, SQL queries/database testing
        )

    [1] => Array
        (
            [featured] => No
            [job_code] => MNX-7A
            [title] => Test Engineer - Mac OS X
            [no_of_pos] => 1
            [experience] => 3 to 5 years
            [job_desc] => Should be excellent in Black Box Test Case Design
Should be able to understand business requirements and develop relevant scenarios and test cases 
Good knowledge on the SDLC / STLC
Preparation of Test environment
Execution of Test cases
Identification, reporting and tracking of defects
            [skillsets] => Should be experienced in testing on Mac OS X
Should be familiar with terms such as plist, starutp options on Mac, OS versions
Should be familiar with Terminal and Console on Mac OS X
Should be familiar with Apple Script Editor or Automator
Should be able to troubleshoot basic boot issues on Mac
            [keywords] => Black Box Testing, Mac OS X
        )

)

I want to iterate thru the array and make sure all the fields have a value and not empty.

Which is the best way to do it ?

share|improve this question
2  
!empty($arr) method –  Pramod May 7 '13 at 5:33
    
does that check all keys and values ? –  Harsha M V May 7 '13 at 5:38
add comment

3 Answers

up vote 1 down vote accepted

Please try this:

<?php

$data=array
(
    0 => array
        (
            'featured' => 'Yes',
            'job_code' => 'WT3',
            'title' => 'Senior Test Engineer - dB Testing',
            'no_of_pos' => '5',
            'experience' => '3 to 5 years',
            'job_desc' => 'Work with Dev counterparts to understand the implementation details.
Adhere to all quality standards from QA perspective.
Able to independently come up with test cases based on the analysis of functionality.
Test planning and Test Design (Creation of Test scenarios/Test Cases)
Preparation of Test environment
Execution of Test cases
Identification, reporting and tracking of defects
            [skillsets] => Should be experienced in testing on Mac OS X
Should be familiar with terms such as plist, starutp options on Mac, OS versions
Should be familiar with Terminal and Console on Mac OS X
Should be familiar with Apple Script Editor or Automator
Should be able to troubleshoot basic boot issues on Mac',
            'keywords' => 'Black Box testing, Web Testing, e-Commerce, SQL queries/database testing'
        ),

    1 => array
        (
            'featured' => 'No',
            'job_code' => 'MNX-7A',
            'title' => 'Test Engineer - Mac OS X',
            'no_of_pos' => '1',
            'experience' => '3 to 5 years',
            'job_desc' => 'Should be excellent in Black Box Test Case Design
Should be able to understand business requirements and develop relevant scenarios and test cases 
Good knowledge on the SDLC / STLC
Preparation of Test environment
Execution of Test cases
Identification, reporting and tracking of defects',
            'skillsets' => 'Should be experienced in testing on Mac OS X
Should be familiar with terms such as plist, starutp options on Mac, OS versions
Should be familiar with Terminal and Console on Mac OS X
Should be familiar with Apple Script Editor or Automator
Should be able to troubleshoot basic boot issues on Mac',
            'keywords' => 'Black Box Testing, Mac OS X'
        )

);

function anyEmpty($array) {
    foreach ($array as $arr) {
        if (!is_array($arr)) {
            if (empty($arr)) {
                return true;
            }
        } else {
            if (anyEmpty($arr)) {
                return true;
            }
        }
    }
    return false;
}

var_dump(anyEmpty($data));

?>

In this example, all fields are set, so it will echo bool(false). However, if I change one of the fields to '' it will return true.

Example:

<?php

$data=array
(
    0=>array(
        0=>"hello",
        1=>array(
            0=>"world"
        ),
        2=>array(
            0=>"my name",
            array(
                0=>""
            )
        )
    )
);

function anyEmpty($array) {
    foreach ($array as $arr) {
        if (!is_array($arr)) {
            if (empty($arr)) {
                return true;
            }
        } else {
            if (anyEmpty($arr)) {
                return true;
            }
        }
    }
    return false;
}

var_dump(anyEmpty($data));

?>

This now returns bool(true).

share|improve this answer
add comment

I think that a simple loop is always mush faster.

function yourFunction($array, $key, $val) {
    foreach ($array as $item)
        if (isset($item[$key])
          // do what you intend to here
}
share|improve this answer
add comment

You can use below one. It referenced to php.net here

function is_multiArrayEmpty($multiarray) { 
        if(is_array($multiarray) and !empty($multiarray)){ 
            $tmp = array_shift($multiarray); 
                if(!is_multiArrayEmpty($multiarray) or !is_multiArrayEmpty($tmp)){ 
                    return false; 
                } 
                return true; 
        } 
        if(empty($multiarray)){ 
            return true; 
        } 
        return false; 
    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.