2016-02-28 98 views
1

我正在寻找一个简单的解决方案来创建一个小的功能合并两个数组与值的concat Concat的值(我使用它来创建HTML标记属性):如何合并两个数组与PHP

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

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

其结果是:

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

和一个选择: 如果$new不是数组,只是用$default["class"]的concat(如果这存在),而另一侧:如果$default是一个简单的字符串,转换为数组:$default["class"] = $default;

我创建了一个功能,但想用一种更简单,更短的方式为:

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;")); 

回答

1

我认为这是你需要的功能。我已经初始化CSS类和风格为空,在取决于你传递什么到功能,那么你得到的相关阵列

/** 
* 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

谢谢。你的函数可以很好的使用class属性,但是清除'$ default [“style”]'并且只保留'$ new [“style”]'值。 – rastafest

+0

我加了修正。我不知道你可以像默认值一样将类型传递给类 – Nick

+0

对不起,我在最后感到困惑:)这个函数对我很好。谢谢!:) – rastafest

0

一种方法,我认为适合你的需要,希望这是为适应和effecient如您所期望。

$array1 = array(
'class' => 'class1', 
'style' => 'display: none;' 
); 

$array2 = array(
    'class' => 'class2' 
); 

$arrayFinal = arrayMerge($array1, $array2); 
var_dump($arrayFinal); 

function arrayMerge($arr1, $arr2 = ''){ 
    // Array of attributes to be concatenated // 
    $attrs = array('class'); 
    if(is_array($arr2)){ 
     foreach($attrs as $attr){ 
      if(isset($arr1[$attr]) && isset($arr2[$attr])){ 
      // Not using .= to allow for smart trim (meaning empty check etc isn't needed // 
      $arr1[$attr] = trim($arr1[$attr] . ' ' . $arr2[$attr]); 
      } 
     } 
    }else{ 
     $arr1['class'] = trim($arr1['class'] . ' ' . $arr2); 
    } 

    return $arr1; 
} 
+0

感谢。但是这个函数在数组收入的时候起作用。有一个选项:这些变量是简单的字符串而不是数组。我不想每次都把它们放入数组中。 – rastafest

+0

有一个if语句来检查它是否是一个数组,否则它将它用作字符串并将其添加到“class”中。就像你的功能一样。 –

+0

另外,我添加了一个数组($ attrs),所以你可以选择添加什么。例如,显示不会被添加/连接。只有班级,因为班级在$ attrs。 –

0
$def = ['class' => 'red']; 
$new = ['class' => 'green', 'style' => 'style']; 

function to_array($in) { 
    return is_array($in) ? $in : ['class' => $in]; 
} 

$def = to_array($def); 
$new = to_array($new); 

$res = $def; 
array_walk($new, function ($val, $key) use (&$res) { 
    $res[$key] = trim(@$res[$key] . ' ' . $val); 
}); 

var_dump($res);