简体   繁体   English

如何在PHP中删除多维数组中的特定层?

[英]How to remove specific layer in multi-dimensional array in PHP?

I have a multidimensional array where I would like to remove a specific layer of the data.我有一个多维数组,我想在其中删除特定的数据层。 Basically, I would like to remove all the labels that are numeric aka the [0] => Array, [1] => Array, [2] => Array, and [3] => Array.基本上,我想删除所有数字标签,即 [0] => Array、[1] => Array、[2] => Array 和 [3] => Array。

Here's the array I have currently:这是我目前拥有的数组:

Array
(
    [0] => Array
        (
            [CN=Abraham Lincoln,CN=Users,DC=test,DC=io] => Array
                (
                    [objectclass] => Array
                        (
                            [0] => top
                            [1] => person
                            [2] => organizationalPerson
                            [3] => user
                        )
                    [cn] => Abraham Lincoln
                )
        )

    [1] => Array
        (
            [CN=Administrator,CN=Users,DC=test,DC=io] => Array
                (
                    [objectclass] => Array
                        (
                            [0] => top
                            [1] => person
                            [2] => organizationalPerson
                            [3] => user
                        )
                    [distinguishedname] => CN=Administrator,CN=Users,DC=test,DC=io
                )

        )

    [2] => Array
        (
            [CN=CloudNCUsers,CN=Users,DC=test,DC=io] => Array
                (
                    [objectclass] => Array
                        (
                            [0] => top
                            [1] => group
                        )
                    [distinguishedname] => CN=CloudNCUsers,CN=Users,DC=test,DC=io
                )

        )

    [3] => Array
        (
            [CN=Jill Dope,CN=Users,DC=test,DC=io] => Array
                (
                    [objectclass] => Array
                        (
                            [0] => top
                            [1] => person
                            [2] => organizationalPerson
                            [3] => user
                        )
                    [distinguishedname] => CN=Jill Dope,CN=Users,DC=test,DC=io
                )

        )
)

Here's the array I need:这是我需要的数组:

Array
(
    [CN=Abraham Lincoln,CN=Users,DC=test,DC=io] => Array
        (
            [objectclass] => Array
                (
                    [0] => top
                    [1] => person
                    [2] => organizationalPerson
                    [3] => user
                )
            [cn] => Abraham Lincoln
            )
       )

    [CN=Administrator,CN=Users,DC=test,DC=io] => Array
        (
            [objectclass] => Array
                (
                    [0] => top
                    [1] => person
                    [2] => organizationalPerson
                    [3] => user
                )
                [distinguishedname] => CN=Administrator,CN=Users,DC=test,DC=io
         )


    [CN=CloudNCUsers,CN=Users,DC=test,DC=io] => Array
        (
            [objectclass] => Array
                (
                    [0] => top
                    [1] => group
                )
        )

    [CN=Jill Dope,CN=Users,DC=test,DC=io] => Array
        (
            [objectclass] => Array
                (
                    [0] => top
                    [1] => person
                    [2] => organizationalPerson
                    [3] => user
                )
             [distinguishedname] => CN=Jill Dope,CN=Users,DC=test,DC=io
        )
)

Any help would be appreciated, thanks!任何帮助将不胜感激,谢谢!

You can remove a level from the array using key and reset and saving to a new array:您可以使用key从数组中删除级别并reset并保存到新数组:

$result = array();
foreach ($entries as $entry) {
    $result[key($entry)] = reset($entry);
}

Demo on 3v4l.org 3v4l.org 上的演示

You should be able to do the same thing when you initially load the array to save this extra step:当您最初加载数组时,您应该能够做同样的事情以节省这个额外的步骤:

$entry = ldap_get_entries($ldapconnection, $result);
$array[key($entry)] = reset($entry);

Note that this assumes that all of the key values ( CN=Abraham Lincoln,CN=Users,DC=test,DC=io etc.) are unique.请注意,这假设所有键值( CN=Abraham Lincoln,CN=Users,DC=test,DC=io等)都是唯一的。 If they are not, later values in the array will overwrite earlier ones in the output.如果不是,则数组中较晚的值将覆盖输出中较早的值。 Note also that if ldap_get_entries returns more than one value, this will remove all but the first.另请注意,如果ldap_get_entries返回多个值,这将删除除第一个之外的所有值。 However based on your existing code and the sample data you have provided, it would appear that this is not the case.但是,根据您现有的代码和您提供的示例数据,情况似乎并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM