如何从PHP中删除数组中的元素

时间:2014-04-30 08:11:36

标签: php arrays foreach strpos

我有多个来自foreach循环的数组。 (有关详细信息,请参阅我的上一个问题:Array values in function

就在这里:

Array
(
    [count] => 1
    [0] => Array
        (
            [distinguishedname] => Array
                (
                    [count] => 1
                    [0] => CN=Beheergroep,OU=Test,DC=stefan,DC=nl
                )

            [0] => distinguishedname
            [count] => 1
            [dn] => CN=Beheergroep,OU=Test,DC=stefan,DC=nl
        )

)
Array
(
    [count] => 1
    [0] => Array
        (
            [distinguishedname] => Array
                (
                    [count] => 1
                    [0] => CN=Belangrijke Groep,OU=Test,DC=stefan,DC=nl
                )

            [0] => distinguishedname
            [count] => 1
            [dn] => CN=Belangrijke Groep,OU=Test,DC=stefan,DC=nl
        )

)
Array
(
    [count] => 1
    [0] => Array
        (
            [distinguishedname] => Array
                (
                    [count] => 1
                    [0] => CN=Domeingebruikers,CN=Users,DC=stefan,DC=nl
                )

            [0] => distinguishedname
            [count] => 1
            [dn] => CN=Domeingebruikers,CN=Users,DC=stefan,DC=nl
        )

)
Array
(
    [count] => 1
    [0] => Array
        (
            [distinguishedname] => Array
                (
                    [count] => 1
                    [0] => CN=Gebruikers,CN=Builtin,DC=stefan,DC=nl
                )

            [0] => distinguishedname
            [count] => 1
            [dn] => CN=Gebruikers,CN=Builtin,DC=stefan,DC=nl
        )

)
Array
(
    [count] => 1
    [0] => Array
        (
            [distinguishedname] => Array
                (
                    [count] => 1
                    [0] => CN=Hoofdgroep,OU=Test,DC=stefan,DC=nl
                )

            [0] => distinguishedname
            [count] => 1
            [dn] => CN=Hoofdgroep,OU=Test,DC=stefan,DC=nl
        )

)
Array
(
    [count] => 1
    [0] => Array
        (
            [distinguishedname] => Array
                (
                    [count] => 1
                    [0] => CN=Mailgroep,OU=Test,DC=stefan,DC=nl
                )

            [0] => distinguishedname
            [count] => 1
            [dn] => CN=Mailgroep,OU=Test,DC=stefan,DC=nl
        )

)
Array
(
    [count] => 1
    [0] => Array
        (
            [distinguishedname] => Array
                (
                    [count] => 1
                    [0] => CN=Securitygroep,OU=Securitygroups,DC=stefan,DC=nl
                )

            [0] => distinguishedname
            [count] => 1
            [dn] => CN=Securitygroep,OU=Securitygroups,DC=stefan,DC=nl
        )

)
Array
(
    [count] => 1
    [0] => Array
        (
            [distinguishedname] => Array
                (
                    [count] => 1
                    [0] => CN=Testgroep2,OU=Test,DC=stefan,DC=nl
                )

            [0] => distinguishedname
            [count] => 1
            [dn] => CN=Testgroep2,OU=Test,DC=stefan,DC=nl
        )

)

现在我想检查所有数组的特定值。

我使用此功能检查数组。

if(strpos($check[0]['distinguishedname'][0], 'OU=Test') !== false) {

如果其中一个数组包含特定值(" OU = Test")则需要显示,否则需要排除或隐藏数组。我怎么能做到这一点?

修改

这是我的foreach循环代码。

$result = $adldap->user()->groups('stefan.guth');

for ($i = 0; $i < count($result); $i++) {
    sort($result);
}
print_r($result);

foreach ($result as $value) {
    $check = $adldap->group()->info($value, array(
        'distinguishedname'
    ));
    print_r($check);

    if (strpos($check[0]['distinguishedname'][0], 'OU=Test') === false) {

    }
}

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?你应该改变foreach循环..

foreach ($result as $key => $value) {
    $check = $adldap->group()->info($value, array(
        'distinguishedname'
    ));
    print_r($check);

    if (strpos($check[0]['distinguishedname'][0], 'OU=Test') === false) {
        unset($result[$key]);
    }
}