Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array_diff problem

I'm confusing array_diff behavior

why genre don't exist on diff array? Do you know how to resolve the matter?

-code

<?php
$array1 = array
(
    'value01' => '0',
    'value02' => 'v2',
    'genre' => '1',
    'type' => 'text',
    'contry' => 'us',
    'data' => '1',
);
$array2 = array
(
    'value01' => 'v1',
    'value02' => 'v2',
    'genre' => '0',
    'type' => 'text',
    'contry' => 'canada',
    'data' => '1',
);

print_r(array_diff($array1,$array2));

My result:

Array
(
    [contry] => us
)

But I expect:

Array
(
    [value01] => 0,
    [genre] => 1,
    [contry] => us,
);
like image 242
freddiefujiwara Avatar asked Jan 20 '11 00:01

freddiefujiwara


People also ask

What is array_ diff?

The array_diff() function compares the values of two (or more) arrays, and returns the differences. This function compares the values of two (or more) arrays, and return an array that contains the entries from array1 that are not present in array2 or array3, etc.

How to match two array in PHP?

The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.

How do I check if two arrays have the same element in PHP?

Now, to check whether two arrays are equal or not, an iteration can be done over the arrays and check whether for each index the value associated with the index in both the arrays is the same or not. PHP has an inbuilt array operator( === ) to check the same but here the order of array elements is not important.


1 Answers

I believe you want to use array_diff_assoc

http://www.php.net/manual/en/function.array-diff-assoc.php

like image 116
scragz Avatar answered Oct 11 '22 19:10

scragz