将多维数组转换为简单数组

时间:2016-10-13 19:50:06

标签: php arrays multidimensional-array

我正在尝试将多维数组转换为简单数组。我的代码的某些部分正在工作,但是当我必须保留子数组时,我迷失了。这是主阵列:

Array
(
    [0] => Array
        (
            [name] => scattr
            [value] => 250
        )

    [1] => Array
        (
            [name] => scattrtel
            [value] => 9830293789
        )

    [2] => Array
        (
            [name] => scattrcolor
            [value] => #2764C6
        )

    [3] => Array
        (
            [name] => scattrta
            [value] => 
        )

    [4] => Array
        (
            [name] => scattrcb
            [value] => 
        )

    [5] => Array
        (
            [name] => scattrmcb[1]
            [value] => 10
        )

    [6] => Array
        (
            [name] => scattrmcb[4]
            [value] => 40
        )

    [7] => Array
        (
            [name] => scattrmrdo
            [value] => 20
        )

    [8] => Array
        (
            [name] => scattrselect
            [value] => 30
        )

    [9] => Array
        (
            [name] => pwpus-shortcode-nonce
            [value] => 028c9426c5
        )

    [10] => Array
        (
            [name] => _wp_http_referer
            [value] => /wp-admin/admin-ajax.php
        )

)  

而且,我的PHP将其转换为简单数组:

$new = array();

foreach ( $datas as $data ) { // $datas as being the multidimensional array.
    $new[$data['name']] = $data['value'];
}

现在,print_r $new给了我:

Array
(
    [scattr] => 250
    [scattrtel] => 9830293789
    [scattrcolor] => #2764C6
    [scattrta] => gdkwsdghwkdhgk
    [scattrcb] =>
    [scattrmcb[1]] => 10
    [scattrmcb[4]] => 40
    [scattrmrdo] => 20
    [scattrselect] => 30
    [pwpus-shortcode-nonce] => 028c9426c5
    [_wp_http_referer] => /wp-admin/admin-ajax.php
)  

所以,我认为我的代码有些工作,但没有在数组中保留scattrmcb[1]scattrmcb[4]。应该给出这样的东西:

[scattrmcb] => Array
            (
                [1] => 10
                [4] => 40
            )

我该怎么做?

由于

2 个答案:

答案 0 :(得分:1)

所以array_column()会帮助你完成大部分工作。我使用正则表达式获取键和内部数组索引,然后eval() != evil

foreach(array_column($datas, 'value', 'name') as $key => $value) {
    if(preg_match('/(.*)(\[\d+\])$/', $key, $match)) {
        eval("\$new['{$match[1]}']{$match[2]} = $value;");
    } else {
        $new[$key] = $value;
    }
}

可能或多或少可读eval()

        eval('$new["'.$match[1].'"]'.$match[2].' = $value;');

答案 1 :(得分:0)

您需要更多逻辑:

<?php
$datas = [
    ['name'=>'scattr', 'value'=>250],
    ['name'=>'scattrmcb[1]', 'value'=>10],
    ['name'=>'scattrmcb[4]', 'value'=>40]
];

$new = array();

foreach ( $datas as $data ) { // $datas as being the multidimensional array.
    // 'scattrmcb' is special so handle it here
    if(substr($data['name'], 0, 9) === 'scattrmcb'){
        // Set the 'scattrmcb' index
        if(!isset($new[substr($data['name'], 0, 9)])){
            $new[substr($data['name'], 0, 9)] = [];
        }

        // Use preg_replace to set a sub-array to the value between []
        $new[substr($data['name'], 0, 9)][preg_replace("/[^0-9]/", "", $data['name'])] = $data['value'];
    }
    else{
        $new[$data['name']] = $data['value'];
    }
}

print_r($new);