Solved

Using PHP to create Multidimensional Arrays

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

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

Suggested Solutions

Title # Comments Views Activity
website maintenance mode 1 25
display balance of stars as greyed out 8 23
Change background color in td if any value is 1 9 24
Split wordpress loop 35 56
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

860 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