Solved

Using PHP to create Multidimensional Arrays

Posted on 2007-03-29
2
182 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
Comment Utility
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
Comment Utility
$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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

7 Experts available now in Live!

Get 1:1 Help Now