简体   繁体   中英

Compare the array keys from one array to another array

I'm wondering if there is a function in php that would able to compare the array keys from another array and return the arrays that doesn't exist on the other array for example:

$sample1 = array('val1', 'val2', 'val3', 'val4');
$sample2 = array('val1'=>'test','val4'=>'uhm...');

If I compare this two arrays I must able to identify that in sample2 variable the 2 associative array indexes val2 and val3 doesn't exist if when I try to base the results coming from sample1 variable.

Are there any functions in php that could do this?

There is a php function called array_diff_key:

<?php
$sample1 = array('val1'=>'char lang', 'val2'=>'wew ambot', 'val3'=>'aw', 'val4'=>'testing lang');
$sample2 = array('val1'=>'test','val4'=>'uhm...');
$difference = array_diff_key($sample1, $sample2);
//print_r($difference); would display val2, val3
?>

PHP manual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM