Link to home
Start Free TrialLog in
Avatar of saabStory
saabStoryFlag for United States of America

asked on

Needing help building an array in a specific format

I've been trying to create a multi-level ordered list in PHP for some time now. I've tried and have been been searching and everything I've seen has a different data layout than what I have to work with. Most examples pull the data from a database and build the array based on separate columns and, in come cases, columns with depth or parental relationships information. My data is in a path to an SSRS report and can vary from 2 to 3 (or more in the future) levels so I'm not sure how to build the array to work with the function in this question as the structure of the data I have is different form all the examples.

The full example I'm working is attached as array1.php

array1.php

I'm pulling data from an SSRS report server. The only relevant part of it is the path to the report I have to link.  The report name is always the last element in string.  There is an additional column called 'Name' but this is simply a duplicate of the last element in the 'Path' data, so it's somewhat redundant anyway.

Data from SSRS report table - column name = "Path"
/Production/Analyst/POS Sales Excel Data Export
/Production/Analyst/STATS Membership Tracking - RAW
/Production/Information Technologies/IT Dashboard DUMMY
/Production/Information Technologies/IT Department
/Production/Membership/MDash
/Production/Membership/Membership Dashboard
/Production/Membership/Membership Reports/Membership Grid
/Production/Membership/Membership Reports/Membership Grid by SU
/Production/Membership/Membership Reports/Membership Pie Charts
/Production/Membership/Membership Reports/Membership Sum Drill Down
/Production/Membership/Membership Reports/POS Sales By Date and Location
/Production/Membership/Stats/STATS Council Participation
/Production/Membership/Stats/STATS Council SU Summary Adults
/Production/Membership/Stats/STATS Council Summary Adults
/Production/Membership/Stats/STATS Council Troop Summary Adults
/Production/Membership/Stats/STATS Lifetime Stats
/Production/Membership/Stats/STATS Membership Tracking
/Production/Online Training (LMS)/LMS User ID Lookup
/Production/Online Training (LMS)/Manager Training 2014
/Production/Shop/POS Reports/POS Sales By Date and Location
/Production/Shop/Shop Dashboard DUMMY
/Production/SLT Dashboard/OLD Dashboard Membership_Overview

Open in new window

That data would need need to go in a format like that below to work with the function I have. The ID is irrelevant to my purposes since the link to the report is the same as the report name so I would have to change the function to suit. That said, I need to take the path above and turn it into an array like that below.

Array format required:
$nested = Array
(
1 => Array ('parent' => 0, 'title' => 'Analyst'),
2 => Array ('parent' => 0, 'title' => 'Membership'),
3 => Array ('parent' => 0, 'title' => 'SLT Dashboard'),
4 => Array ('parent' => '1', 'title' => 'POS Sales Excel Data Export'),
5 => Array ('parent' => '1', 'title' => 'STATS Membership Tracking - RAW'),
6 => Array ('parent' => '2', 'title' => 'Membership Dashboard'),
7 => Array ('parent' => '6', 'title' => 'Membership Grid'),
8 => Array ('parent' => '6', 'title' => 'Membership Grid by Service Unit'),
);

Open in new window

So, how do convert the data I'm working with into an array that will output a format like the snippet above?  I've been looking at a lot of sites trying to understand more about arrays - I'm starting to see the light but still need some help in the right direction.  Any help would be greatly appreciated.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I can't line up the data from the SSRS report table with the Array posted above.  I'm trying to get a better handle on what you want to achieve here.  In this snippet I've taken the rows apart a little bit to try to show parent/child directories (for want of a better term) in groups.  Everything is a child of Production and the children include Analyst, Membership, etc.  Membership may have child directories that include Membership Reports and Stats.

Does this start to make sense of what sort of data structure you're after?

/Production/Analyst/POS Sales Excel Data Export
/Production/Analyst/STATS Membership Tracking - RAW

/Production/Information Technologies/IT Dashboard DUMMY
/Production/Information Technologies/IT Department

/Production/Membership/MDash
/Production/Membership/Membership Dashboard

/Production/Membership/Membership Reports/Membership Grid
/Production/Membership/Membership Reports/Membership Grid by SU
/Production/Membership/Membership Reports/Membership Pie Charts
/Production/Membership/Membership Reports/Membership Sum Drill Down
/Production/Membership/Membership Reports/POS Sales By Date and Location

/Production/Membership/Stats/STATS Council Participation
/Production/Membership/Stats/STATS Council SU Summary Adults
/Production/Membership/Stats/STATS Council Summary Adults
/Production/Membership/Stats/STATS Council Troop Summary Adults
/Production/Membership/Stats/STATS Lifetime Stats
/Production/Membership/Stats/STATS Membership Tracking

/Production/Online Training (LMS)/LMS User ID Lookup
/Production/Online Training (LMS)/Manager Training 2014

/Production/Shop/POS Reports/POS Sales By Date and Location

/Production/Shop/Shop Dashboard DUMMY

/Production/SLT Dashboard/OLD Dashboard Membership_Overview

Open in new window

Avatar of saabStory

ASKER

Right - it looks like you have it right.  In fact, since production is common to all, it can probably be left off.  This will ultimately be used for a menu and it requires an unordered list to work properly.

If it helps, this is  a test from the function in the attachment using part of the data above.

<ul>
	<li><a href="#">Analyst</a> 
		<ul>
            <li><a href="#">POS Sales Excel Data Export</a></li>
			<li><a href="#">STATS Membership Tracking - RAW</a></li>
		</ul>
	</li>
	<li><a href="#">Membership</a>
    	<ul>
			<li><a href="#">Membership Dashboard</a>
            	<ul>
					<li><a href="#">Membership Race and Ethnicity Grid</a></li>
					<li><a href="#">Membership Race and Ethnicity Grid by SU</a></li>
				</ul>
			</li>
		</ul>
	</li>
	<li><a href="#">SLT Dashboard</a></li>
</ul>

Open in new window

I've been looking at this a little more, and I still can't see how you're getting these results.  For example, the data from the SSRS table has Membership Dashboard, and there is nothing under that category.  Yet in the HTML example here we have Membership Race and Ethnicity Grid and Membership Race and Ethnicity Grid by SU.  This matters to my thinking because I am wondering if the ends of the tree are unique and the branches are unique.  If they're not, I will need to use a different code set to traverse the tree.

Do you have a complete test data set for this?
The example above is pretty much like the path field in the database.  It's not my table, but our dba/report writer.  There's a lot of other stuff in the tables to track transactions (i.e. inserts, updates, etc) but this is the the schema of the table:

COL. NAME      DATA TYPE               ALLOW NULLS
==============================================
ItemID		uniqueidentifier	Unchecked
Path		nvarchar(425)		Unchecked
Name		nvarchar(425)		Unchecked
ParentID	uniqueidentifier	Checked
Type		int			Unchecked
[Content]	image			Checked
Intermediate	uniqueidentifier	Checked
SnapshotDataID	uniqueidentifier	Checked
LinkSourceID	uniqueidentifier	Checked
Property	ntext			Checked
Description	nvarchar(512)		Checked
Hidden		bit			Checked
CreatedByID	uniqueidentifier	Unchecked
CreationDate	datetime		Unchecked
ModifiedByID	uniqueidentifier	Unchecked
ModifiedDate	datetime		Unchecked
MimeType	nvarchar(260)		Checked
SnapshotLimit	int			Checked
Parameter	ntext			Checked
PolicyID	uniqueidentifier	Unchecked
PolicyRoot	bit			Unchecked
ExecutionFlag	int			Unchecked
ExecutionTime	datetime		Checked
SubType		nvarchar(128)		Checked
ComponentID	uniqueidentifier	Checked

Open in new window


For my purposes, the relevant data would be in the attachment.  The ItemID is the PK of the table and is referenced in the ParentID column.  Path is the full path to the report including the name of the report and Name is just the name of the report.  From the Type column, I'm was only using those rows with Type=2, as those are the only ones that actually show in the navigation.

I hope this helps - I really appreciate your efforts on this.
dashboard-TestData.xlsx
Please confirm something for me...  Here is the code and the rendered tree (image).  Am I correct in understanding that Page 2-3-3 is out of place under Page 3-1, and instead should be under Page 2-3?

$nested = Array
(
 1 => Array ('id' =>  1, 'parent' =>  0, 'title' => 'Page 1'),
 2 => Array ('id' =>  2, 'parent' =>  0, 'title' => 'Page 2'),
 3 => Array ('id' =>  3, 'parent' =>  0, 'title' => 'Page 3'),
 4 => Array ('id' =>  4, 'parent' =>  0, 'title' => 'Page 4' ),
 5 => Array ('id' =>  5, 'parent' =>  0, 'title' => 'Page 5'),
 6 => Array ('id' =>  6, 'parent' =>  1, 'title' => 'Page 1-1'),
 7 => Array ('id' =>  7, 'parent' =>  1, 'title' => 'Page 1-2'),
 8 => Array ('id' =>  8, 'parent' =>  1, 'title' => 'Page 1-3'),
 9 => Array ('id' =>  9, 'parent' =>  2, 'title' => 'Page 2-1'),
10 => Array ('id' => 10, 'parent' =>  2, 'title' => 'Page 2-2'),
11 => Array ('id' => 11, 'parent' =>  2, 'title' => 'Page 2-3'),
12 => Array ('id' => 12, 'parent' =>  3, 'title' => 'Page 3-1'),
13 => Array ('id' => 13, 'parent' =>  3, 'title' => 'Page 3-2'),
14 => Array ('id' => 14, 'parent' =>  4, 'title' => 'Page 4-1'),
15 => Array ('id' => 15, 'parent' =>  6, 'title' => 'Page 1-1-1'),
16 => Array ('id' => 16, 'parent' =>  6, 'title' => 'Page 1-1-2'),
17 => Array ('id' => 17, 'parent' =>  6, 'title' => 'Page 1-1-3'),
18 => Array ('id' => 18, 'parent' =>  7, 'title' => 'Page 1-2-1'),
19 => Array ('id' => 19, 'parent' =>  7, 'title' => 'Page 1-2-2'),
20 => Array ('id' => 20, 'parent' =>  7, 'title' => 'Page 1-2-3'),
21 => Array ('id' => 21, 'parent' =>  9, 'title' => 'Page 2-1-1'),
22 => Array ('id' => 22, 'parent' =>  9, 'title' => 'Page 2-1-2'),
23 => Array ('id' => 23, 'parent' => 12, 'title' => 'Page 2-3-3')
)
;

Open in new window

User generated image
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As my kids like to say - HOLY FREAKING AWESOME!!  That looks absolutely perfect - and I can see that I have a lot to study on over the weekend.  I can't tell you how much we appreciate this - will make for a very happy director and vp!
Excellent is not nearly enough in this instance - given all that he is obviously doing throughout this board, to spend this much time is just way, way above and beyond.  I appreciate this more than I can say.
Thanks for the points and thanks for using EE.  I may have been overthinking the problem.  If I see a simpler solution, I'll post it back here.
No, now that I looked at it again, I think this is a good enough solution.  Best regards, ~Ray