我有array
这样:
array(1) {
[0]=>
string(16) "1785328791698338"
}
和其他array
一样:
array(7) {
[0]=>
string(17) "10207252567926988"
[1]=>
string(17) "10208823390691752"
[2]=>
string(17) "10209064245580796"
[3]=>
string(16) "1274474365912572"
[4]=>
string(16) "1294280923934896"
[5]=>
string(16) "1317727711586522"
[6]=>
string(16) "1785328791698338"
}
我应该检查第一个数组中的某些元素(在这种情况下只有一个,但它可以变化)是否与第二个数组中的某些元素相同,如果是,则从第一个数组中删除它们。我尝试这样做,但它不起作用:
function findSameValuesOfArrays($arrayOne,$arrayTwo){
$newArray=array();
foreach($arrayOne as $a){
$newArray[0]=$a;
}
foreach($arrayTwo as $b){
$newArray[1]=$b;
}
if (strpos($newArray[1],$newArray[0])) {
return true;
}
}
这只是为了找出是否有相同的元素,然后我可能会取消设置这些值所在的键。但函数返回NULL
。
答案 0 :(得分:4)
array_diff
完全符合您的要求。
$sourceArr = array(1,2,3,4,5);
$filterArr = array(2,4);
$result = array_diff($sourceArr, $filterArr);
var_dump($result);
结果:
array(3) {
[0]=>
int(1)
[2]=>
int(3)
[4]=>
int(5)
}
答案 1 :(得分:0)
您可以在此处使用array_intersect
,如:
$arr3 = array_intersect($arr1,$arr2);
print_r($arr3);
以下是一个例子:
了解更多信息答案 2 :(得分:0)
试试这个:
$arrFirst = array("1785328791698338","10207252567926988");
$arrMain = array("10207252567926988","10208823390691752","10209064245580796","1274474365912572","1294280923934896","1317727711586522","1785328791698338");
foreach ($arrFirst as $key => $value) {
if(in_array($value, $arrMain )) { // check if value exist in seconf array
unset($arrFirst[$key]); // if yes - unset that value
}
}
答案 3 :(得分:0)
您可以在此处使用 array_intersect 返回一个数组,其中包含所有参数中存在的array1的所有值。然后是array_diff
public class LabelData
{
public int StartRange { get; set; }
public int EndRange { get; set; }
}
public class ConfigInfo
{
public string ParentGroup;
public string Label;
public int ID;
public int StartRange;
public int EndRange;
}
public Dictionary<string, Dictionary<string, LabelData>> GetData(List<ConfigInfo> configList)
{
var labelData = new Dictionary<string, Dictionary<string, LabelData>>();
foreach (var listItem in configList)
{
labelData[listItem.ParentGroup] = configList.Distinct().ToDictionary(x => x.Label.ToString(), row => new LabelData()
{
StartRange = Convert.ToInt32(listItem.StartRange.ToString()),
EndRange = Convert.ToInt32(listItem.EndRange.ToString())
});
}
return labelData;
}
protected void createFile()
{
List<ConfigInfo> configInfoList = new List<ConfigInfo>();
ConfigInfo configInfo = new ConfigInfo();
configInfo.ParentGroup = "Employee1";
configInfo.StartRange = 11;
configInfo.EndRange = 22;
configInfo.Label = "YYY";
configInfoList.Add(configInfo);
configInfo = new ConfigInfo();
configInfo.ParentGroup = "Employee2";
configInfo.StartRange = 24;
configInfo.EndRange = 56;
configInfo.Label = "XXX";
configInfoList.Add(configInfo);
if (!File.Exists(fileName))
{
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
JsonTextWriter writer = new JsonTextWriter(sw);
Dictionary<string, Dictionary<string, LabelData>> data = GetData(configInfoList);
string json = JsonConvert.SerializeObject(data,Formatting.Indented);
sw.Write(json);
sw.Close();
}
}