0

私は以下の配列のために次のコードを持っています

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



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

私がやろうとしているのは、会社IDを1つ入力するだけなので、その下にユーザーを追加できますが、何らかの理由で機能しません。

これが私の配列です

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 に答える