Some critical point you have to avoid copy values from a multi-dimensional array in PHP and you can take after with offered code to get one of a kind values in an avoid.
array_unique() method are utilized to avoid copy information from given array with the help of array_map() method. We saw this in the last tutorial with one-dimensional array values. Here array_map() method returns an array containing all the elements of given array values after applying the specific callback function to every each element. The passing parameters list with the specific callback function allows should equal to the number of arrays passed to the array_map().
In this example, we are going to use unserialize & serialize callback functions to filter the duplicate values in a multi-dimensional array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php $language_mark_array = array( 'language1'=> array('name'=>'PHP', 'mark'=>'85', 'grade'=>'A'), 'language2'=> array('name'=>'HTML', 'mark'=>'65', 'grade'=>'B'), 'language3'=> array('name'=>'CSS', 'mark'=>'45', 'grade'=>'C'), 'language4'=> array('name'=>'PHP', 'mark'=>'85', 'grade'=>'A') ); $language_mark_array = array_map("unserialize", array_unique(array_map("serialize", $language_mark_array))); print_r($language_mark_array); /* output as : here language4 array removed Array ( [language1] => Array ( [name] => PHP [mark] => 85 [grade] => A ) [language2] => Array ( [name] => HTML [mark] => 65 [grade] => B ) [language3] => Array ( [name] => CSS [mark] => 45 [grade] => C ) ) */ ?> |