0

I have 2 identical PHP arrays. There is only one value different. I want to find this value:

var_dump(array_diff(array(
  "a" => "1",
  "b" => "SomeString",
  "c" => 1, // <- different value, same key 
  "d" => "4521",
  "e" => "7546654241",
  "f" => "78",
  "g" => "99.999",
  "h" => "34",
  "i" => "http://google.com/"
), array(
  "a" => "1",
  "b" => "SomeString",
  "c" => "0", // <- different value, same key 
  "d" => "4521",
  "e" => "7546654241",
  "f" => "78",
  "g" => "99.999",
  "h" => "34",
  "i" => "http://google.com/"
)));

The result is array(0) { } but there should be new "c" value but isn't. When I remove all others values:

var_dump(array_diff(array(
  "c" => 1
), array(
  "c" => "0"
)));

I get what I want array(1) { ["c"]=> int(1) }.

I don't understand it. Why is it so?

1

2 Answers 2

4

array_diff() matches values from the first array to the second

returns the values in array1 that are not present in any of the other arrays.

The value 1 for key c in the first array does exist in the second array as the value for key a, so there is no difference there that array_diff() will recognise

Perhaps using array_diff_assoc() will gve you the result that you're actually trying to get.... http://ideone.com/xHCVfF

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

2 Comments

Do you mean array_diff_assoc()? Good explanation of why OP is not getting what they expect.
@Mike - I was just typing array_diff_assoc into ideone.com :)
2

i tried with http://php.net/manual/en/function.array-diff-assoc.php and i get

array (size=1)
  'c' => int 1

as for using array_diff it compares values...

Comments

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.