PHP转换多维数组

时间:2018-05-21 12:55:51

标签: php arrays multidimensional-array

我有阵列

$Old = [_1526906780329_329] => Array (
    [frlabel] => aa
    [enlabel] => AA
    )
[_1526906783640_640] => Array (
    [frlabel] => bb
    [enlabel] => BB
    )

我希望$old数组以单数组键值格式转换它,如

array('AA' => 'aa','BB' => 'bb');

3 个答案:

答案 0 :(得分:3)

$Old = [
    '_1526906780329_329' => [
        'frlabel' => 'aa',
        'enlabel' => 'AA',
    ],
    '_1526906783640_640' => [
        'frlabel' => 'bb',
        'enlabel' => 'BB',
    ],
];

$newArray = array_column($Old, 'frlabel', 'enlabel');
print_r($newArray);

输出:

Array
(
    [AA] => aa
    [BB] => bb
)

答案 1 :(得分:1)

<?php 


    $final = array();
    $Old[_1526906780329_329] = Array (
        'frlabel' => 'aa',
        'enlabel' => 'AA',
        );

    $Old[_1526906783640_640] = Array (
        'frlabel' => 'bb',
        'enlabel' => 'BB',
        );

    foreach ($Old as $key => $value) {
        $final[$value['enlabel']] = $value['frlabel'];
    }
    echo "<pre>";print_r($final);

答案 2 :(得分:0)

假设这是特定于数据的......

$array1 = array();
$array1['frlabel'] = 'aa';
$array1['enlabel'] = 'AA';

$array2 = ['frlabel' => 'bb', 'enlabel' => 'BB'];

$old = array();
$old[] = $array1;
$old[] = $array2;
$result = array();

foreach($old as $tmp){
    $result[$tmp['enlabel']] = $tmp['frlabel'];
}

var_dump($result);

输出:

array(2) {
    ["AA"]=>
    string(2) "aa"
    ["BB"]=>
    string(2) "bb"
}