0

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?

2
  • 1
    This post should answer your question stackoverflow.com/questions/2917243/… Commented Dec 7, 2011 at 0:08
  • Thanks for the link the array_key_exists really works for me. :) Commented Dec 7, 2011 at 0:16

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks this is really a great function it compares associative keys for 2 associative arrays and returns the difference in array datatype but I updated the post I just made a mistake the other one is actually a one dimensional array the array_key_exists did worked for me. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.