Skip to content
Advertisement

Transform a 3-dimensional array in PHP

I have an array like this :

JavaScript

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

JavaScript

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

JavaScript

Output:

JavaScript

Demo on 3v4l.org

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