How to use matrix in programming – matrix transpose in PHP.
In last time, one of my regular clients ordered dedicated application to count prices of his products. He is producer of animals accessories so his offer is very large. For today, He has about 3 500 products.
When prices of ingredients will change, He must recount the prices of all products by hand, what is very problematic. That’s why he asked me to create an application to automation of this process. I will not elaborate here all details of creating process. I will describe one of the problems what I met during writing application.
When the model of the product contained in itself only one dynamic component then all very simple, the whole problem started when the model of the products contained more than one dynamic component. All dynamic arrays have to scroll together, substituting another value and return the price of the products.
This picture should brighten situation:
And now, how can I write loops to get everything set up properly? At first idea occurred to me that I will write a separate function that after set arguments of all three boards and position the cursor to return data 3 elements and at that moment I saw the brilliant solution, arrays of the same size immediately reminded me of a mathematical matrix, so I need just the transpose !
To transpose the array in PHP can use this function:
function transpose($array) { array_unshift($array, null); return call_user_func_array('array_map', $array); }
At this point, just set the normal foreach to substitute the appropriate set of components.
Greetings!