user1080247
user1080247

Reputation: 1166

how to find empty value for specific key in multidimensional array

i have array like this

<?php
$array =
    array
    (
        array (
            0 => 1,
            1 => 'php',
            2 => 11,
            3 => 11,
            4 => 11,
            5 => 11,
            6 => 11,
        ),
        array (
            0 => 1,
            1 => 'php',
            2 => 11,
            3 => 11,
            4 => 11,
            5 => 11,
            6 => ,
        ),

    );

and i want to search in this multi-array to find if the key [6] => is empty.if it was empty in any array return false so how to do this

foreach($array as $item)
{
    foreach($item as $key=>$value)
    {
        print($key);
        if($key=="6" && $value==NULL)
        {
            echo "found";
            return false;
        }else{
            echo "not found";
            return true;
        }
    }
}

Upvotes: 0

Views: 833

Answers (3)

Alex
Alex

Reputation: 17289

$empty = false;
foreach($array as $item)
{
    if(empty($item[6]))
    {
            $empty=true;
            break;
    }
}
return $empty;

Upvotes: 1

AbraCadaver
AbraCadaver

Reputation: 78994

Here's an alternate for PHP >= 5.5.0 that checks for '', 0, null and false:

return !array_diff($six = array_column($array, 6), array_filter($six));

Upvotes: 0

Adam Solymos
Adam Solymos

Reputation: 99

Upvotes: 0

Related Questions