Solved

Using PHP to create Multidimensional Arrays

Posted on 2007-03-29
2
183 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWS EC2 PHP code is not being executed code shows on the page instead 8 65
Problem with Simple PHP/mySQL Query 3 51
updating the date data 12 19
datetime in sql 6 22
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now