2

I have the following array, and I want to be able to delete all the "phonenumber" keys and of course its value from the JSON objects. Only the keys "phonenumber" an not the whole object. How can I do that?

[
    {
        "role": "admin",
        "id": "59df4ef2d8d39",
        "email": "[email protected]",
        "name": "A",
        "lastname": "A",
        "password": "1",
        "image": "img_webshop\/userimage-59dfb91515810.png"
    },
    {
        "role": "user",
        "id": "59df4f1b070e6",
        "phonenumber": "12345678",
        "name": "B",
        "lastname": "B",
        "password": "2",
        "image": "img_webshop\/userimage-59e37de69475b.png"
    },
    {
        "role": "user",
        "id": "59dfc0cb07985",
        "email": "[email protected]",
        "name": "C",
        "lastname": "C",
        "password": "3",
        "image": "img_webshop\/userimage-59dfc0cb06c5f.png"
    },
    {
        "role": "user",
        "id": "59dfc22f26f78",
        "phonenumber": "87654321",
        "name": "D",
        "lastname": "D",
        "password": "4",
        "image": "img_webshop\/userimage-59dfc22f2638d.png"
    },
    {
        "role": "user",
        "id": "59dfc460b261e",
        "email": "[email protected]",
        "name": "E",
        "lastname": "E",
        "password": "5",
        "image": "img_webshop\/userimage-59dfc460af866.png"
    }
]
0

2 Answers 2

6
// make array
$array = json_decode($your_json_string, true);

// loop through array
foreach($array as $key => $item){
       // unset them
       unset($array[$key]["phonenumber"]);
}

// make json again
$json_string_modified = json_encode($array);

OR using reference

// make array
$array = json_decode($your_json_string, true);

// loop through array using reference
foreach($array as &$item){

      // unset specific key
      unset($item["phonenumber"]);

}

// unset reference
unset($item);

// make json again
// you may remove JSON_PRETTY_PRINT flag, I kept it just to see o/p
$json_string_modified = json_encode($array,JSON_PRETTY_PRINT);
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think you need the isset test. If it's not there, unset doesn't error.
1
$jsonArray=json_decode($data);
//Remove unvanted props
foreach ($jsonArray as $key=>$row) {

   foreach ($row as $prop=>$field) {
       if ($prop != 'phonenumber')
           $newArray[$key][$prop] = $field;
       }

}

 $jsonArray=json_encode($newArray);

3 Comments

This uses potentially lots of memory to make a clone array and is slower than @3161993
yeah you are right but some php versions not remove object property .if this property protected then you cant make any changes
You're dealing with arrays here, not objects.

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.