-2

remove entire stdclass object based if condition php

i have an stdObject

Array
(
      [0] => stdClass Object
        (
            [id] => 66159
            [test_ids] => 32489,32490
            [special_test_ids] => 33290,47516,56835,68201,68230
            [total_test] => 7
            [entry_done] => 1
            [approval_done] => 1
        )

      [1] => stdClass Object
        (
            [id] => 66160
            [test_ids] => 32489,32490
            [special_test_ids] => 33290,47516,56835
            [total_test] => 5
            [entry_done] => 5
            [approval_done] => 5
        )

)

i am trying to unset whole key where condition met

here is my code so far

$newArray = array();
    
    foreach ($result_array as $key => $value) {

          if ($value->entry_done == $value->total_test) {
                $value->unset($key);
          }

          $result[]=$value;

    }

Expected Result =

$NewArray
(
      [0] => stdClass Object
        (
            [id] => 66159
            [test_ids] => 32489,32490
            [special_test_ids] => 33290,47516,56835,68201,68230
            [total_test] => 7
            [entry_done] => 1
            [approval_done] => 1
        )    
)

when i am executing i am getting an php error please correct me where i am doing wrong thanks

273K
  • 29,503
  • 10
  • 41
  • 64
uzthegeek
  • 43
  • 6
  • 1
    `array_filter` invented already. – u_mulder Aug 18 '23 at 08:08
  • but how to use it can you please rewrite the code? – uzthegeek Aug 18 '23 at 08:11
  • _"when i am executing i am getting an php error"_ - instead of giving us a statement containing so little actually _useful_ information, please quote error messages verbatim. – CBroe Aug 18 '23 at 08:45
  • `$value->unset($key);` makes no sense. First of all, a StdClass instance does not _have_ an `unset` method. And second, you do not want to unset anything inside that StdClass instance to begin with - you want to unset an element from your array that you are looping over. `unset($result_array[$key]);` – CBroe Aug 18 '23 at 08:49
  • How is the C++ tag [tag:std] relevant to PHP? – 273K Aug 18 '23 at 17:12

0 Answers0