Sunday, 15 May 2011

Multidimensional array comparsion in PHP with same keys on second level -



Multidimensional array comparsion in PHP with same keys on second level -

i have next 2 arrays:

$newarray = array( 0 => array( 'id' => 2, 'name' => 'color', 'valid' => 35, 'value' => 'black' ), 1 => array( 'id' => 14, 'name' => 'size', 'valid' => 233, 'value' => 'm' ), 2 => array( 'id' => 15, 'name' => 'width', 'valid' => 200, 'value' => '123 cm' ) ); $oldarray = array( 0 => array( 'name' => 'size', 'value' => 'm' ) 1 => array( 'name' => 'color', 'value' => 'black' ), );

what want comparing of 2 arrays based on 'name' , 'value' keys, priority set on first array ($newarray). in upper illustration method should homecoming 'false' because

both arrays have elements contain pair 'name' => 'size', 'value' => 'm' , 'name' => 'color', 'value' => 'black'.

althoug pair 'name' => 'width', 'value' => '123 cm', isn't in $oldarray result must remain false.

but if $oldarray had elements weren't contained in $newarray, result should array exact elements didn't fulfill conditions.

use array_diff:

$result=array_diff($a1,$a2); print_r($result);

php arrays multidimensional-array

No comments:

Post a Comment