AndieB Posted June 15, 2007 Share Posted June 15, 2007 Hi all! I have fetched some data that looks like this in the output: Servers Lab,Servers Non_Standard,Servers Group,Servers Operations,Servers DEV,Servers TS,DEV,Servers RIB,Non_Standard,Servers Infrastructure,Servers CITRIX,Infrastructure,Servers CITRIX,DEV,Servers CITRIX,Operations,Servers CITRIX,Lab,Servers TS,Infrastructure,Servers TS,Operations,Servers TS,Lab,Servers Stage,Infrastructure,Servers As you can see it is almost like a directory structure, with the "top level" starting at the right. Each comma separates the "levels". What I would like to get some help with, is how to code with PHP so that each "level" gets into an Array. Something like this: $structure["Servers"] $structure["Servers"]["DEV"] $structure["Servers"]["DEV"]["TS"] $structure["Servers"]["DEV"]["CITRIX"] $structure["Servers"]["LAB"]["TS"] ... I think you get the picture. But I do not know how to process the output and code it so I get it all into the "array" structure I want. Perhaps some of you reading this can help me out! Thankful for all kind of help! Sincerely, Andreas Quote Link to comment https://forums.phpfreaks.com/topic/55789-organize-into-an-array/ Share on other sites More sharing options...
emehrkay Posted June 16, 2007 Share Posted June 16, 2007 i wasnt sure what each line broke at so i put a ; at the end <?php $var = " Servers; Lab,Servers; Non_Standard,Servers; Group,Servers; Operations,Servers; DEV,Servers; TS,DEV,Servers; RIB,Non_Standard,Servers; Infrastructure,Servers; CITRIX,Infrastructure,Servers; CITRIX,DEV,Servers; CITRIX,Operations,Servers; CITRIX,Lab,Servers; TS,Infrastructure,Servers; TS,Operations,Servers; TS,Lab,Servers; Stage,Infrastructure,Servers; "; $var = explode(';', $var); $new_var = array(); foreach($var as $key => $arr){ $arr = explode(',', $arr); $count = count($arr); $val = ''; for($i = ($count - 1); $i >= 0; $i--){ $c = ($i == ($count - 1)) ? array($arr[$i]) : array($arr[$i] => $val); $val = $c; } $new_var[] = $val; } print_r($new_var); ?> Quote Link to comment https://forums.phpfreaks.com/topic/55789-organize-into-an-array/#findComment-275662 Share on other sites More sharing options...
emehrkay Posted June 16, 2007 Share Posted June 16, 2007 oops just reread your question change the for loop to start at zero, terminate at count, and increment by one for($i =0; $i < $count; $i++){ ... } Quote Link to comment https://forums.phpfreaks.com/topic/55789-organize-into-an-array/#findComment-275663 Share on other sites More sharing options...
sasa Posted June 16, 2007 Share Posted June 16, 2007 try <?php $var = " Servers; Lab,Servers; Non_Standard,Servers; Group,Servers; Operations,Servers; DEV,Servers; TS,DEV,Servers; RIB,Non_Standard,Servers; Infrastructure,Servers; CITRIX,Infrastructure,Servers; CITRIX,DEV,Servers; CITRIX,Operations,Servers; CITRIX,Lab,Servers; TS,Infrastructure,Servers; TS,Operations,Servers; TS,Lab,Servers; Stage,Infrastructure,Servers "; $var = str_replace("\n",'',$var); $var = explode(';',$var); foreach ($var as $str) { $str = explode(',',$str); $tmp1 = array(); $tmp2 = array(); if (count($str) > 1){ $tmp2[$str[1]] = $str[0]; for ($i = 2; $i < count($str); $i++) { $tmp1[$str[$i]] = $tmp2; $tmp2 = $tmp1; } } else $out[] = $str[0]; $out = array_merge_recursive($out,$tmp2); } print_r($out); ?> Quote Link to comment https://forums.phpfreaks.com/topic/55789-organize-into-an-array/#findComment-275679 Share on other sites More sharing options...
AndieB Posted June 16, 2007 Author Share Posted June 16, 2007 Thank you for the answer guys! Now I ran into some other "issues". I am going to create a SELECTION-tag and the result you helped me to create should give the following options values: Servers/Lab Servers/Non_Standard Servers/Group Servers/Operations Servers/DEV Servers/DEV/TS Servers/DEV/CITRIX Servers/Non_Standard/RIB and so on... You know how I would make this from the newly created array? Quote Link to comment https://forums.phpfreaks.com/topic/55789-organize-into-an-array/#findComment-275908 Share on other sites More sharing options...
emehrkay Posted June 17, 2007 Share Posted June 17, 2007 just use the same code sasa posted. except where he explodes by a comma, you explode by foward slash Quote Link to comment https://forums.phpfreaks.com/topic/55789-organize-into-an-array/#findComment-276138 Share on other sites More sharing options...
AndieB Posted June 17, 2007 Author Share Posted June 17, 2007 just use the same code sasa posted. except where he explodes by a comma, you explode by foward slash I must admit I am not aligned with what you mean. With the script you and "sasa" posted, I managed to get it all into an Array. With this new Array, I would like to create the HTML SELECT-tag with the options based on the Array just created with the values. Is this solved with "sasas" code also? thank you for your time to reply! Sincerely, Andreas Quote Link to comment https://forums.phpfreaks.com/topic/55789-organize-into-an-array/#findComment-276202 Share on other sites More sharing options...
sasa Posted June 17, 2007 Share Posted June 17, 2007 are you try this[codeg<?php $a = 'Servers Lab,Servers Non_Standard,Servers Group,Servers Operations,Servers DEV,Servers TS,DEV,Servers RIB,Non_Standard,Servers Infrastructure,Servers CITRIX,Infrastructure,Servers CITRIX,DEV,Servers CITRIX,Operations,Servers CITRIX,Lab,Servers TS,Infrastructure,Servers TS,Operations,Servers TS,Lab,Servers Stage,Infrastructure,Servers'; $a = explode("\n",$a); foreach ($a as $v) { $b[] = implode('/',array_reverse(explode(',',$v))); } echo '<pre>'; print_r($b); echo '</pre>'; // or foreach ($a as $v) { if (strpos($v, ',')) $c[] = implode('/',array_reverse(explode(',',$v))); } echo '<pre>'; print_r($c); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/55789-organize-into-an-array/#findComment-276227 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.