即使我打了一张支票,in_array仍然获得了公司ID

时间:2012-05-10 18:28:45

标签: php cakephp cakephp-1.3 cakephp-2.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
        )

)

你可以看到该公司已被重复

1 个答案:

答案 0 :(得分: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'];
        }
   }