检查多维数组中是否存在键

时间:2017-04-11 02:47:41

标签: php arrays

使用下面的数组,我想检查数组中是否存在键[year]。正如您所看到的,阵列1和2没有[year],而其余的则没有[year]。如何检查数组是否存在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 ) ) ?基本上如果它存在,运行其他东西运行别的东西。

Order

3 个答案:

答案 0 :(得分:1)

尝试提取year列,如果它导致非空数组,则数组中某处有year

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

如果您想要检查数组中的每个数组并为每个数组执行某些操作,那么只需循环并检查year

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

答案 1 :(得分:0)

您可以使用isset()功能:

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."; 
    }
}

答案 2 :(得分:0)

通过此代码,您可以获得数组的偏移值:

<?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));

<强>输出

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

之后,检查哪个数组没有年份列。