CloudSeph
CloudSeph

Reputation: 883

Check if key exist in a multidimensional array

With the array below, I want to check if the key [year] exist in the array. As you can see Array 1 and 2 has no [year] while the rest have it. How can I check if in array exist [year] or not? Basically if it exist, run something else run something else.

Array
        (
            [0] => Array
                (
                    [filmId] => 61359
                    [url] => http://www.moviemeter.nl/film/61359
                    [title] => Unstoppable
                    [alternative_title] => 
                    [year] => 2011
                    [thumbnail] => http://www.moviemeter.nl/images/covers/thumbs/61000/61359.jpg
                    [average] => 0
                    [votes_count] => 0
                    [similarity] => 100.00
                    [directors_text] => geregisseerd door Richard Harrison
                    [actors_text] => met Chen Shilony, Ruben Crow en David Powell
                    [genres_text] => Drama / Komedie
                    [duration] => 90
                )
            [1] => Array
                (
                    [filmId] => 87923
                    [url] => http://www.moviemeter.nl/film/87923
                    [title] => Unstoppable
                    [alternative_title] => 

                    [thumbnail] => http://www.moviemeter.nl/images/covers/thumbs/87000/87923.jpg
                    [average] => 0
                    [votes_count] => 0
                    [similarity] => 100.00
                    [directors_text] => geregisseerd door Example Director
                    [actors_text] => met Actor 1, Actor 2 en Actor 3
                    [genres_text] => Drama / Komedie
                    [duration] => 90
                )
            [2] => Array
                (
                    [filmId] => 68593
                    [url] => http://www.moviemeter.nl/film/68593
                    [title] => Unstoppable
                    [alternative_title] => 

                    [thumbnail] => http://www.moviemeter.nl/images/covers/thumbs/68000/68593.jpg
                    [average] => 3.3
                    [votes_count] => 191
                    [similarity] => 100.00
                    [directors_text] => geregisseerd door Tony Scott
                    [actors_text] => met Denzel Washington, Chris Pine en Rosario Dawson
                    [genres_text] => Actie / Thriller
                    [duration] => 98
                )
            [3] => Array
                (
                    [filmId] => 17931
                    [url] => http://www.moviemeter.nl/film/17931
                    [title] => Unstoppable
                    [alternative_title] => Nine Lives
                    [year] => 2004
                    [thumbnail] => http://www.moviemeter.nl/images/covers/thumbs/17000/17931.jpg
                    [average] => 2.64
                    [votes_count] => 237
                    [similarity] => 100.00
                    [directors_text] => geregisseerd door David Carson
                    [actors_text] => met Wesley Snipes, Jacqueline Obradors en Mark Sheppard
                    [genres_text] => Actie / Thriller
                    [duration] => 96
                )
        )

Upvotes: 0

Views: 6650

Answers (3)

AbraCadaver
AbraCadaver

Reputation: 78994

Attempt to extract the year column and if it results in a non-empty array then there is a year somewhere in the array:

if(array_column($array, 'year')) {
    //yes year exists  :-)
} else {
    //no doesn't exist :-(
}

If you're wanting to check each array in the array and do something for each one, then just loop and check for year:

foreach($array as $values) {
    if(isset($values['year'])) {
        //do something with $values
    } else {
        //do something else
    }
}

Upvotes: 4

Gaurav
Gaurav

Reputation: 721

By this code you can get offset of array with their value:

<?php

$my_array = array(    
        0 =>  array(  
            "filmId"   => "61359",  
            "year"    =>  "2011"
        ),  
        1   =>  array(  
            "filmId" =>  "87923",  
        ), 
        2   =>  array(  
            "filmId" =>  "68593",  
            "year" => "2011"  
        )
);
$column_name = 'year';
print_r(array_map(function($element) use($column_name){return isset($element[$column_name]) ? $element[$column_name] : "";}, $my_array));

Output

Array
(
    [0] => 2011
    [1] => 
    [2] => 2011
)

After that you check which array don't have year column.

Upvotes: 1

Yulio Aleman Jimenez
Yulio Aleman Jimenez

Reputation: 1677

You can use isset() function:

isset($custom_array[0]['year']);

====================================================

for($i = 0; $i < count($custom_array); $i++){ 
    if(isset($custom_array[$i]['year'])){ 
        echo "Position [" . $i . "]-[year] exist."; 
    } else { 
        echo "Position [" . $i . "]-[year] not exist."; 
    }
}

Upvotes: 0

Related Questions