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 array of arrays as follows (for example)

$array = (
    [0] => array (
        [a_key]     => 123456,
        [b_key]     => First Name,
        [c_key]     => Last Name,
        [this_key ] => Institution,
        ),
    [1] => array (
        [a_key]     => 123456,
        [b_key]     => First Name,
        [c_key]     => Last Name,
        [this_key ] => Institution,
        ),
    [2] => array (
        [a_key]     => 123456,
        [b_key]     => First Name,
        [c_key]     => Last Name,
        [this_key ] => Institution,
        ),
)

And I want to check if $array[$key]['this_key'] is not null or does not equal '' when

$key = (any number)

How do I set $key to match any number so that the following returns true when at least one second-dimension-array in the array has [this_key] set?

if( $array[$key]['this_key'] != '' )
    echo "true";
else 
    echo "false";

So, if $array[0][this_key] is not set, $array[1][this_key] IS set, and $array[2][this_key] is not set, the IF statement will return true because at least ONE of all of the arrays has [this_key] set.


Edit:

To give some context to the situation and provide a specific scenario to which this solution could be applied:

I have a list of people in a database that I need to display. For each person, I have a first and last name, and optionally an institution/degree/job title held by the individual. These need to display like this:

Without a title:
John Smith, Jane Doe, Fred Nerk

With a title:
John Smith, Penologist; Jane Doe, Professor; Fred Nerk, Astrophysicist

With mixed cases of a title:
John Smith; Jane Doe, Professor; Fred Nerk

Note the punctuation used in each case. If there is no optional information for ANY record, a comma separates the individuals. If there is optional information for ONE OR MORE records, a semicolon separates the individuals.

With the if statement, I would be able to determine if one of the individuals has optional information, even if every other individual does not.


By the way:

This PHP Sandbox Tool was incredibly useful for testing PHP code before integrating it into my file. It supports PHP versions from 4.4.9 up to 5.5.5.

share|improve this question
    
array_column() and array_filter() might be helpful. Else use a loop and a state variable. –  mario Aug 25 '14 at 15:21
    
Thanks, this looks like it will work, just need to update my version of PHP before I can use these. –  Nicky Aug 26 '14 at 16:10
    
There's a userland fallback for array_column in the manual, which becomes redundant when you upgrade, but can benefit code readability already now. –  mario Aug 26 '14 at 16:38
    
Thank you! I did not see that link. –  Nicky Aug 26 '14 at 19:03

2 Answers 2

You could do something like this:

foreach($array as $key=>$array){
    if(!empty($array['this_key'])){
        //found one, do something here           
    }
}
share|improve this answer
up vote 0 down vote accepted

For PHP >= 5.5.0, I used:

if(array_filter(array_column($array, 'this_key'))){
    // true, do something
}

When this returns true, I know that at least one array has information in this_key, and so I can do something unique for this case.

My server runs PHP 5.4.13, so I used the recommended userland implementation for PHP lower than 5.5. It was simple to implement, and it works as expected.

share|improve this answer

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.