重新排列数组(从键移动到值)并组织顺序

时间:2015-12-02 14:49:51

标签: php arrays multidimensional-array

我有这个数组:

Array
(
    [LLLLLLL:] => Array
        (
            [  brand1:] => Array
                (
                    [people: (100%)] => Array
                        (
                        )
                    [countries: (90%)] => Array
                        (
                        )
                    [yyyy: (80%)] => Array
                        (
                        )
                    [languages: (70%)] => Array
                        (
                        )
                )
            [  brand2:] => Array
                (
                    [people: (60%)] => Array
                        (
                        )
                    [countries: (50%)] => Array
                        (
                        )
                    [yyyy: (40%)] => Array
                        (
                        )
                    [languages: (30%)] => Array
                        (
                        )
                )
       )
)

如何才能重新安排我的阵列:

array(
        array(
          ('LLLLLLL') => 'people',
          ('BRAND1') => '100%',
          ('BRAND2')   => '60%'),
    array(
          ('LLLLLLL') => 'countries',
          ('BRAND1') => '90%',
          ('BRAND2')   => '50%')

    array(
          ('LLLLLLL') => 'yyyy',
          ('BRAND1') => '80%',
          ('BRAND2')   => '50%')
    array(
          ('LLLLLLL') => 'languages',
      ('BRAND1') => '70%',
      ('BRAND2')   => '30%',

我不知道,例如,如何在'之后移动字符串:'关键的价值,而不是重新安排在不同的顺序。怎么办呢?

2 个答案:

答案 0 :(得分:0)

我认为应该这样做。请注意,它未经过测试:

$result = array();

foreach ($initialArray as $lll => $brands) {
    $lll = rtrim($lll, ':');
    foreach ($brands as $brandName => $brandValues) {
        $brandName = strtoupper(preg_replace('/[\s:]/', '', $brandName));
        $values = array_keys($brandValues);
        foreach ($values as $value) {
            preg_match('/([a-z]*): \(([0-9]*%)\)/', $value, $matches);

            if (!isset($result[$matches[1]])) {
                $result[$matches[1]] = array($lll => $matches[1]);
            }
            $result[$matches[1]][$brandName] = $matches[2];
        }
    }
}

答案 1 :(得分:0)

你可以使用regexp + array_map。

$datos = Array(
        'LLLLLLL:' => Array(
                'brand1:' => Array(
                        'people: (100%)' => Array
                            (
                            ),
                        'countries: (90%)' => Array
                            (
                            ),
                        'yyyy: (80%)' => Array
                            (
                            ),
                        'languages: (70%)' => Array
                            (
                            )
                    ),
                'brand2:' => Array(
                        'people: (60%)' => Array
                            (
                            ),
                        'countries: (50%)' => Array
                            (
                            ),
                        'yyyy: (40%)' => Array
                            (
                            ),
                        'languages: (30%)' => Array
                            (
                            )
                    )
           )
    );

    $datos = array_map(function($row) {
        $ret = array();
        foreach ($row as $key => $items) {
            foreach ($items as $dato => $vacio) {
                if (preg_match('/([^:]+): \(([^\)]+)\)/i', $dato, $coincidencias)) {
                    $pos = $coincidencias[1];

                    if (!isset($ret[$pos]))
                        $ret[$pos] = array();

                    $ret[$pos][$key] = $coincidencias[2];
                }
            }
        }
        return $ret;
    }, $datos);

    echo '<pre>' . print_r($datos, true);

没有完成,但你可以这样做。