Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using PHP to create Multidimensional Arrays

Posted on 2007-03-29
2
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 24

Assisted Solution

by:glcummins
glcummins earned 400 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 1600 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.

704 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