0

I am trying to remove an object from JSON array but is having a few issues. here is what I have right now.

{  
  "value":"In-plop",
  "no_delete":true,
  "disabled":null,
  "resource":"e48f8f",
  "email":"1"
 }
]';



foreach ($status as $key => $value) {
   if (in_array('Dispatched', $value)) {
    unset($status[$key]);
   }
  }
$status = json_encode($status);

echo $status;

I am trying to remove this entire object. I know my current code will only remove the value of the single object, but this is where I am stuck. The problem is that the object position is not static, it can be anywhere in the array. The only thing that is static is value:Dispatched. Any suggestions?

{  
  "value":"opuy",
  "no_delete":true,
  "disabled":null,
  "resource":"a382d1",
  "email":"1"
},

3 Answers 3

4

You should convert it first, remove the element and then re-encode.

$json = json_decode($status, true); //return an array
foreach($json as $key => $value) {
   if($value['value'] == 'Dispatched') {
    unset($json[$key]);
   }
}
$status = json_encode($json);
Sign up to request clarification or add additional context in comments.

3 Comments

awesome man, i actually have json decode in my actual code but i guess when i made is shorter here i forgot to put that in. Thanks for the help. ,much appreciated.
@samjones39: This is a good answer, but does it tell you why your code doesn't work?
@abraCadaver, hey thanks for taking your time and explaining in your comments. i realize why my original coded did not work. your answer is good too. since i cannot accept both, i decided to accept the first response.
3

Since you have boolean true values in the array (that will match a type juggled true value such as string "Dispatched"), you need to pass true as the third parameter to in_array() for strict comparison.

Assuming you have run json_decode() and passed true for an array, just use strict comparison in in_array():

   if (in_array('Dispatched', $value, true)) {
    unset($status[$key]);
   }

In this case, knowing the key I personally would use:

   if ($value['value'] === 'Dispatched') {
    unset($status[$key]);
   }

1 Comment

awesome man, i actually have json decode in my actual code but i guess when i made is shorter here i forgot to put that in. Thanks for the help. ,much appreciated
3

First you have to parse your json using json_decode, after that remove unused elements and on the end use json_encode to convert the array to string.

This code works:

$status='[  
{ 


  "value":"New",
  "no_delete":true,
  "disabled":null,
  "resource":"4eb2df",
  "email":null
},
{  
  "value":"Assigned",
  "no_delete":true,
  "disabled":null,
  "resource":"4c85b6",
  "email":1
},
{  
  "value":"Dispatched",
  "no_delete":true,
  "disabled":null,
  "resource":"a382d1",
  "email":"1"
},
{  
  "value":"Scheduled",
  "no_delete":true,
  "disabled":null,
  "resource":"75b4eb",
  "email":"1"
},
{  
  "value":"In-Progress",
  "no_delete":true,
  "disabled":null,
  "resource":"e48f8f",
  "email":"1"
 }
]';

$json = json_decode($status);
$result = [];

foreach($json as $key => $value) {
        if($value->value != "Dispatched") {
                $result[] = $value;
        }
}

print_r(json_encode($result));

1 Comment

awesome man, i actually have json decode in my actual code but i guess when i made is shorter here i forgot to put that in. Thanks for the help. ,much appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.