note 37527 modified in function.array-diff by vrana

From: Date: Tue, 17 Aug 2004 13:57:26 +0000
Subject: note 37527 modified in function.array-diff by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80026@lists.php.net to get a copy of this message
From the page: Note: Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]); I've found a way to bypass that. I had 2 arrays made of arrays. I wanted to extract from the first array all the arrays not found in the second array. So I used the serialize() function: <?php function my_serialize(&$arr,$pos){ $arr = serialize($arr); } function my_unserialize(&$arr,$pos){ $arr = unserialize($arr); } //make a copy $first_array_s = $first_array; $second_array_s = $second_array; // serialize all sub-arrays array_walk($first_array_s,'my_serialize'); array_walk($second_array_s,'my_serialize'); // array_diff the serialized versions $diff = array_diff($first_array_s,$second_array_s); // unserialize the result array_walk($diff,'my_unserialize'); // you've got it! print_r($diff); ?> --was-- From the page: Note: Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]); I've found a way to bypass that. I had 2 arrays made of arrays. I wanted to extract from the first array all the arrays not found in the second array. So I used the serialize() function: function my_serialize(&$arr,$pos){ $arr = serialize($arr); } function my_unserialize(&$arr,$pos){ $arr = unserialize($arr); } //make a copy $first_array_s = $first_array; $second_array_s = $second_array; // serialize all sub-arrays array_walk($first_array_s,'my_serialize'); array_walk($second_array_s,'my_serialize'); // array_diff the serialized versions $diff = array_diff($first_array_s,$second_array_s); // unserialize the result array_walk($diff,'my_unserialize'); // you've got it! print_r($diff); http://php.net/manual/en/function.array-diff.php

« previous php.notes (#80026) next »