php - How do I create a multidimensional tree array from a flat array? -


'मेरे पास यह सपाट सरणी है:

  $ folders = [' test / something txt ',' test / hello.txt ',' test / another-folder / myfile.txt ',' test / another-folder / kamil.txt ',' test / another-folder / john / hi.txt '] < / code>  

और मुझे इसे निम्न प्रारूप में चाहिए:

  $ folders = ['test' = & gt; ['कुछ। txt', 'hello.txt', 'another-folder' = & gt; ['Myfile.txt', 'केमिल.txt', 'जॉन' = & gt; ['hi.txt']]]]];   

मैं यह कैसे कर सकता हूँ? धन्यवाद।

दोहराव आपका दोस्त है: -)

  function createrray ($ फ़ोल्डर, $ आउटपुट) {if (count ($ folders) & gt; 2) {$ key = array_shift ($ folders); $ आउटपुट [$ key] = createArray ($ फ़ोल्डर, जारी ($ आउटपुट [$ की])? $ आउटपुट [$ की]: []); } Else {if (! Isset ($ output [$ folders [0]])) {$ आउटपुट [$ folders [0]] = []; } $ आउटपुट [$ फ़ोल्डर [0]] [] = $ फ़ोल्डर [1]; } $ आउटपुट लौटाएं; }   

जब तक आप फ़ाइल नाम पर नहीं जाते तब तक ड्रिलिंग रखें, फिर उन्हें सरणी में एक साथ जोड़ दें।

आपको अपने प्रत्येक तत्व के लिए इस फ़ंक्शन को कॉल करना होगा सरणी, इस तरह:

  $ newFolders = []; विदेशी मुद्रा ($ फ़ोल्डर के रूप में $ फ़ोल्डर) {$ newFolders = createArray (विस्फोट ('/', $ फ़ोल्डर), $ newFolders); }   

डेमो:

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -