?
Solved

Recursive array function...

Posted on 2009-04-21
10
Medium Priority
?
465 Views
Last Modified: 2012-06-27
All,

I have the following array;

$arrayHubDeviceParams => Array (1)
(
|    ['InternetGatewayDevice'] => Array (2)
|    (
|    |    ['DeviceInfo'] => Array (2)
|    |    (
|    |    |    ['SpecVersion'] = String(3) "1.1"
|    |    |    ['VendorConfigFile'] => Array (2)
|    |    |    (
|    |    |    |    ['1'] => Array (4)
|    |    |    |    (
|    |    |    |    |    ['Date'] = String(8) "1/4/2009"
|    |    |    |    |    ['Description'] = String(6) "test 1"
|    |    |    |    |    ['Name'] = String(8) "testing1"
|    |    |    |    |    ['Version'] = String(3) "1.1"
|    |    |    |    )
|    |    |    |    ['2'] => Array (4)
|    |    |    |    (
|    |    |    |    |    ['Date'] = String(8) "2/4/2009"
|    |    |    |    |    ['Description'] = String(6) "test 2"
|    |    |    |    |    ['Name'] = String(9) "testing 2"
|    |    |    |    |    ['Version'] = String(3) "2.1"
|    |    |    |    )
|    |    |    )
|    |    )
|    |    ['DeviceSummary'] = String(39) "InternetGatewayDevice:1.0[](Baseline:1)"
|    )
)

What I need is a function that would output in a table, but also need for each item a 'path' as it's used in the DB as a fieldname for editing/deleteing etc etc...

the function I have does this:

InternetGatewayDevice
.InternetGatewayDevice.DeviceInfo
.InternetGatewayDevice.DeviceInfo.SpecVersion
.InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile
.InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1
.InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1.Date
.InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1.Date.Description
.InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1.Date.Description.Name
.InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1.Date.Description.Name.Version
.InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1.2
.InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1.2.Date
.InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1.2.Date.Description
.InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1.2.Date.Description.Name
.InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1.2.Date.Description.Name.Version
.InternetGatewayDevice.DeviceInfo.DeviceSummary

what I need it to generate is:

InternetGatewayDevice
InternetGatewayDevice.DeviceInfo
InternetGatewayDevice.DeviceInfo.SpecVersion
InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile
InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1
InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1.Date
InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1.Description
InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1.Name
InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.1.Version
InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.2
InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.2.Date
InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.2.Description
InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.2.Name
InternetGatewayDevice.DeviceInfo.SpecVersion.VendorConfigFile.2.Version
InternetGatewayDevice.DeviceInfo.DeviceSummary

Hope this makes sense... thanks in advance!
My Function as it stands:
 
function testPrint($array,$px="") {
	while(list($key, $value) = each($array)) {
		if (is_array($value)) {
			// item with no value
			$px .= ".".$key;
			echo $px."<br />";
			//echo "ARR: k: ".$key."<br />";
			testPrint($value,$px);
		} else {
			// item with value (end of sub array)
			$px .= ".".$key;
			echo $px."<br />";
			//echo "VAL: k: ".$key." :: v: ".$value."<br />";
		}
	}
}

Open in new window

0
Comment
Question by:BrianMM
  • 5
  • 3
  • 2
10 Comments
 
LVL 11

Author Comment

by:BrianMM
ID: 24192817
Hi, just an addendum...

What I am looking for is to print out a table like, each parameter should be a link with the 'path'

i.e. ExternalIPAddress would have <a href="index.php?edit=InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.ExternalIPAddress>edit</a>:
Parameter					Value
=========					=====
InternetGatewayDevice	 
    DeviceInfo	 
        HardwareVersion	 
            BANT				Z
        ProvisioningCode			BT
        SoftwareVersion				8.1.E.7.AA
        SpecVersion				1.0
        VendorConfigFile	 
            1	 
                Date				1/4/2009
                Description			test 1
                Name				testing1
                Version				1.1
            2	 
                Date				2/4/2009
                Description			test 2
                Name				testing 2
                Version				2.1
    DeviceSummary				InternetGatewayDevice:1.3[](Baseline:1, EthernetLAN:1, ADSLWAN:1, EthernetWAN:1, Bridging:1, Time:1, DeviceAssociation:1, WiFiLAN:1, USBLAN:1, QoS:1, IPPing:1), VoiceService:1.0[1](SIPEndpoint:1, TAEndpoint:1)
    ManagementServer	 
        ConnectionRequestURL			http://86.177.74.121:51005/
        ParameterKey				9716048
    WANDevice	 
        1	 
            WANConnectionDevice	 
                1	 
                    WANPPPConnection	 
                        1	 
                            ExternalIPAddress	86.177.74.121

Open in new window

0
 
LVL 3

Accepted Solution

by:
SPARC-DESIGN earned 750 total points
ID: 24192935
what about this:
 

function testPrint($array,$px="") {
	while(list($key, $value) = each($array)) {
		if (is_array($value)) {
			// item with no value
			$px .= ".".$key;
			echo $px."<br />";
			//echo "ARR: k: ".$key."<br />";
			testPrint($value,$px);
		} else {
			// item with value (end of sub array)
			echo $px.".".$key."<br />";
			//echo "VAL: k: ".$key." :: v: ".$value."<br />";
		}
	}
}

Open in new window

0
 
LVL 11

Author Comment

by:BrianMM
ID: 24193268
That ALMOST does it!...

Only one small quirk... the items ther are .1.2 should only be 2...

.InternetGatewayDevice.DeviceInfo.HardwareVersion.VendorConfigFile              
.InternetGatewayDevice.DeviceInfo.HardwareVersion.VendorConfigFile.1              
.InternetGatewayDevice.DeviceInfo.HardwareVersion.VendorConfigFile.1.Date      1/4/2009       
.InternetGatewayDevice.DeviceInfo.HardwareVersion.VendorConfigFile.1.Description      test 1       
.InternetGatewayDevice.DeviceInfo.HardwareVersion.VendorConfigFile.1.Name      testing1       
.InternetGatewayDevice.DeviceInfo.HardwareVersion.VendorConfigFile.1.Version      1.1       
.InternetGatewayDevice.DeviceInfo.HardwareVersion.VendorConfigFile.1.2              
.InternetGatewayDevice.DeviceInfo.HardwareVersion.VendorConfigFile.1.2.Date      2/4/2009       
.InternetGatewayDevice.DeviceInfo.HardwareVersion.VendorConfigFile.1.2.Description      test 2       
.InternetGatewayDevice.DeviceInfo.HardwareVersion.VendorConfigFile.1.2.Name      testing 2       
.InternetGatewayDevice.DeviceInfo.HardwareVersion.VendorConfigFile.1.2.Version
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Author Comment

by:BrianMM
ID: 24193272
BTW, thanks for your quick response! If you can help me solve the above I'll be happy! :P
0
 
LVL 3

Expert Comment

by:SPARC-DESIGN
ID: 24193401
Np, I will answer youre question later today because I don't have an PHP environnement over here (I'm at work)
0
 
LVL 11

Author Comment

by:BrianMM
ID: 24193457
I am ultimatley wanting this as an expandible treeview!!! is your able to help with that? :)
0
 
LVL 5

Expert Comment

by:AtanAsfaloth
ID: 24193807
Maybe I can assist in the mean time. I think this solves the 1.2. problem, also, it removes the leading period.


function testPrint($array,$px="") {
	foreach($array as $key => $value) {
		if (is_array($value)) {
			// item with no value
			echo "$px$key<br />";
			testPrint($value, "$px$key.");
		} else {
			// item with value (end of sub array)
			echo "$px$key<br />";
		}
	}
	$px .= "$key.";
}

Open in new window

0
 
LVL 5

Assisted Solution

by:AtanAsfaloth
AtanAsfaloth earned 750 total points
ID: 24194412
To get some sort of treeview it is possible to use the following though this is not expandable. Note that I have completely removed the $px variable here as it was no longer necessary. SPARC-DESIGNs suggestion looks very good so I suggest using (part of) that code for an expandable treeview.
<?php
function testPrint($array) {
	foreach($array as $key => $value) {
		if (is_array($value)) {
			// item with no value
			echo "<li style=\"clear: both;\">$key<ul>";
			testPrint($value);
			echo "</ul></li>";
		} else {
			// item with value (end of sub array)
			echo "<li style=\"clear: both;\"><div style=\"float:left;\">$key</div><div style=\"float:right; width:400px;\">$value</div></li>";
		}
	}
}
?>
<div style="width:800px">
<ul>
<?php testPrint($arrayHubDeviceParams); ?>
</ul>
</div>

Open in new window

0
 
LVL 11

Author Comment

by:BrianMM
ID: 24195040
I have kind of done it... not fully... but have been dragged onto another project for the rest of today...

I shall look in more detail tomorrow but that looks good thanks!

Cheers,

Brian
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question