How to push the condition based value in existing array in PHP The Next CEO of Stack OverflowHow can I prevent SQL injection in PHP?How to check if PHP array is associative or sequential?PHP: Delete an element from an arrayHow do I make a redirect in PHP?How do I get PHP errors to display?How to Sort Multi-dimensional Array by Value?How Do You Parse and Process HTML/XML in PHP?PHP array delete by value (not key)How does PHP 'foreach' actually work?How to check the condition in given three array

Is it okay to store user locations?

I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin

How to be diplomatic in refusing to write code that breaches the privacy of our users

What is the point of a new vote on May's deal when the indicative votes suggest she will not win?

How do spells that require an ability check vs. the caster's spell save DC work?

Is a stroke of luck acceptable after a series of unfavorable events?

Text adventure game code

When airplanes disconnect from a tanker during air to air refueling, why do they bank so sharply to the right?

Does the Brexit deal have to be agreed by both Houses?

Why do professional authors make "consistency" mistakes? And how to avoid them?

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

If the heap is initialized for security, then why is the stack uninitialized?

Apart from "berlinern", do any other German dialects have a corresponding verb?

Anatomically Correct Mesopelagic Aves

Can a single photon have an energy density?

Where to find order of arguments for default functions

Shade part of a Venn diagram

Why do remote companies require working in the US?

Return the Closest Prime Number

Describing a person. What needs to be mentioned?

Natural language into sentence logic

Increase performance creating Mandelbrot set in python

Robert Sheckley short story about vacation spots being overwhelmed

What makes a siege story/plot interesting?



How to push the condition based value in existing array in PHP



The Next CEO of Stack OverflowHow can I prevent SQL injection in PHP?How to check if PHP array is associative or sequential?PHP: Delete an element from an arrayHow do I make a redirect in PHP?How do I get PHP errors to display?How to Sort Multi-dimensional Array by Value?How Do You Parse and Process HTML/XML in PHP?PHP array delete by value (not key)How does PHP 'foreach' actually work?How to check the condition in given three array










1















I am having three arrays



  1. topicsSelected

  2. relavantGroups

  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 array values at least one value should present $topicAssingned means based on groupID, i have to push one value to $relavantGroups like disable : D suppose value not present means disable : A



Expected output:



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









share|improve this question



















  • 2





    Can you be more clear?

    – vivek_23
    Mar 8 at 9:30











  • @vivek_23,$topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-001). $topicAssingned under topics , topicID : T-100 is present , so "disable" : "D"

    – user9988771
    Mar 8 at 10:20











  • @vivek_23,$topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-002). $topicAssingned under topics , topicID : T-600 is not present , so "disable" : "A"

    – user9988771
    Mar 8 at 10:20















1















I am having three arrays



  1. topicsSelected

  2. relavantGroups

  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 array values at least one value should present $topicAssingned means based on groupID, i have to push one value to $relavantGroups like disable : D suppose value not present means disable : A



Expected output:



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









share|improve this question



















  • 2





    Can you be more clear?

    – vivek_23
    Mar 8 at 9:30











  • @vivek_23,$topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-001). $topicAssingned under topics , topicID : T-100 is present , so "disable" : "D"

    – user9988771
    Mar 8 at 10:20











  • @vivek_23,$topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-002). $topicAssingned under topics , topicID : T-600 is not present , so "disable" : "A"

    – user9988771
    Mar 8 at 10:20













1












1








1








I am having three arrays



  1. topicsSelected

  2. relavantGroups

  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 array values at least one value should present $topicAssingned means based on groupID, i have to push one value to $relavantGroups like disable : D suppose value not present means disable : A



Expected output:



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









share|improve this question
















I am having three arrays



  1. topicsSelected

  2. relavantGroups

  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 array values at least one value should present $topicAssingned means based on groupID, i have to push one value to $relavantGroups like disable : D suppose value not present means disable : A



Expected output:



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






php php-5.6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 12:54









vivek_23

3,0502622




3,0502622










asked Mar 8 at 9:21







user9988771














  • 2





    Can you be more clear?

    – vivek_23
    Mar 8 at 9:30











  • @vivek_23,$topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-001). $topicAssingned under topics , topicID : T-100 is present , so "disable" : "D"

    – user9988771
    Mar 8 at 10:20











  • @vivek_23,$topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-002). $topicAssingned under topics , topicID : T-600 is not present , so "disable" : "A"

    – user9988771
    Mar 8 at 10:20












  • 2





    Can you be more clear?

    – vivek_23
    Mar 8 at 9:30











  • @vivek_23,$topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-001). $topicAssingned under topics , topicID : T-100 is present , so "disable" : "D"

    – user9988771
    Mar 8 at 10:20











  • @vivek_23,$topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-002). $topicAssingned under topics , topicID : T-600 is not present , so "disable" : "A"

    – user9988771
    Mar 8 at 10:20







2




2





Can you be more clear?

– vivek_23
Mar 8 at 9:30





Can you be more clear?

– vivek_23
Mar 8 at 9:30













@vivek_23,$topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-001). $topicAssingned under topics , topicID : T-100 is present , so "disable" : "D"

– user9988771
Mar 8 at 10:20





@vivek_23,$topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-001). $topicAssingned under topics , topicID : T-100 is present , so "disable" : "D"

– user9988771
Mar 8 at 10:20













@vivek_23,$topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-002). $topicAssingned under topics , topicID : T-600 is not present , so "disable" : "A"

– user9988771
Mar 8 at 10:20





@vivek_23,$topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-002). $topicAssingned under topics , topicID : T-600 is not present , so "disable" : "A"

– user9988771
Mar 8 at 10:20












2 Answers
2






active

oldest

votes


















0














<?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);


Output:



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
)

)


  • First, make a map(associative array) of values of $topicsSelected. Same goes for $relavantGroups. This is to make the check more efficient. See more on Hash Table.


  • Now, iterate over $topicAssigned and then iterate over each group's topics inside it. Now, check if a topic exists inside $topicsSelected using a simple isset function. If yes, we disable them, else we don't.






share|improve this answer
































    0














    It's not very clear what you are asking and the code is a bit weird but I'll give it a try.
    First fix your array declaration - you should use => and not :;



    You have to Iterate over the $relavantGroups and for each element iterate the $topicAssingned array. Then perform a simple comparison to see if the group Id is present and you are done!



    Here is my solution (quick and dirty): 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);


    Updated the solution - note that I'm using in_array() to determine if the topicID is present. That mean that any value that is in the $topicsSelected array will affect the result.



    Hope I helped.



    This will output (based one your example):



    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"







    share|improve this answer

























    • $topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-001). $topicAssingned under topics , topicID : T-100 is present , so "disable" : "D".

      – user9988771
      Mar 8 at 10:18












    • $topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-002). $topicAssingned under topics , topicID : T-600 is not present , so "disable" : "A".

      – user9988771
      Mar 8 at 10:19











    • I don't get it... What happen if the both are present??? - and in your example "G-002"don't have neither one and still is D.

      – Shlomi Hassid
      Mar 8 at 10:22











    • Yes G-002 is don't have topicID so "disable" : "A", i has updated my expected output please check

      – user9988771
      Mar 8 at 10:24











    • if the both are present also "disable" : "D"

      – user9988771
      Mar 8 at 10:25











    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55060118%2fhow-to-push-the-condition-based-value-in-existing-array-in-php%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown
























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    <?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);


    Output:



    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
    )

    )


    • First, make a map(associative array) of values of $topicsSelected. Same goes for $relavantGroups. This is to make the check more efficient. See more on Hash Table.


    • Now, iterate over $topicAssigned and then iterate over each group's topics inside it. Now, check if a topic exists inside $topicsSelected using a simple isset function. If yes, we disable them, else we don't.






    share|improve this answer





























      0














      <?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);


      Output:



      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
      )

      )


      • First, make a map(associative array) of values of $topicsSelected. Same goes for $relavantGroups. This is to make the check more efficient. See more on Hash Table.


      • Now, iterate over $topicAssigned and then iterate over each group's topics inside it. Now, check if a topic exists inside $topicsSelected using a simple isset function. If yes, we disable them, else we don't.






      share|improve this answer



























        0












        0








        0







        <?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);


        Output:



        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
        )

        )


        • First, make a map(associative array) of values of $topicsSelected. Same goes for $relavantGroups. This is to make the check more efficient. See more on Hash Table.


        • Now, iterate over $topicAssigned and then iterate over each group's topics inside it. Now, check if a topic exists inside $topicsSelected using a simple isset function. If yes, we disable them, else we don't.






        share|improve this answer















        <?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);


        Output:



        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
        )

        )


        • First, make a map(associative array) of values of $topicsSelected. Same goes for $relavantGroups. This is to make the check more efficient. See more on Hash Table.


        • Now, iterate over $topicAssigned and then iterate over each group's topics inside it. Now, check if a topic exists inside $topicsSelected using a simple isset function. If yes, we disable them, else we don't.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 8 at 13:16

























        answered Mar 8 at 13:09









        vivek_23vivek_23

        3,0502622




        3,0502622























            0














            It's not very clear what you are asking and the code is a bit weird but I'll give it a try.
            First fix your array declaration - you should use => and not :;



            You have to Iterate over the $relavantGroups and for each element iterate the $topicAssingned array. Then perform a simple comparison to see if the group Id is present and you are done!



            Here is my solution (quick and dirty): 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);


            Updated the solution - note that I'm using in_array() to determine if the topicID is present. That mean that any value that is in the $topicsSelected array will affect the result.



            Hope I helped.



            This will output (based one your example):



            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"







            share|improve this answer

























            • $topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-001). $topicAssingned under topics , topicID : T-100 is present , so "disable" : "D".

              – user9988771
              Mar 8 at 10:18












            • $topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-002). $topicAssingned under topics , topicID : T-600 is not present , so "disable" : "A".

              – user9988771
              Mar 8 at 10:19











            • I don't get it... What happen if the both are present??? - and in your example "G-002"don't have neither one and still is D.

              – Shlomi Hassid
              Mar 8 at 10:22











            • Yes G-002 is don't have topicID so "disable" : "A", i has updated my expected output please check

              – user9988771
              Mar 8 at 10:24











            • if the both are present also "disable" : "D"

              – user9988771
              Mar 8 at 10:25















            0














            It's not very clear what you are asking and the code is a bit weird but I'll give it a try.
            First fix your array declaration - you should use => and not :;



            You have to Iterate over the $relavantGroups and for each element iterate the $topicAssingned array. Then perform a simple comparison to see if the group Id is present and you are done!



            Here is my solution (quick and dirty): 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);


            Updated the solution - note that I'm using in_array() to determine if the topicID is present. That mean that any value that is in the $topicsSelected array will affect the result.



            Hope I helped.



            This will output (based one your example):



            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"







            share|improve this answer

























            • $topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-001). $topicAssingned under topics , topicID : T-100 is present , so "disable" : "D".

              – user9988771
              Mar 8 at 10:18












            • $topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-002). $topicAssingned under topics , topicID : T-600 is not present , so "disable" : "A".

              – user9988771
              Mar 8 at 10:19











            • I don't get it... What happen if the both are present??? - and in your example "G-002"don't have neither one and still is D.

              – Shlomi Hassid
              Mar 8 at 10:22











            • Yes G-002 is don't have topicID so "disable" : "A", i has updated my expected output please check

              – user9988771
              Mar 8 at 10:24











            • if the both are present also "disable" : "D"

              – user9988771
              Mar 8 at 10:25













            0












            0








            0







            It's not very clear what you are asking and the code is a bit weird but I'll give it a try.
            First fix your array declaration - you should use => and not :;



            You have to Iterate over the $relavantGroups and for each element iterate the $topicAssingned array. Then perform a simple comparison to see if the group Id is present and you are done!



            Here is my solution (quick and dirty): 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);


            Updated the solution - note that I'm using in_array() to determine if the topicID is present. That mean that any value that is in the $topicsSelected array will affect the result.



            Hope I helped.



            This will output (based one your example):



            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"







            share|improve this answer















            It's not very clear what you are asking and the code is a bit weird but I'll give it a try.
            First fix your array declaration - you should use => and not :;



            You have to Iterate over the $relavantGroups and for each element iterate the $topicAssingned array. Then perform a simple comparison to see if the group Id is present and you are done!



            Here is my solution (quick and dirty): 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);


            Updated the solution - note that I'm using in_array() to determine if the topicID is present. That mean that any value that is in the $topicsSelected array will affect the result.



            Hope I helped.



            This will output (based one your example):



            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"








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 8 at 13:43

























            answered Mar 8 at 10:16









            Shlomi HassidShlomi Hassid

            5,29322038




            5,29322038












            • $topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-001). $topicAssingned under topics , topicID : T-100 is present , so "disable" : "D".

              – user9988771
              Mar 8 at 10:18












            • $topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-002). $topicAssingned under topics , topicID : T-600 is not present , so "disable" : "A".

              – user9988771
              Mar 8 at 10:19











            • I don't get it... What happen if the both are present??? - and in your example "G-002"don't have neither one and still is D.

              – Shlomi Hassid
              Mar 8 at 10:22











            • Yes G-002 is don't have topicID so "disable" : "A", i has updated my expected output please check

              – user9988771
              Mar 8 at 10:24











            • if the both are present also "disable" : "D"

              – user9988771
              Mar 8 at 10:25

















            • $topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-001). $topicAssingned under topics , topicID : T-100 is present , so "disable" : "D".

              – user9988771
              Mar 8 at 10:18












            • $topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-002). $topicAssingned under topics , topicID : T-600 is not present , so "disable" : "A".

              – user9988771
              Mar 8 at 10:19











            • I don't get it... What happen if the both are present??? - and in your example "G-002"don't have neither one and still is D.

              – Shlomi Hassid
              Mar 8 at 10:22











            • Yes G-002 is don't have topicID so "disable" : "A", i has updated my expected output please check

              – user9988771
              Mar 8 at 10:24











            • if the both are present also "disable" : "D"

              – user9988771
              Mar 8 at 10:25
















            $topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-001). $topicAssingned under topics , topicID : T-100 is present , so "disable" : "D".

            – user9988771
            Mar 8 at 10:18






            $topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-001). $topicAssingned under topics , topicID : T-100 is present , so "disable" : "D".

            – user9988771
            Mar 8 at 10:18














            $topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-002). $topicAssingned under topics , topicID : T-600 is not present , so "disable" : "A".

            – user9988771
            Mar 8 at 10:19





            $topicsSelected array values(T-100 or T-600) at least one value should present in $topicAssingned array, based on groupID(G-002). $topicAssingned under topics , topicID : T-600 is not present , so "disable" : "A".

            – user9988771
            Mar 8 at 10:19













            I don't get it... What happen if the both are present??? - and in your example "G-002"don't have neither one and still is D.

            – Shlomi Hassid
            Mar 8 at 10:22





            I don't get it... What happen if the both are present??? - and in your example "G-002"don't have neither one and still is D.

            – Shlomi Hassid
            Mar 8 at 10:22













            Yes G-002 is don't have topicID so "disable" : "A", i has updated my expected output please check

            – user9988771
            Mar 8 at 10:24





            Yes G-002 is don't have topicID so "disable" : "A", i has updated my expected output please check

            – user9988771
            Mar 8 at 10:24













            if the both are present also "disable" : "D"

            – user9988771
            Mar 8 at 10:25





            if the both are present also "disable" : "D"

            – user9988771
            Mar 8 at 10:25

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55060118%2fhow-to-push-the-condition-based-value-in-existing-array-in-php%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

            Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

            How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page