Php 如果信息存在于两个数组之一中,如何不显示信息

Php 如果信息存在于两个数组之一中,如何不显示信息,php,arrays,json,api,Php,Arrays,Json,Api,情况是,我有两个数组通过API收集JSON数据: $players = getAPI("http://xx.xxx.xxx.xx:xxxxx/players.json?apiKey=xxxxxxxxxxxxxxxxxxxxxxxx"); $recents = getAPI("xx.xxx.xxx.xx:xxxxx/recent.json?apiKey=xxxxxxxxxxxxxxxxxxxxxxxx"); 方法是获取内容并将JSON解码为数组。 对于玩家数组,我们在数组中有以下数据: 美元

情况是,我有两个数组通过API收集JSON数据:

$players  = getAPI("http://xx.xxx.xxx.xx:xxxxx/players.json?apiKey=xxxxxxxxxxxxxxxxxxxxxxxx");
$recents  = getAPI("xx.xxx.xxx.xx:xxxxx/recent.json?apiKey=xxxxxxxxxxxxxxxxxxxxxxxx");
方法是获取内容并将JSON解码为数组。 对于玩家数组,我们在数组中有以下数据:

美元玩家

[
  {
    "id": "76561198033377272",
    "name": "PitMonk",
    "position": {
      "x": -339,
      "y": 26,
      "z": 191
    },
    "rotation": 128,
    "time": 418310,
    "ip": "",
    "inventory": {
      "main": [],
      "belt": [
        {
          "name": "rock",
          "amount": 1,
          "blueprint": false,
          "condition": 100
        },
        {
          "name": "torch",
          "amount": 1,
          "blueprint": false,
          "condition": 100
        }
      ],
      "wear": []
    }
  },
  {
    "id": "76561198088638439",
    "name": "Pippa",
    "position": {
      "x": -337,
      "y": 25,
      "z": 177
    },
    "rotation": 73,
    "time": 419136,
    "ip": "",
    "inventory": {
      "main": [
        {
          "name": "arrow.wooden",
          "amount": 12,
          "blueprint": false
        },
        {
          "name": "bow.hunting",
          "amount": 1,
          "blueprint": false,
          "condition": 93
        },
        {
          "name": "blueprint_fragment",
          "amount": 25,
          "blueprint": false
        },
        {
          "name": "metal.fragments",
          "amount": 1366,
          "blueprint": false
        },
        {
          "name": "metal.refined",
          "amount": 48,
          "blueprint": false
        },
        {
          "name": "charcoal",
          "amount": 1120,
          "blueprint": false
        },
        {
          "name": "lowgradefuel",
          "amount": 738,
          "blueprint": false
        }
      ],
      "belt": [
        {
          "name": "rock",
          "amount": 1,
          "blueprint": false,
          "condition": 100
        },
        {
          "name": "torch",
          "amount": 1,
          "blueprint": false,
          "condition": 100
        },
        {
          "name": "pickaxe",
          "amount": 1,
          "blueprint": false,
          "condition": 76
        },
        {
          "name": "pickaxe",
          "amount": 1,
          "blueprint": false,
          "condition": 17
        },
        {
          "name": "pickaxe",
          "amount": 1,
          "blueprint": false,
          "condition": 100
        },
        {
          "name": "pickaxe",
          "amount": 1,
          "blueprint": false,
          "condition": 100
        }
      ],
      "wear": [
        {
          "name": "burlap.shirt",
          "amount": 1,
          "blueprint": false
        },
        {
          "name": "attire.hide.skirt",
          "amount": 1,
          "blueprint": false
        }
      ]
    }
  }
]
$recents

[
  {
    "id": "76561198039206786",
    "name": "JakeGroves"
  },
  {
    "id": "76561198088638439",
    "name": "Pippa"
  },
  {
    "id": "76561198033377272",
    "name": "PitMonk"
  },
  {
    "id": "76561198146864439",
    "name": "YepWellDone"
  },
  {
    "id": "76561198164836207",
    "name": "Baz"
  },
  {
    "id": "76561198076406281",
    "name": "xwalnutx"
  },
  {
    "id": "76561197985716090",
    "name": "Darkflame134"
  },
  {
    "id": "76561198263423842",
    "name": "XitaikiznerX"
  },
  {
    "id": "76561198129952244",
    "name": "NatanGamer"
  },
  {
    "id": "76561198071842055",
    "name": "Baha Bey"
  }
]
正如你所看到的,玩家是有联系的人,而recents是最近有联系的人的总列表

我尝试过这样做:

 foreach ($players as $player) {
       echo $players->name;
    }
    echo "</br></br>";
    foreach ($recent as $rec) {
      if ($rec->name != $player->name) {
       echo $rec->name . "</br>";
     }
    }

因此,它只是忽略了“pippa”,我不确定是否可以与两个数组进行交互以获得唯一值?

您感兴趣的是列出
$users
中不存在于
$players
$recents
中的所有名称,对吗

假设您只对名称感兴趣:

// First, let's get a new array with all names from both arrays (can contain dups)
$pcNames = array_map($players + $recents, function($playerObject) {
    return $playerObject->name;
});

// Next, let's remove all dups
$pcNames = array_unique($pcNames);

// === At this point you have an array with all names from `$players` and `$recents` ===
// === You may do something else with those, but I'll now create another array with  ===
// === all users not in the players/recents lists.                                   ===

// Now let's also get a list of names of users in the `$users` variable
$userNames = array_map($users, function($playerObject) {
    return $playerObject->name;
});

// And finally let's get all names which are not in players or recents
$diffNames = array_diff($userNames, $pcNames);

// Let's output those to see whether it worked
var_dump($diffNames);

当然,根据您的用例,还有其他方法。例如,我们可以提取所有三个数组的名称,然后只使用带有3个参数的
array_diff
(但是您没有
$pcArray
副产品),或者如果您确实想要比较ID,但打印名称,我们必须更改所有内联函数以提取ID而不是名称,并进一步向下引用用户数组以获取实际名称,等等。

您的算法似乎还可以,到底是什么问题?输出看起来很奇怪,我不明白它怎么能在同一行显示两个玩家的名字,因为你在每个名字后面都回显一个br。“Pitmank Pippa”如何显示在同一行上?也许你应该在ID上匹配用户,这比比较名字更干净。@OlivierH yaa我正在使用名字,这样我就可以知道它现在是谁,如果你在第一行看到Pitmank和Pippa都在$players数组中,它们都在$recents数组中,但是我要做的是遍历$recents数组,忽略$players数组中存在的任何条目什么是
$users
数组?你的代码中根本没有使用
$players
。我写错了$users=$players。。。。我已经做了这个代码,我认为它是工作
foreach($rec){foreach($users as$user){if($user->name==$rec->name){break;}}}if($user->name!=$rec->name){echo$rec->name;}}
为什么使用中间数组
$userNames
?如果他的用户数据量很大,这可能会消耗大量内存。如果在
$pcNames
中找不到
$user->name
,则只在
$users
数组上循环,并为另一个数组提供数据,会更有效;你说得对,它可能更有效。请随意在回答中发表您的想法。我的答案是尝试使用数组函数将其分解为多个步骤,这些函数描述了它们在做什么,这样数据的“流”就更清晰了。
// First, let's get a new array with all names from both arrays (can contain dups)
$pcNames = array_map($players + $recents, function($playerObject) {
    return $playerObject->name;
});

// Next, let's remove all dups
$pcNames = array_unique($pcNames);

// === At this point you have an array with all names from `$players` and `$recents` ===
// === You may do something else with those, but I'll now create another array with  ===
// === all users not in the players/recents lists.                                   ===

// Now let's also get a list of names of users in the `$users` variable
$userNames = array_map($users, function($playerObject) {
    return $playerObject->name;
});

// And finally let's get all names which are not in players or recents
$diffNames = array_diff($userNames, $pcNames);

// Let's output those to see whether it worked
var_dump($diffNames);