1

I need to unset the [device_id] element from the every array. I tried with recursive. But i am not able to do this. Can any one help on this. I tried like this.

foreach($tree as $key) {
    foreach($key as $value => $k){
        if ($value == 'device_id'){
            unset($k['device_id']);
        }
    }   
}

I have nested multidimensional array as below:

Array
(
    [4] => Array
        (
            [device_id] => 4
            [device_parent_id] => 2
            [device_ext] => 20122
            [device_name] => section controller1
            [username] => sectioncontroller1
            [password] => 90714771f0abd448f208e1aff5b662fc
            [device_macaddress] => 
            [device_ip] => 
            [device_desc] => 
            [admin_id] => 2
            [children] => Array
                (
                    [5] => Array
                        (
                            [device_id] => 5
                            [device_parent_id] => 4
                            [device_ext] => 20199
                            [device_name] => Emergency1
                            [username] => Emergency1
                            [password] => bb75df6eb4fdbf5753640a34674ecce1
                            [device_macaddress] => 
                            [device_ip] => 
                            [device_desc] => 
                            [admin_id] => 2
                            [children] => Array
                                (
                                )
                        )
                    [6] => Array
                        (
                            [device_id] => 6
                            [device_parent_id] => 4
                            [device_ext] => 20111
                            [device_name] => HQ Phone
                            [username] => HQPhone
                            [password] => 2f89d97f24539dc50ddd8fc53667b194
                            [device_macaddress] => 
                            [device_ip] => 
                            [device_desc] => 
                            [admin_id] => 2
                            [children] => Array
                                (
                                )
                        )
                    [9] => Array
                        (
                            [device_id] => 9
                            [device_parent_id] => 4
                            [device_ext] => 20144
                            [device_name] => Other controller1
                            [username] => Othercontroller1
                            [password] => 861426b485e2afd62ad0d914f524f23c
                            [device_macaddress] => 
                            [device_ip] => 
                            [device_desc] => 
                            [admin_id] => 2
                            [children] => Array
                                (
                                    [10] => Array
                                        (
                                            [device_id] => 10
                                            [device_parent_id] => 9
                                            [device_ext] => 30122
                                            [device_name] => Way Station1
                                            [username] => WayStation1
                                            [password] => 085d397b0392d3a325216a716c757113
                                            [device_macaddress] => 
                                            [device_ip] => 
                                            [device_desc] => 
                                            [admin_id] => 2
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )
                )
        )

    [21] => Array
        (
            [device_id] => 21
            [device_parent_id] => 2
            [device_ext] => 1123299
            [device_name] => sectioncontroller99
            [username] => sectioncontroller3
            [password] => 73d25d443117f76c6fe6bd5a4679f458
            [device_macaddress] => 124511251419
            [device_ip] => 124.235.63.569
            [device_desc] => test section9
            [admin_id] => 0
            [children] => Array
                (
                    [22] => Array
                        (
                            [device_id] => 22
                            [device_parent_id] => 21
                            [device_ext] => 9999
                            [device_name] => emergancy
                            [username] => emergancy
                            [password] => c2bc24d3d41ad9ade0171c3d5d6f59c6
                            [device_macaddress] => 1245122
                            [device_ip] => 124.235.235
                            [device_desc] => sdfdf sdfsdf sdfsd
                            [admin_id] => 0
                            [children] => Array
                                (
                                )
                        )
                )
        )
);
2
  • What do you want to unset? Commented Sep 27, 2016 at 8:16
  • [device_id] from every array and nested array Commented Sep 27, 2016 at 8:17

2 Answers 2

1

You need a recursive function for this. This should work:

function deleteDeviceId(&$tree) {
    foreach ($tree as $k => &$item) {
        unset($item['device_id']);
        deleteDeviceId($item['children']);
    }
}

deleteDeviceId($tree);
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that the variables in the loop are independant variables, changing them does not affect the original array.

To solve that you can either:

  • Use the complete path in $tree to unset your variable there directly:
    unset($tree[$outer_key][$inner_key]);. You would need the key in the outer loop as well.
    Note that you seem to be using key and value the wrong way around in your foreach loop but that's just the name...
  • Pass the loop variables by reference; when they reference the original variable, any change will be a change in the original variable as well.

Comments

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.