Thursday 30 August 2018

How to merge two tables and concatenate values ​​with PHP

I'm looking for an easy solution to create a little function to merge two arrays with value concat (I'm using it to create html tag attribute):

$default["class"] = "red";

$new["class"] = "green";
$new["style"] = "display:block"

The result:
$res["class"] = "red green";
$res["style"] = "display: block";

and one more option: if the $new is not an array, just concat with the $default["class"] (if this exist), and the other side: if the $default is a simple string, convert to array: $default["class"] = $default;
I created a function but would like to use an easier, shorter way for that:
function attrMerge( $default, $new="" ){
    $res = array();

    if(!is_array($default)) {
        $res["class"] = $default;
    }
    else {
        $res = $default;
    }

    if( $new !== "" ){
        if(!is_array($new)) {
            if(isset($res["class"])){
                $res["class"].= " ".$new;
            }
        }
        else {
            foreach($new as $key=>$value) {
                if( isset($res[$key]) ) {
                    $res[$key].= " ".$value;
                }
                else {
                    $res[$key] = $value;
                }
            }
        }
    }

    return $res;
}

$a = attrMerge("red", array("class"=>"green", "style"=>"display: block;"));


I think this is the function that you need. I have initialised the css classes and styles as empty and in depends what you pass into the function then you get the relevant array
/**
* This function returns an array of classes and styles
*
* @param $default
* @param $new
* @return array
*/
function attrMerge($default=null, $new=nul)
{
    $result = array();
    $result['class'] = "";
    $result['style'] = "";

    // add default class if exists
    if (!empty($default) && is_string($default)) {
        // $default is string
        $result['class'] = $default;
    }
    if (!empty($default)
        && is_array($default)
    ) {
        if (array_key_exists('class', $default)
            && !empty($default['class'])
        ) {
           // $default['class'] exists and it's not empty
           $result['class'] = $default['class'];
        }
        if (array_key_exists('style', $default)
            && !empty($default['style'])
        ) {
           // $default['style'] exists and it's not empty
           $result['style'] = $default['style'];
        }
    }

    // add additional classes OR styles
    if (!empty($new)) {
        if(!is_array($new)) {
            $result['class'] = empty($result['class'])
                ? $new
                : $result['class'] . " " . $new;
        } else {
            foreach ($new as $key => $value) {
                if (isset($result[$key])) {
                    $result[$key] = empty($result[$key])
                        ? $value
                        : $result[$key] . " " . $value;
                } else {
                    $result[$key] = $value;
                }
            }
        }
    }

    return $result;
}

0 comments:

Post a Comment