colap Posted June 21, 2011 Share Posted June 21, 2011 A set doen't contain any duplicate value. Quote Link to comment https://forums.phpfreaks.com/topic/239945-how-to-create-or-make-a-set-array-in-php/ Share on other sites More sharing options...
ebmigue Posted June 21, 2011 Share Posted June 21, 2011 I could not agree more. IMO, PHP's array is ambivalent of what it really is, i..e., it could be a set (no duplicates), list (no order, has duplicates), or ordered-list (has order, has duplicates). It depends on the context of its use. I've tried to document it here: http://www.reetudes.com/docs/latest/ch04.html#the-model-api.general-constructs.lists As for creating a set in PHP, you can try re. re is an implementation the Relational Model of data. Its basic data type is the relation, which is basically a set. Also, the usual set operators (i.e., UNION, JOIN/INTERSECT, MINUS, TIMES) are made available. Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/239945-how-to-create-or-make-a-set-array-in-php/#findComment-1232563 Share on other sites More sharing options...
ebmigue Posted June 21, 2011 Share Posted June 21, 2011 .... Quote Link to comment https://forums.phpfreaks.com/topic/239945-how-to-create-or-make-a-set-array-in-php/#findComment-1232570 Share on other sites More sharing options...
colap Posted June 21, 2011 Author Share Posted June 21, 2011 Is it possible to create set array in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/239945-how-to-create-or-make-a-set-array-in-php/#findComment-1232725 Share on other sites More sharing options...
TeNDoLLA Posted June 21, 2011 Share Posted June 21, 2011 http://php.net/manual/en/function.array-unique.php Quote Link to comment https://forums.phpfreaks.com/topic/239945-how-to-create-or-make-a-set-array-in-php/#findComment-1232737 Share on other sites More sharing options...
ebmigue Posted June 21, 2011 Share Posted June 21, 2011 IMO, array_unique could not do the trick. Consider: $a = array(array(1), array(2)); print_r(array_unique($a)); //outputs array(0=>array(0=>1)) Clearly, $a has two elements which are themselves arrays: array(1) and array(2). IMO, further, array_unique has a bug. It treats two distinct elements (in this case array elements) as equal! Quote Link to comment https://forums.phpfreaks.com/topic/239945-how-to-create-or-make-a-set-array-in-php/#findComment-1232760 Share on other sites More sharing options...
TeNDoLLA Posted June 21, 2011 Share Posted June 21, 2011 I am not even sure what the original poster is looking for. But he said in the first post "does not contain duplicate values" so I thought array_unique might be helpful. Its also always possible to write your own function to modify data if there is not one that fits that purpose already in php. Quote Link to comment https://forums.phpfreaks.com/topic/239945-how-to-create-or-make-a-set-array-in-php/#findComment-1232786 Share on other sites More sharing options...
ebmigue Posted June 21, 2011 Share Posted June 21, 2011 Well, array_unique might do the trick for scalar elements. But as for non-scalar elements, PHP has this disclaimer in the manual (I found out just now): Note: Note that array_unique() is not intended to work on multi dimensional arrays. So, yes, array_unique would suffice for "simple sets" (i.e., sets who do not have nonscalar elements). But as for sets that does have nonscalar elements, it is an entirely different story. I hope PHP in the future will provide built-in set-theoretic operators and constructs. Quote Link to comment https://forums.phpfreaks.com/topic/239945-how-to-create-or-make-a-set-array-in-php/#findComment-1232806 Share on other sites More sharing options...
TeNDoLLA Posted June 21, 2011 Share Posted June 21, 2011 Yeh, well you could make the nested arrays as one dimensional array and then run array_unique. But this is really up to that what kind of and output you really want. Might not work in all occasions. // Function to flatten nested arrays. function flatten_array($array,$return) { for($x = 0; $x < count($array); $x++) { if(is_array($array[$x])) { $return = flatten_array($array[$x],$return); } else { if($array[$x]) { $return[] = $array[$x]; } } } return $return; } Quote Link to comment https://forums.phpfreaks.com/topic/239945-how-to-create-or-make-a-set-array-in-php/#findComment-1232833 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.