[thelist] PHP - Parsing Arrays

Alex Ezell taranis at spittingllamas.com
Thu Oct 9 08:16:55 CDT 2003


This is a lot to digest and think about, so I realize I am asking a lot of you here, so any thoughts or suggestions are greatly appreciated.

I am pretty new to PHP and so I am finally up against my first big Array task. I could use some pointers, pseudo-code suggestions, or just ways to think about this. I am really just daunted by what I need to do.

So, my class is giving me back an array with all the data that I need, but I need to pull certain pieces out to use within the Smarty templates that I am using. Here's a sample of what the main array I am dealing with looks like. Some of the text data is truncated for space in this post.


PHP Code:

Array ( 
[0] => Array ( 
  [workID] => 7 
  [workFileName] => dentalWhiteOnBlue2.jpg 
  [workFileSize] => 87072 
  [projectID] => 4 
  [workDescription] => Main photography for the interior wall splash. 
  [workTypeName] => Brand Awareness 
  [projectName] => Neocon Booth 
  [clientID] => 2 
  [projectDescription] => Design and specify vendor for booth at Neocon trade show. 
  [projectTypeName] => Electronic Marketing 
  [clientEngageDate] => 2001-10-17 
  [clientDescription] => Patcraft manufactures commercial carpet for a variety of applications including heavy industrial. 
  [clientName] => Patcraft 
  [clientCity] => Dalton 
  [clientState] => GA 
  [clientTypeName] => Commercial Flooring - Manufacturer 
) 
[1] => Array ( 
  [workID] => 6 
  [workFileName] => trialWork8.txt 
  [workFileSize] => 29 
  [projectID] => 4 
  [workDescription] => Various keywords to appear on the wall screen. 
  [workTypeName] => Brand Awareness 
  [projectName] => Neocon Booth 
  [clientID] => 2 
  [projectDescription] => Design and specify vendor for booth at Neocon trade show. 
  [projectTypeName] => Electronic Marketing 
  [clientEngageDate] => 2001-10-17 
  [clientDescription] => Patcraft manufactures commercial carpet for a variety of applications including heavy industrial.
  [clientName] => Patcraft 
  [clientCity] => Dalton 
  [clientState] => GA 
  [clientTypeName] => Commercial Flooring - Manufacturer 
) 
[2] => Array ( 
  [workID] => 5 
  [workFileName] => trialWork1.txt 
  [workFileSize] => 29 
  [projectID] => 2 
  [workDescription] => Copy for the maintenance email blast. 
  [workTypeName] => Electronic Marketing 
  [projectName] => Customer Email Blast 
  [clientID] => 2 
  [projectDescription] => Create attractive image and message for bulk email to customers. 
  [projectTypeName] => Electronic Marketing 
  [clientEngageDate] => 2001-10-17 
  [clientDescription] => Patcraft manufactures commercial carpet for a variety of applications including heavy industrial.
  [clientName] => Patcraft 
  [clientCity] => Dalton 
  [clientState] => GA 
  [clientTypeName] => Commercial Flooring - Manufacturer 
) 
[3] => Array ( 
  [workID] => 8 
  [workFileName] => dentalLightGreen2.jpg 
  [workFileSize] => 86083 
  [projectID] => 3 
  [workDescription] => Pantone-adjusted screen for the signmakers. 
  [workTypeName] => Outdoor Board 
  [projectName] => Main Building Signage 
  [clientID] => 1 
  [projectDescription] => Design and specify vendor for new signage with new identity. 
  [projectTypeName] => Identity/Branding 
  [clientEngageDate] => 2002-05-15 
  [clientDescription] => Bradley Memorial Hospital is a regional provider of care to a community of 100,000 residents.
  [clientName] => Bradley Memorial Hospital 
  [clientCity] => Cleveland 
  [clientState] => TN 
  [clientTypeName] => Healthcare - Hospital 
) 
) 


Each uppermost item in the Array is a "work." These works are collected into saved configurations which are just groups of works as chosen by the user. The entire array then represents the "works" that the user wants to include in their final product, which is a static HTML portfolio.

What I need to extract into other arrays are the distinct client data, distinct project data by client, distinct work data by project. I'm not even sure if I want to end up with an array that is resorted or several different arrays or what. 

I will be feeding the list of clients into a Smarty template to create some static files. I will also need to feed the list of projects matching a particular client into a Smarty template and then a static file. The same goes for each "work" according to the which project it belongs.

For example, one page will list all the clients included in this "config." Then, click a client, and you will see a list of that client's projects in this "config." And then a list of works for each of those projects.

This array is the result of a query done within the class I have written. Perhaps the answer lies in putting code in the query inside the class instead of manipulating the data in the logic layer. Since the code is really not logic, but actually more data manipulation, it might make more structural sense to put it in the class.

The query looks something like this:
PHP Code:

$query = 'SELECT ' .
  $this -> TBL_PREF . 'config_works.configID,' .
  $this -> TBL_PREF . 'config_works.workID,' .
  $this -> TBL_PREF . 'work.workID AS workID1,' .
  $this -> TBL_PREF . 'work.workFileName,' .
  $this -> TBL_PREF . 'work.workFileSize,' .
  $this -> TBL_PREF . 'work.workTypeID,' .
  $this -> TBL_PREF . 'work.projectID,' .
  $this -> TBL_PREF . 'work.workDescription,' .
  $this -> TBL_PREF . 'work_types.workTypeName,' .
  $this -> TBL_PREF . 'projects.projectID AS projectID1,' .
  $this -> TBL_PREF . 'projects.projectName,' .
  $this -> TBL_PREF . 'projects.clientID,' .
  $this -> TBL_PREF . 'projects.projectDescription,' .
  $this -> TBL_PREF . 'projects.projectTypeID,' .
  $this -> TBL_PREF . 'project_types.projectTypeName,' .
  $this -> TBL_PREF . 'clients.clientID AS clientID1,' .
  $this -> TBL_PREF . 'clients.clientTypeID,' .
  $this -> TBL_PREF . 'clients.clientEngageDate,' .
  $this -> TBL_PREF . 'clients.clientDescription,' .
  $this -> TBL_PREF . 'clients.clientName,' .
  $this -> TBL_PREF . 'clients.clientCity,' .
  $this -> TBL_PREF . 'clients.clientState,' .
  $this -> TBL_PREF . 'client_types.clientTypeName ' .
  'FROM ' .
  $this -> TBL_PREF . 'config_works ' .
  'INNER JOIN ' . $this -> TBL_PREF . 'work ON (' . $this -> TBL_PREF . 'config_works.workID = ' . $this -> TBL_PREF . 'work.workID) ' .
    'INNER JOIN ' . $this -> TBL_PREF . 'projects ON (' . $this -> TBL_PREF . 'work.projectID = ' . $this -> TBL_PREF . 'projects.projectID) ' .
    'INNER JOIN ' . $this -> TBL_PREF . 'clients ON (' . $this -> TBL_PREF . 'projects.clientID = ' . $this -> TBL_PREF . 'clients.clientID) ' .
  'INNER JOIN ' . $this -> TBL_PREF . 'client_types ON (' . $this -> TBL_PREF . 'clients.clientTypeID = ' . $this -> TBL_PREF . 'client_types.clientTypeID) ' .
    'INNER JOIN ' . $this -> TBL_PREF . 'project_types ON (' . $this -> TBL_PREF . 'projects.projectTypeID = ' . $this -> TBL_PREF . 'project_types.projectTypeID) ' .
    'INNER JOIN ' . $this -> TBL_PREF . 'work_types ON (' . $this -> TBL_PREF . 'work.workTypeID = ' . $this -> TBL_PREF . 'work_types.workTypeID) ' .
  'WHERE ' .
    '(' . $this -> TBL_PREF . 'config_works.configID = "' . $id . '" )'; 


which then gets stuffed into an array like so (this is inside a For loop that iterates through the recordset):
PHP Code:

$configWorks[$i] = array('workID' => $workID,
    'workFileName' => $this -> cleanout($workFileName),
    'workFileSize' => $workFileSize,
    'projectID' => $projectID,
    'workDescription' => $this -> cleanout($workDescription),
    'workTypeName' => $this -> cleanout($workTypeName),
    'projectName' => $this -> cleanout($projectName),
    'clientID' => $clientID,
    'projectDescription' => $this -> cleanout($projectDescription),
    'projectTypeName' => $this -> cleanout($projectTypeName),
    'clientEngageDate' => $clientEngageDate,
    'clientDescription' => $this -> cleanout($clientDescription),
    'clientName' => $this -> cleanout($clientName),
    'clientCity' => $this -> cleanout($clientCity),
    'clientState' => $clientState,
    'clientTypeName' => $this -> cleanout($clientTypeName)); 


I am thinking that my sorting code could go here. But keep in mind that this is in a class, so whatever it does needs to be able to be return to the function call in the application/logic code. 

Hopefully, that gives you a pretty good idea of what I am trying to do. I have the whole rest of this app complete but I just can't wrap my head around this. I'm not sure if Regular Expressions can help or if there are some lesser known PHP array functions that might help.

Thanks in advance for even reading this far. I appreciate that something like this is not a quick answer and will take some of your time which I am sure is precious.

-- 
Alex
http://www.spittingllamas.com
"Formal Restrictions, contrary to what you might think,
free you up by allowing you to concentrate on purer ideas."
 - Winter Sorbeck in Chip Kidd's The Cheese Monkeys



More information about the thelist mailing list