从字符串创建数组,然后与另一个数组进行比较

问题描述:

我有两个问题: 1.如何在给定起始字符串和结束字符串的情况下将字符串拆分为数组,从而使数组元素不在中间。 2.将我的新数组与另一个数组进行比较,如果有任何元素匹配,让用户知道哪些值匹配。从字符串创建数组,然后与另一个数组进行比较

我需要一个字符串的内容打入一个数组我需要的MAC地址的值

[MAC] 
mac=12EG23OCSOPC 
mac=111111111111 
mac=222222222222 
mac=333333333333 

我所需要的一切的值之后“MAC =”与之前\ n

然后我需要比较这些值与另一个数组,如果有相同的项目,我需要让用户知道哪些匹配这是我到目前为止。

function count_compare_macs($macs_to_compare, $new_array) 

{ 
    $macs_to_compare_count = count($macs_to_compare); 
    $new_array_count = count($new_array); 

     if(($macs_to_compare_count + $new_array_count) >= 5) 
      { 

      return false; 
        $error_message="You already have 5 MAC address in your system"; 
      }else{ 
       //here is where i need to compare the two arrays and if any are the same say false and set the error message. 
         $compare_arrays = array_Diff($macs_to_compare,$new_array) 
         if (!$compare_arrays){ 
           return true; 
           }else{ 


//here is where i need to compare the two arrays and if any are the same say false and set the error message. 
             $error_message= "The following mac is already used" . $match 
return false; 
             } 
       } 
    } 

你能为#1做的是:

$str = "mac=12EG23OCSOPC 
mac=111111111111 
mac=222222222222 
mac=333333333333"; 

$strSplode = explode("\n",$str); 

$mac = array(); 

foreach($strSplode as $m){ 
    list($temp, $mac[]) = explode("=", $m); 
} 

这里是第1部分演示:http://codepad.org/Taiyrvf2
(在演示中,我不得不通过\r\n爆炸第1部分,因为这是什么线被拆分,这一切都依赖于字符串是如何编码)

的第二部分:

function find_matches($mac1, $mac2){ 

    $matches = array(); 

    foreach($mac1 as $mac){ 
     if(in_array($mac, $mac2)){ 
      $matches[] = $mac; 
     } 
    } 

    //return an array of matching mac addresses 
    return $matches; 

} 
+0

非常感谢您的协助。我从来没有创建一个基于比赛的新阵列,如果有的话。很有帮助。 – Denoteone 2011-04-14 16:57:10

+0

没问题^ _ ^随时可以 – Neal 2011-04-14 16:59:45