Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 重新排列数组(从键移动到值)并组织顺序_Php_Arrays_Multidimensional Array - Fatal编程技术网

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

Php 重新排列数组(从键移动到值)并组织顺序,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有这个阵列: Array ( [LLLLLLL:] => Array ( [ brand1:] => Array ( [people: (100%)] => Array ( ) [countries: (90%)] =&g

我有这个阵列:

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%',

例如,我不知道如何将键上“:”之后的字符串移动到值,然后按不同的顺序重新排列。如何做到这一点?

我认为这应该做到。请注意,未对其进行测试:

$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];
        }
    }
}

您可以使用regexp+array\u映射

$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);
$datos=数组(
'llll:'=>数组(
'brand1:'=>数组(
“人员:(100%)”=>数组
(
),
“国家:(90%)”=>数组
(
),
“yyyy:(80%)”=>数组
(
),
'语言:(70%)'=>数组
(
)
),
“brand2:”=>数组(
“人:(60%)”=>数组
(
),
“国家:(50%)”=>数组
(
),
“yyyy:(40%)”=>数组
(
),
'语言:(30%)'=>数组
(
)
)
)
);
$datos=数组映射(函数($row){
$ret=array();
foreach($key=>$items的行){
foreach($dato=>$vacio的项目){
if(preg_match('/([^:]+):\([^\]+)\)/i',$dato,$concurrencias)){
$pos=$concurrencias[1];
如果(!isset($ret[$pos]))
$ret[$pos]=数组();
$ret[$pos][$key]=$Concurrencias[2];
}
}
}
返回$ret;
},$datos);
回显“”。打印($datos,true);

未完成,但您可以这样做。

不太清楚“网络”、“品牌1”和其他名称来自何处…@MrHug,对不起,我有一个错误,已经编辑过了