Robert1995 Posted October 13, 2011 Report Posted October 13, 2011 (edited) PHP Map Array By Param , Am Editat Titlul pentru ca am gresit initial .Oricum , sugestii sau comentariile sunt bine veniteDa eu l-am scrisclass ArrayFunctions{ /** * Assign an element to a bidimensional array * This is A Private Function * @param $entry - @type array * @return $array */ private function assignToArray($entry){ return array(0 => $entry); } /** * Checks if $entries is an object it will become and array * If $entries is an array it will return $entries else array() * @param $entries - @type array / object * @return @array */ private function checkArray($entries){ if(is_object($entries)){ $entries = $entries->toArray(); } if(!is_array($entries)){ return array(); } return $entries; } /** * MapByParam an Array or an object * Features : CheckArray . If the param does not exist in one of the arrays this will return FALSE * Avoid Colisions . If this is True is the key is identical it will transform the `entries` into an array * @param $toMap - @type array or object * @param $param - @type string * @param $check_array - @type boolean ; DEFAULT : FALSE * @param $avoid_colisions - @type boolean ; DEFAULT : FALSE * @return @array */ public static function mapByParam($toMap , $param , $check_array = FALSE , $avoid_colisions = FALSE){ $colision = array(); $final = array(); $toMap = $this->checkArray($toMap); if(!empty($toMap)){ foreach($toMap as $k=>$map){ if(isset($map[$param])){ if(isset($final[$map[$param]]) && $avoid_colisions == TRUE){ if(!isset($colision[$param])){ $final[$map[$param]] = $this->assignToArray($final[$map[$param]]); $colision[$param] = true; } $final[$map[$param]][] = $map; } else { $final[$map[$param]] = $map; } } else { // Return Empty because array is malformed if($check_array == TRUE) return FALSE; } } } return $final; }} Edited October 13, 2011 by Robert1995 Quote
Andrei Posted October 13, 2011 Report Posted October 13, 2011 Hmm, nu prea vad vreo sugestie legata de implementare, e scrisa ok. De ce nu clasa statica? Quote
Robert1995 Posted October 14, 2011 Author Report Posted October 14, 2011 clasa statica? pai e functie statica e destul presupun , de exempluArrayFunctions::mapByParam($array , 'id'); Quote
Andrei Posted October 15, 2011 Report Posted October 15, 2011 My bad, ma uitasem doar la primele doua functii la tipul lor. Quote