Search code examples
phparraysmultidimensional-array

Reduce a 2d array to a flat associative array using one column as keys and another column as values


How do I get this array:

Array
(
    [0] => Array
        (
            [max] => 5
            [year] => 2007
        )

    [1] => Array
        (
            [max] => 6.05
            [year] => 2008
        )

    [2] => Array
        (
            [max] => 7
            [year] => 2009
        )

)

Into this format:

[year] => [max]

Solution

  • $result = array();
    foreach($array as $v) {
        $result[$v['year']] = $v['max'];
    }
    

    There you go.