重新创建一个数组,以便我得到我想要的结果

时间:2016-03-05 18:20:55

标签: php arrays

以下是示例数组:

 $arr = array(array (
  'country' => 
  array (
    'code' => 'in',
    'name' => 'India',
  ),
  'language' => 
  array (
    'code' => 'en',
    'name' => 'English',
  ),
),
array (
  'country' => 
  array (
    'code' => 'in',
    'name' => 'India',
  ),
  'language' => 
  array (
    'code' => 'hi',
    'name' => 'Hindi',
  ),
),

array (
  'country' => 
  array (
    'code' => 'id',
    'name' => 'Indonesia',
  ),
  'language' => 
  array (
    'code' => 'id',
    'name' => 'Indonesian',
  ),
),

array (
  'country' => 
  array (
    'code' => 'it',
    'name' => 'Italy',
  ),
  'language' => 
  array (
    'code' => 'it',
    'name' => 'Italian',
  ),
),

array (
  'country' => 
  array (
    'code' => 'jp',
    'name' => 'Japan',
  ),
  'language' => 
  array (
    'code' => 'ja',
    'name' => 'Japanese',
  ),
),

array (
  'country' => 
  array (
    'code' => 'mx',
    'name' => 'Mexico',
  ),
  'language' => 
  array (
    'code' => 'es',
    'name' => 'Spanish; Castilian',
  ),
),

array (
  'country' => 
  array (
    'code' => 'nl',
    'name' => 'Netherlands',
  ),
  'language' => 
  array (
    'code' => 'nl',
    'name' => 'Dutch',
  ),
),

array (
  'country' => 
  array (
    'code' => 'ng',
    'name' => 'Nigeria',
  ),
  'language' => 
  array (
    'code' => 'en',
    'name' => 'English',
  ),
),

array (
  'country' => 
  array (
    'code' => 'no',
    'name' => 'Norway',
  ),
  'language' => 
  array (
    'code' => 'no',
    'name' => 'Norwegian',
  ),
),

array (
  'country' => 
  array (
    'code' => 'pk',
    'name' => 'Pakistan',
  ),
  'language' => 
  array (
    'code' => 'en',
    'name' => 'English',
  ),
),

array (
  'country' => 
  array (
    'code' => 'ph',
    'name' => 'Philippines',
  ),
  'language' => 
  array (
    'code' => 'en',
    'name' => 'English',
  ),
));

您可能已经注意到,在上面的数组中,印度被提及了两次,现在,我想要的是如果两个或多个数组包含相同的国家/地区,那么包含相同国家/地区的所有数组都将成为这样的一个数组:

 $arr = array (array (
  'country' => 
  array (
    'code' => 'in',
    'name' => 'India',
  ),
  'language' => array(
  array (
    'code' => 'en',
    'name' => 'English',
  ),
    array (
    'code' => 'hi',
    'name' => 'Hindi',
  ),
)
)
);

其余阵列保持不变。

2 个答案:

答案 0 :(得分:0)

这样的事情可能起作用:

<?php
    $Data = array (
        array (
            'country' => array (
                'code' => 'in',
                'name' => 'India'
            ),

            'language' => array (
                'code' => 'en',
                'name' => 'English'
            )
        ),

        array (
            'country' => array (
                'code' => 'in',
                'name' => 'India'
            ),

            'language' => array (
                'code' => 'hi',
                'name' => 'Hindu'
            )
        ),

        array (
            'country' => array (
                'code' => 'id',
                'name' => 'Indonesia'
            ),

            'language' => array (
                'code' => 'id',
                'name' => 'Indonesian'
            )
        ),

        array (
            'country' => array (
                'code' => 'it',
                'name' => 'Italy',
            ),

            'language' => array (
                'code' => 'it',
                'name' => 'Italian'
            )
        ),

        array (
            'country' => array (
                'code' => 'jp',
                'name' => 'Japan'
            ),

            'language' => array (
                'code' => 'ja',
                'name' => 'Japanese'
            )
        ),

        array (
            'country' => array (
                'code' => 'mx',
                'name' => 'Mexico'
            ),

            'language' => array (
                'code' => 'es',
                'name' => 'Spanish; Castilian'
            )
        ),

        array (
            'country' => array (
                'code' => 'nl',
                'name' => 'Netherlands'
            ),

            'language' => array (
                'code' => 'nl',
                'name' => 'Dutch'
            )
        ),

        array (
            'country' => array (
                'code' => 'ng',
                'name' => 'Nigeria'
            ),

            'language' => array (
                'code' => 'en',
                'name' => 'English'
            )
        ),

        array (
            'country' => array (
                'code' => 'no',
                'name' => 'Norway'
            ),

            'language' => array (
                'code' => 'no',
                'name' => 'Norwegian'
            )
        ),

        array (
            'country' => array (
                'code' => 'pk',
                'name' => 'Pakistan'
            ),

            'language' => array (
                'code' => 'en',
                'name' => 'English'
            )
        ),

        array (
            'country' => array (
                'code' => 'ph',
                'name' => 'Philippines'
            ),

            'language' => array (
                'code' => 'en',
                'name' => 'English'
            )
        ),

        array (
            'country' => array (
                'code' => 'nl',
                'name' => 'Netherlands'
            ),

            'language' => array (
                'code' => 'en',
                'name' => 'English'
            )
        )
    );

    function ProcessData ($Data) {
        $Res = array ();

        $m = array ();
        foreach ($Data as $k => $v) {
            $Country = $v ['country'];

            if (in_array ($Country ['name'], $m)) {
                continue;   
            }

            $Lang = array ();
            $Lang [] = $v ['language'];

            for ($i = $k + 1, $l = count ($Data); $i < $l; $i++) {
                if ($Data [$i] ['country'] ['name'] === $Country ['name']) {
                    $Lang [] = $Data [$i] ['language'];   
                }
            }

            $m [] = $Country ['name'];

            $Res [] = array (
                'country' => $v ['country'] ['name'],
                'language' => $Lang
            );
        }

        return $Res;
    }

    print_r (ProcessData ($Data));

?>

,并提供:

Array
(
    [0] => Array
        (
            [country] => India
            [language] => Array
                (
                    [0] => Array
                        (
                            [code] => en
                            [name] => English
                        )

                    [1] => Array
                        (
                            [code] => hi
                            [name] => Hindu
                        )

                )

        )

    [1] => Array
        (
            [country] => Indonesia
            [language] => Array
                (
                    [0] => Array
                        (
                            [code] => id
                            [name] => Indonesian
                        )

                )

        )

    [2] => Array
        (
            [country] => Italy
            [language] => Array
                (
                    [0] => Array
                        (
                            [code] => it
                            [name] => Italian
                        )

                )

        )

    [3] => Array
        (
            [country] => Japan
            [language] => Array
                (
                    [0] => Array
                        (
                            [code] => ja
                            [name] => Japanese
                        )

                )

        )

    [4] => Array
        (
            [country] => Mexico
            [language] => Array
                (
                    [0] => Array
                        (
                            [code] => es
                            [name] => Spanish; Castilian
                        )

                )

        )

    [5] => Array
        (
            [country] => Netherlands
            [language] => Array
                (
                    [0] => Array
                        (
                            [code] => nl
                            [name] => Dutch
                        )

                    [1] => Array
                        (
                            [code] => en
                            [name] => English
                        )

                )

        )

    [6] => Array
        (
            [country] => Nigeria
            [language] => Array
                (
                    [0] => Array
                        (
                            [code] => en
                            [name] => English
                        )

                )

        )

    [7] => Array
        (
            [country] => Norway
            [language] => Array
                (
                    [0] => Array
                        (
                            [code] => no
                            [name] => Norwegian
                        )

                )

        )

    [8] => Array
        (
            [country] => Pakistan
            [language] => Array
                (
                    [0] => Array
                        (
                            [code] => en
                            [name] => English
                        )

                )

        )

    [9] => Array
        (
            [country] => Philippines
            [language] => Array
                (
                    [0] => Array
                        (
                            [code] => en
                            [name] => English
                        )

                )

        )

)

答案 1 :(得分:0)

我找到了另外一个解决方案,但@anders代码也正常运行!

$res = array();

foreach($arr as $val)
{
    if(!isset($res[$val['country']['name']]))

   $res[$val['country']['name']] = array('code'=>$val['country']['code'], 'name'=>$val['country']['name'], 'language'=>array());

    $res[$val['country']['name']]['language'][] = array('code'=>$val['language']['code'], 'name'=>$val['language']['name']);
}
echo "<pre>";
print_r(var_export(array_values($res)));
echo "</pre>";

Here is a demo of working example.