You are viewing our Forum Archives. To view or take place in current topics click here.
PHP | Remove properties from nested objects
Posted:
PHP | Remove properties from nested objectsPosted:
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,743
Reputation Power: 449
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,743
Reputation Power: 449
I have an dynamic array of objects like so. (pseudo code)
How can I remove all the id properties from the collection? Since the depth of the object is dynamic, I can't make a bunch of foreach loops.
I want to do something like this.
This is the result I want.
Last edited by CriticaI ; edited 1 time in total
How can I remove all the id properties from the collection? Since the depth of the object is dynamic, I can't make a bunch of foreach loops.
I want to do something like this.
$nested->removeAll('id');
//or
removeAll($nested, 'id');
This is the result I want.
Last edited by CriticaI ; edited 1 time in total
#2. Posted:
Status: Offline
Joined: Jun 11, 200914Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Status: Offline
Joined: Jun 11, 200914Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
#3. Posted:
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,743
Reputation Power: 449
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,743
Reputation Power: 449
speed wrote [ Register or Signin to view external links. ]
Get the key from that, then use unset to remove it.
Edit: this may not work since you want to search for keys, not values. Give it a shot but don't expect anything :p
Yeah the values are unknown so i don't think this will work.
- 0useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
<?php
$array = [
"id" => 1,
"user" => "John",
"roles" => [
"id" => 1,
"name" => "admin",
"permissions" => [
[
"id" => 1,
"name" => "can edit"
],
[
"id" => 1,
"name" => "can delete"
]
]
]
];
function deleteKeys(&$array){
foreach($array as $key => $value){
if($key === "id"){
unset($array[$key]);
}else if(is_array($value)){
$array[$key] = deleteKeys($value);
}
}
return $array;
}
var_dump(deleteKeys($array));
This seems to work fine. The method is to simply traverse through the nested structure and remove any occurrence of the id key.
I have uploaded a demo online [ Register or Signin to view external links. ]
- 0useful
- 0not useful
#5. Posted:
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
If you can't remove the ID directly, perhaps adding all of the data into a JSON Array would allow you to target the ID and remove it using unset()
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.