Jump to content
Robert1995

PHP Map Array

Recommended Posts

Posted (edited)

PHP Map Array By Param , Am Editat Titlul pentru ca am gresit initial .

Oricum , sugestii sau comentariile sunt bine venite

Da eu l-am scris


class 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 by Robert1995

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...