Skip to content
Advertisement

Transform a 3-dimensional array in PHP

I have an array like this :

array(3) {
    ["FL_1"] => array(3) {
        ["MIC_1"] => array(1) {
            ["SP_4"] => float(7)
        }
        ["MIC_13"] => array(1) {
            ["SP_16"] => float(4)
        }
        ["MIC_6"] => array(1) {
            ["SP_74"] => float(4)
        }
    }
    ["FL_2"] => array(2) {
        ["MIC_1"] => array(1) {
            ["SP_5"] => float(4)
        }
        ["MIC_13"] => array(1) {
            ["SP_17"] => float(4)
        }
        ["MIC_6"] > array(1) {
            ["SP_75"] => float(4)
        }
    }
    ["FL_3"] => array(2) {
        ["MIC_1"] => array(1) {
            ["SP_5"] => float(89)
        }
        ["MIC_13"] => array(1) {
            ["SP_18"] => float(1)
        }
        ["MIC_6"] > array(1) {
            ["SP_78"] => float(21)
        }
    }
}

For each FL_X, I need to keep only one MIC_X that follow the conditions below :

1- This MIC_X needs to be the same for each FL_X
2- This MIC_X needs to have the lowest possible SP_Xvalue

From this example I need to get the following array

array(3) {
    ["FL_1"] => array(1) {
        ["MIC_13"] => array(1) {
            ["SP_16"] => float(4)
        }
    }
    ["FL_2"] => array(1) {
        ["MIC_13"] => array(1) {
            ["SP_17"] => float(6)
        }
    }
    ["FL_3"] => array(1) {
        ["MIC_13"] => array(1) {
            ["SP_18"] => float(1)
        }
    }
}

Any help on how to do this would be much appreciated.
Thank you !

Advertisement

Answer

Here’s one possible solution. It uses array_walk_recursive to find the SP_X key associated with the minimum SP_X value, then it traverses the array to find the MIC_X key associated with that SP_X key and value, and finally it uses array_map and array_filter to extract only those MIC_X key values from the original array:

// find the minimum SP_X value and its key

$min_sp = PHP_INT_MAX;
$min_key = '';
array_walk_recursive($array, function ($v, $k) use (&$min_sp, &$min_key) {
    if ($v < $min_sp) {
        $min_sp = $v;
        $min_key = $k;
    } 
});

// find the MIC_X key corresponding to the min SP_X value
$mic_key = '';
foreach ($array as $fl) {
    foreach ($fl as $mic => $sp) {
        if (isset($sp[$min_key]) && $sp[$min_key] == $min_sp) {
            $mic_key = $mic;
            break 2;
        }
    }
}

// filter the array to get all the MIC_X values
$out = array_map(function ($fl) use ($mic_key) {
    return array_filter($fl, function ($mic) use ($mic_key) {
        return $mic == $mic_key;
    }, ARRAY_FILTER_USE_KEY);
}, $array);

print_r($out);

Output:

Array
(
    [FL_1] => Array
        (
            [MIC_13] => Array
                (
                    [SP_16] => 4
                )
        )
    [FL_2] => Array
        (
            [MIC_13] => Array
                (
                    [SP_17] => 4
                )
        )
    [FL_3] => Array
        (
            [MIC_13] => Array
                (
                    [SP_18] => 1
                )
        )
)

Demo on 3v4l.org

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement