Jump to content

Organize into an Array


AndieB

Recommended Posts

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

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.