2014-02-21 43 views
0

我查询用户信息LDAP的钥匙,被拉扯的信息是用户角色的。一些角色被分配到一个中心,有些是没有中心的具体我试图合并两个数组,一个包含值和其他只包含我想使用的阵列

$info = ldap_get_entries($ds, $sr)or die('get info fail'); 
    foreach($info[$i]['memberof'] as $key => $value){ 
     $var = $info[$i]['memberof'][$key]; 
     $replaceSearch = array('dc=','dc=co','dc=uk','ou=Groups','ou=','cn='); 
     $var = str_replace($replaceSearch,'',$var); 
     $var = explode(',', $var); 
     $var[$key] = array_filter($var); 

     if(count($var[$key])!=1){ 
      $index = array('role','center','country','region'); 
     }else{ 
      $index = array('role'); 
     } 
     $newArray[$key] = array_combine($index, $var[$key]); 
    } 

我这个得到的错误是array_combine公正的全球角色,它是说,两个数组大小也不一样。这是因为一些角色并不是特定于中心的,所以这些数组中的唯一项是array =('role'=>'WhateverRole'),其中作为特定于中心的角色是array =('role '=>'WhateverRole','center'=>'centerName','country'=>'countryName','region'=>'regionName')。这就是为什么我说if(count($ var [$ key])!= 1),根据数组中元素的数量设置$ index。我在php 5.4.16中没有得到错误,但是5.3.xx的人得到了array_combine错误。有没有其他方法可以用来防止错误?

回答

0
  <?php 
      $ar1 = array("color" => array("favorite" => "red"), 5); 
      $ar2 = array(10, "color" => array("favorite" => "green", "blue")); 
      $result = array_merge_recursive($ar1, $ar2); 
      print_r($result); 
      ?> 
      The above example will output: 

       Array 
      (
       [color] => Array 
        (
         [favorite] => Array 
         (
          [0] => red 
          [1] => green 
         ) 

         [0] => blue 
        ) 

       [0] => 5 
       [1] => 10 
      ) 

     You Can Merge Differennt Size of array Using array_merge_recursive($array1, $array2) Method. 
     learn More php.net 

enter image description here

相关问题