Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // http://stackoverflow.com/questions/26713294/individually-sorting-subarrays-of-an-array-in-php
- // $arr1 gives the expected result, $arr2 does not,
- // although array_walk() should do exactly the same as the foreach - apply sort() on all subarrays of $arr
- $arr = array(
- array( 1, 1, 1, 2 ),
- array( 1, 2, array(1,1) ),
- array( 1, 2, array(1,1) ),
- array( 1, 2, array(1,1) ),
- array( 2, array(1,1,1) ),
- array( 1, 1, array(1,2) ),
- array( array(1,2), array(1,1) ),
- array( 1, 1, array(1,2) ),
- array( array(1,1), array(1,2) ),
- array( 1, array(1,1,2) ),
- array( 1, 1, array(1,2) ),
- array( array(1,1), array(1,2) ),
- array( 1, array(1,1,2) ),
- array( 1, array(1,1,2) ),
- array( array(1,1,1,2) )
- );
- // 15 rows
- $arr1 = $arr;
- foreach ($arr1 as $key => $val) {
- $derp = $val;
- sort($derp);
- $arr1[$key] = $derp;
- }
- sort($arr1);
- $arr1 = array_map("unserialize", array_unique(array_map("serialize", $arr1)));
- $arr2 = $arr;
- array_walk($arr2, 'sort');
- sort($arr2);
- $arr2 = array_map("unserialize", array_unique(array_map("serialize", $arr2)));
- echo array_values($arr1) == array_values(array(
- array( array(1,1,1,2) ),
- array( 1, array(1,1,2) ),
- array( 2, array(1,1,1) ),
- array( array(1,1), array(1,2) ),
- array( 1, 1, array(1,2) ),
- array( 1, 2, array(1,1) ),
- array( 1, 1, 1, 2 )
- ));
- // 7 rows
- echo array_values($arr2) == array_values(array(
- array( array(1,1,1,2) ),
- array( 1, array(1,1,2) ),
- array( 2, array(1,1,1) ),
- array( array(1,1), array(1,2) ),
- array( array(1,1,2), 1 ),
- array( 1, 1, array(1,2) ),
- array( 1, 2, array(1,1) ),
- array( 1, array(1,1), 2 ),
- array( 1, 1, 1, 2 )
- ));
- // 9 rows
- // array( 1, array(1,1), 2 ) is a duplicate of array( 1, 2, array(1,1) )
- // array( array(1,1,2), 1 ) is a duplicate of array( 1, array(1,1,2) )
- // this script will display two ones for the equalities
- // and six times "Notice: Array to string conversion in ..." for the line that contains array_walk
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement