【问题标题】:How to push the condition based value in existing array in PHP如何在PHP中的现有数组中推送基于条件的值
【发布时间】:2019-03-08 09:21:53
【问题描述】:

我有三个数组

  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 数组值至少应该存在一个值 $topicAssingned 表示基于 groupID,我必须将一个值推送到 $relavantGroups 就像 disable : D 假设值不存在表示 禁用:A

预期输出:

[
     "id" => "G-001",
    "name" => "3 A",
    "active" => false,
    "disable" => "D"
],
[
     "id" => "G-002",
    "name" => "3 B",
    "active" => false,
    "disable" => "A"
]

【问题讨论】:

  • 你能说清楚一点吗?
  • @vivek_23,$topicsSelected 数组值(T-100 或 T-600)至少一个值应存在于 $topicAssingned 数组中,基于 groupID(G-001)。 $topicAssingned under topics , topicID : T-100 is present ,所以 "disable" : "D"
  • @vivek_23,$topicsSelected 数组值(T-100 或 T-600)至少一个值应存在于 $topicAssingned 数组中,基于 groupID(G-002)。 $topicAssingned 在 topics 下,topicID : T-600 is not present ,所以 "disable" : "A"

标签: php php-5.6


【解决方案1】:
<?php 

$topicsSelected = [ "T-100","T-600"];

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

$topicAssigned = [
    "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"
            ]
        ]
    ],
];

$topic_selected_map = [];

foreach($topicsSelected as $each_topic){
    $topic_selected_map[$each_topic] = true;
}

$relevant_group_map = [];

foreach($relavantGroups as $each_group){
    $relevant_group_map[$each_group['id']] = $each_group;
}

$result = [];

foreach($topicAssigned as $each_assigned_topic){
    if(!isset($relevant_group_map[$each_assigned_topic['groupID']])) continue;

    $topics_not_found = true;
    foreach($each_assigned_topic['topics'] as $each_topic => $topic_details){
        if(isset($topic_selected_map[$each_topic])){
            $topics_not_found = false;
            break;
        }
    }

    $result[] = [
        'id' => $each_assigned_topic['groupID'],
        'name' => $each_assigned_topic['groupName'],
        'active' => $relevant_group_map[$each_assigned_topic['groupID']]['active'],
        'disable' => ($topics_not_found === true ? 'A' : 'D')
    ];
}


print_r($result);

输出:

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

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

)
  • 首先,制作$topicsSelected 值的映射(关联数组)。 $relavantGroups 也是如此。这是为了让检查更有效率。在Hash Table 上查看更多信息。

  • 现在,遍历$topicAssigned,然后遍历其中每个组的topics。现在,使用简单的isset 函数检查$topicsSelected 中是否存在主题。如果是,我们禁用它们,否则我们不禁用。

【讨论】:

    【解决方案2】:

    你问的不是很清楚,代码有点奇怪,但我会试一试。 首先修复您的数组声明 - 您应该使用 =&gt; 而不是 :;

    您必须迭代 $relavantGroups 并为每个元素迭代 $topicAssingned 数组。然后进行简单的比较,看看Id 组是否存在,就完成了!

    这是我的解决方案(又快又脏): You can see it here

    foreach ($relavantGroups as &$g) {
        $found = false;
        foreach ($topicAssingned as $key => $assigned) {
            if ($key === $g["id"] && is_array($assigned["topics"])) {
                foreach ($assigned["topics"] as $topic) {
                    if (in_array($topic["topicID"], $topicsSelected)) {
                        $found = true;
                        break;
                    }
                }
            }
        }
        $g["disable"] = $found ? "D" : "A";
    }
    var_dump($relavantGroups);
    

    更新了解决方案 - 请注意,我使用 in_array() 来确定 topicID 是否存在。这意味着$topicsSelected 数组中的任何值都会影响结果。

    希望我能帮上忙。

    这将输出(基于您的示例):

    array(2) {
      [0]=> array(4) {
        ["id"]=> string(5) "G-001"
        ["name"]=> string(3) "3 A"
        ["active"]=> bool(false)
        ["disable"]=> string(1) "D"
      }
      [1]=> array(4) {
        ["id"]=> string(5) "G-002"
        ["name"]=> string(3) "3 B"
        ["active"]=> bool(false)
        ["disable"]=> string(1) "A"
      }
    }
    

    【讨论】:

    • $topicsSelected 数组值(T-100 或 T-600)至少一个值应存在于 $topicAssingned 数组中,基于 groupID(G-001)。 $topicAssingnedtopics 下,topicID :T-100 存在,所以“禁用”:“D”。
    • $topicsSelected 数组值(T-100 或 T-600)至少一个值应存在于 $topicAssingned 数组中,基于 groupID(G-002)。 $topicAssingnedtopics 下,topicID :T-600 不存在,所以“禁用”:“A”。
    • 我不明白...如果两者都存在会怎样??? - 在您的示例中,"G-002"两者都没有,仍然是 D。
    • 是的 G-002 没有 topicID 所以“禁用”:“A”,我已经更新了我的预期输出,请检查
    • 如果两者都存在也“禁用”:“D”
    猜你喜欢
    • 2021-09-06
    • 2019-12-07
    • 2014-07-17
    • 2015-02-09
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 2018-12-20
    • 2017-03-21
    相关资源
    最近更新 更多