【问题标题】:How to check the condition in given three array如何检查给定三个数组中的条件
【发布时间】:2019-07-30 09:53:49
【问题描述】:

我有三个数组

  1. 主题已选
  2. 相关组
  3. topicAssingned
$topicsSelected = [ "T-100","T-600"];

$relavantGroups = [[ "id" => "G-001","name" => "3 A","active"=> false ], ["id"=> "G-002","name"=> "3 B","active"=> false]  ];

$topicAssingned = [
    "G-001" => [
        "groupID" => "G-001",
        "groupName" => "3 A",
        "topics" => [
            "T-100" => [
                "topicID" => "T-100"
            ],
            "T-200" => [
                "topicID" => "T-200"
            ]
        ]
    ],
    "G-002" => [
        "groupID" => "G-002",
        "groupName" => "3 B",
        "topics" => [
            "T-400" => [
                "topicID" => "T-400"
            ],
            "T-500" => [
                "topicID" => "T-500"
            ]
        ]
    ],
];

$topicsSelected 数组值(T-100 或 T-600)至少一个值应存在于 $topicAssingned 数组中,基于 groupID(G-001)。 $topicAssingned 在 topics , topicID : T-100 是 present ,所以Disable : D

$topicsSelected 数组值(T-100 或 T-600)至少一个值应存在于 $topicAssingned 数组中,基于 groupID(G-002)。 $topicAssingned under topics , topicID : T-100 & T-600 is not present ,所以Disable : A

预期输出:

[
     "id": "G-001",
    "name": "3 A",
    "active": false,
    "Disable" : "D"
],
[
     "id": "G-002",
    "name": "3 B",
    "active": false,
    "Disable" : "A"
]

我的代码

    foreach ($relavantGroups as &$g) {
    $found = false;
    foreach ($topicAssingned as $key => $assigned) {
        if ($key === $g["id"]) {
            $found = true;
            break;
        }
    }
    $g["disable"] = $found ? "D" : "A";
}
echo "<pre>";
print_r($relavantGroups);

我的输出

    Array
(
    [0] => Array
        (
            [id] => G-001
            [name] => 3 A
            [active] => 
            [disable] => D
        )

    [1] => Array
        (
            [id] => G-002
            [name] => 3 B
            [active] => 
            [disable] => D
        )

)

【问题讨论】:

  • 你能解释一下吗?您的第三个数组比较代码在哪里?你面临什么问题?在这里我只看到活动元素是空的并且禁用是不同的?我对吗?你能给出所有三个数组的 JSON 格式,以便我们在最后尝试吗?

标签: php arrays multidimensional-array php-5.6


【解决方案1】:

你可以试试这个sn-p,

foreach ($relavantGroups as &$g) {
    $found = false;
    foreach ($topicAssingned as $key => $assigned) {
        if ($key === $g["id"]) {
            $temp  = array_keys($assigned['topics']); // fetching all topic ids
            $intr  = array_intersect($topicsSelected, $temp); // checking if there are any matching values between topicSelected and traversed values
            $found = (!empty($intr) ? true : false); // if found return and break
            break;
        }
    }
    $g["disable"] = $found ? "D" : "A";
}
print_r($relavantGroups);

array_intersect — 计算数组的交集
array_keys — 返回数组的所有键或键的子集

输出

Array
(
    [0] => Array
        (
            [id] => G-001
            [name] => 3 A
            [active] => 
            [disable] => D
        )

    [1] => Array
        (
            [id] => G-002
            [name] => 3 B
            [active] => 
            [disable] => A
        )

)

Demo

【讨论】:

  • 对你所做的事情以及为什么它解决了 OP 的问题的一些解释总是有助于帮助其他人理解你的解决方案。
猜你喜欢
  • 2023-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-08
  • 1970-01-01
相关资源
最近更新 更多