LLeoun Posted October 15, 2009 Share Posted October 15, 2009 Hi all, How can be duplicates removed from a multidimensional array attending to just one value of the element? Here an example (taken from a post opened here time ago) of what I mean : The following code will show the array without the last 'ID' => 22222, 'Name' => 'Mike' <?php $result = array ( 'SearchContentByEverythingResult' => array ( 'Content' => array ( 0 => array ( 'ID' => 11111, 'Name' => 'Bill' ), 1 => array ( 'ID' => 22222, 'Name' => 'Mike' ), 2 => array ( 'ID' => 33333, 'Name' => 'Sam' ), 3 => array ( 'ID' => 22222, 'Name' => 'Mike' ) ) ) ); /** * remove dupes */ $a = $result['SearchContentByEverythingResult']['Content']; $k = count($a); for($i=0; $i < $k-1; $i++) { for($j=$i+1; $j < $k; $j++) { if ($a[$i] == $a[$j]) unset($a[$j]); } } $result['SearchContentByEverythingResult']['Content'] = $a; echo '<pre>', print_r($result, true), '</pre>'; ?> But if the $result is composed like this: $result = array ( 'SearchContentByEverythingResult' => array ( 'Content' => array ( 0 => array ( 'ID' => 11111, 'Name' => 'Bill' ), 1 => array ( 'ID' => 22222, 'Name' => 'Mike' ), 2 => array ( 'ID' => 33333, 'Name' => 'Sam' ), 3 => array ( 'ID' => 44444, 'Name' => 'Mike' ) ) ) ); where the repetition is in the name, not in the ID .. the code will show two Mikes. Using this last version of $result how can I remove index 3, how can I have just one Mike no matter what his ID is?? Thanks a ton! Link to comment https://forums.phpfreaks.com/topic/177766-duplicates-removed-from-a-multidimensional-array-attending-to-one-value/ Share on other sites More sharing options...
ILMV Posted October 15, 2009 Share Posted October 15, 2009 Instead of trying to manipulate the array here, can you tell us how the array is being constructed... is it from a database? There is a chance you can modify code elsewhere to make this more efficient. Link to comment https://forums.phpfreaks.com/topic/177766-duplicates-removed-from-a-multidimensional-array-attending-to-one-value/#findComment-937345 Share on other sites More sharing options...
LLeoun Posted October 15, 2009 Author Share Posted October 15, 2009 Hi ILMV, thanks for anwering. The array is coming from a webservice call, so I'm afraid no changes can be made. Thanks again Link to comment https://forums.phpfreaks.com/topic/177766-duplicates-removed-from-a-multidimensional-array-attending-to-one-value/#findComment-937429 Share on other sites More sharing options...
LLeoun Posted October 15, 2009 Author Share Posted October 15, 2009 Just in case somebody needs it, the way to do it is: <?php $result = array ( 'SearchContentByEverythingResult' => array ( 'Content' => array ( 0 => array ( 'ID' => 11111, 'Name' => 'Bill' ), 1 => array ( 'ID' => 22222, 'Name' => 'Mike' ), 2 => array ( 'ID' => 33333, 'Name' => 'Sam' ), 3 => array ( 'ID' => 44444, 'Name' => 'Mike' ) ) ) ); $a = $result['SearchContentByEverythingResult']['Content']; $k = count($a); for($i=0; $i < $k-1; $i++) { if ( false == isset( $a[$i] ) ) { continue; } for($j=$i+1; $j < $k; $j++) { if ( false == isset( $a[$j] ) ) { continue; } if ( $a[$i]['ID'] == $a[$j]['ID'] || $a[$i]['Name'] == $a[$j]['Name'] ) { unset($a[$j]); } } } $result['SearchContentByEverythingResult']['Content'] = $a; echo '<pre>', print_r($result, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/177766-duplicates-removed-from-a-multidimensional-array-attending-to-one-value/#findComment-937451 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.