PHP如何扁平化这个索引数组?

时间:2017-06-09 13:35:48

标签: php arrays multidimensional-array

我有以下数组:

Array
(
    [0] => Array
        (
            [ContractorName] => Joe Soap
            [BonusAmount] => 73.92
        )

    [1] => Array
        (
            [ContractorName] => Mike Michaels
            [BonusAmount] => 68.55
        )

    [2] => Array
        (
            [ContractorName] => John Smith
            [BonusAmount] => 34.35
        )

    [3] => Array
        (
            [ContractorName] => Pete Peterson
            [BonusAmount] => 24.61
        )

    [4] => Array
        (
            [ContractorName] => Pete Smith
            [BonusAmount] => 22.76
        )

)

如何最终得到一个如下所示的数组:

Array
(
    [Joe Soap] => 73.92
    [Mike Michaels] => 68.55
    [John Smith] => 34.35
    [Pete Peterson] => 24.61
    [Pete Smith] => 22.76
)

此刻我有点失落。我试过通过循环第一个数组来创建一个新数组,但是我得到了不需要的结果。非常感谢任何帮助。

3 个答案:

答案 0 :(得分:4)

gag *pathfinder(gag *root, int choice) { if (root == NULL) { printf_s("the map didnt exits."); getchar(); exit(1); } if (choice == root->index) { printf_s("(%d,%d)", root->x, root->y); return root; } if (choice != root->index) if (root->A != NULL) { if (root->A->index != NULL && root->A->index == choice) ; else { pathfinder(root->A, choice); } } else if (choice != root->index) if (root->B != NULL) { if (root->B->index != NULL && root->B->index == choice) ; // printf_s("(%d,%d)->", root->B->x, root->B->y); else { pathfinder(root->B, choice); } } else if (choice != root->index) if (root->C != NULL) { if (root->C->index != NULL && root->C->index == choice) ; // printf_s("(%d,%d)->", root->C->x, root->C->y); else { pathfinder(root->C, choice); printf_s("(%d,%d)->", root->C->x, root->C->y); } } } gag* path(gag *map, gag* tree, int len) { char temp = map->index; gag* hold; tree->index = temp; static int c = -1; int *close; c++; (map)->index = 'v'; // Mark as visited. (map)->entries++; //go A if (!map->A || map->A->index == 'v' || map->A->entries == 3) tree->A = NULL; else { hold = malloc(sizeof(gag)*len); d_p_s(map, hold); tree->A = path(map->A, hold, len); } //go B if (!map->B || map->B->index == 'v' || map->B->entries == 3) tree->B = NULL; else { hold = malloc(sizeof(gag)*len); d_p_s(map, hold); tree->B = path(map->B, hold, len); } //go C if (!map->C || map->C->index == 'v' || map->C->entries == 3) tree->C = NULL; else { hold = malloc(sizeof(gag*)*len); d_p_s(map, hold); tree->C = path(map->C, hold, len); } map->index = temp; return tree; } array_combine一起使用

array_column

答案 1 :(得分:1)

使用foreach遍历整个数组,然后使用每个部分构建新数组。

$out = [];
foreach ($inputArray as $v) {
    $out[$v['ContractorName']] = $v['BonusAmount'];
}

第二种解决方案是使用array_combinearray_column

$keys = array_column($inputArray, 'ContractorName');
$values = array_column($inputArray, 'BonusAmount');

$output = array_combine($keys, $values);

//Or put everything in single line
$output = array_combine(array_column($inputArray, 'ContractorName'), array_column($inputArray, 'BonusAmount'));

第三个选项

$output = array_column($inputArray, 'BonusAmount', 'ContractorName');

答案 2 :(得分:0)

您可以使用一个array_column(),并使用第三个参数指定索引。 Live Demo.

array_column($array, 'BonusAmount', 'ContractorName');