Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用字符串创建数组,然后与另一个数组进行比较_Php - Fatal编程技术网

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

Php 用字符串创建数组,然后与另一个数组进行比较,php,Php,我有两个问题: 1.如何在给定起始字符串和结束字符串的情况下将字符串拆分为数组,使数组元素不包含中间的所有内容。 2.将我的新数组与另一个数组进行比较,如果有匹配的元素,请让用户知道哪些值匹配 我需要把一个字符串的内容分解成一个数组,我需要mac地址的值 [MAC] mac=12EG23OCSOPC mac=111111111111 mac=222222222222 mac=333333333333 我需要“mac=”之后和之前的所有值\n 然后我需要将这些值与另一个数组进行比较,如果有相同的

我有两个问题: 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做的是:

下面是第1部分的演示:
(在演示中,我不得不将第一部分分解为
\r\n
,因为这是行的拆分依据,这完全取决于字符串的编码方式)

第二部分:

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;

}

非常感谢你的帮助。我从未想过根据匹配项(如果有的话)创建一个新数组。非常有用。
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;

}