0

我对下面的数组有以下代码

$arrCompany = array();
foreach($users as $k => $user){



    if(!in_array($user['User']['company_id'], $arrCompany)){
        $arrCompany['company'][] = $user['User']['company_id'];
        }else{}
        }

我想要做的只是有一个公司ID,所以我可以在它下面添加用户,但由于某些原因它不起作用

这是我的数组

Array
(
    [0] => Array
        (
            [User] => Array
                (
                    [id] => 1
                    [company_id] => 20
                    [type] => 
                )


        )

    [1] => Array
        (
            [User] => Array
                (
                    [id] => 6
                    [company_id] => 21
                    [type] => 
                )


        )

    [2] => Array
        (
            [User] => Array
                (
                    [id] => 7
                    [company_id] => 22
                    [type] => 
                )


        )

    [3] => Array
        (
            [User] => Array
                (
                    [id] => 14
                    [company_id] => 21
                    [type] => 
                )


        )

    [4] => Array
        (
            [User] => Array
                (
                    [id] => 15
                    [company_id] => 22
                    [type] => 
                )



        )

    [5] => Array
        (
            [User] => Array
                (
                    [id] => 16
                    [company_id] => 21
                    [type] => 
                )


                )

        )

)

当我做 var_dump ......我明白了

Array
(
    [company] => Array
        (
            [0] => 20
            [1] => 21
            [2] => 22
            [3] => 21
            [4] => 22
            [5] => 21
        )

)

你可以看到公司已经重复

4

1 回答 1

1
$arrCompany = array('company'=>array());

foreach($users as $k => $user){
    if(!in_array($user['User']['company_id'], $arrCompany['company'])){
        $arrCompany['company'][] = $user['User']['company_id'];
        }
   }
于 2012-05-10T18:33:45.343 回答