Solved

Using PHP to create Multidimensional Arrays

Posted on 2007-03-29
2
184 Views
Last Modified: 2013-12-13
Trying to create a multidimensional array, and having no luck.  In fact, I think even my query is all screwed up. :((((

Let's say my data looks like this (some of the actual data is sensitive, but this is very close to it):

ID  |   Contact   |   Shift   |   Group
1   |   Joe   |   Morning   |   1
2   |   Joe   |   Morning   |   2
3   |   Joe   |   Morning   |   3
4   |   Joe   |   Evening   |   1
5   |   Joe   |   Evening   |   3
6   |   Sam   |   Morning   |   1
7   |   Sam   |   Morning   |   1
8   |   Max   |   Evening   |   3
9   |   Max   |   Evening   |   2
10  |   Max   |   Evening   |   3

First, I want to find the unique contacts, and create a multidimensional array for that one, unique contact.  For example, Joe.  Then I want to find the unique shifts for Joe, then I want to build an array that looks like this:

Array ( "Morning" => array("1", "2", "3"), "Evening" => array("1", "3"))

Does anyone know how to accomplish this?  I don't even know where to start!  
0
Comment
Question by:jen_jen_jen
2 Comments
 
LVL 24

Assisted Solution

by:glcummins
glcummins earned 100 total points
ID: 18821670
Here is an example script that will build the multidimensional array as you requested. Does this look like it will fit your needs?

<?php

mysql_connect('localhost', 'user', 'password') or die(mysql_error());
mysql_select_db('test') or die(mysql_error());

$query = "SELECT * from shifts;";
$result = mysql_query($query) or die(mysql_error());

mysql_close();

$arrContacts = array();

while ($row = mysql_fetch_object($result))
{
      if (!array_key_exists($row->Contact, $arrContacts))
      {
            $arrContacts[$row->Contact] = array();
      }
      if (!array_key_exists($row->Shift, $arrContacts[$row->Contact]))
      {
            $arrContacts[$row->Contact][$row->Shift] = array();
      }

      $arrContacts[$row->Contact][$row->Shift][] = $row->Group;
}

?>
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 400 total points
ID: 18822413
$query  = mysql_query("SELECT Contact FROM tablename GROUP BY Contact");

while(list($contact) = mysql_fetch_row($query)) {


      $subquery = mysql_query("SELECT * FROM tablename WHERE Contact='".$contact."'");
      
      while($row = mysql_fetch_object($subquery)) {
      
            $all_contacts[$contact][$row->Shift][] = $row->Group;
      
      }

}


this should give you something like  Array("Joe"=>Array ( "Morning" => array("1", "2", "3"), "Evening" => array("1", "3")),"Max"=>Array ( "Morning" => array("1", "2", "3"), "Evening" => array("1", "3"))) etc
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

773 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