12 Replies - 284 Views - Last Post: 23 September 2013 - 09:08 PM Rate Topic: -----

#1 BuhRock  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 255
  • Joined: 21-February 10

Associative array help

Posted 11 September 2013 - 03:13 PM

So I need to do an assignment that has a form of checkboxes. The form includes Teachers and classes. I need help deciding on how to use an associative array storing these. I need to use an associative array, but didn't know how to.

So for instance, Dr. Carter teaches 120, 220, and 340. Dr. Barland teachers 120, and 220. Dr. Chase just teaches 220.

I know an associative array works like this:

$car = array ("weight"=>"100kg", "year"=>"2004", "price"=>"7000"); 


How could I make the array where it can store multiple classes for one professor? I don't need the code for anything, just some syntax help with the arrays on how that would work.

Is This A Good Question/Topic? 1
  • +

Replies To: Associative array help

#2 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 376
  • Joined: 29-July 09

Re: Associative array help

Posted 11 September 2013 - 03:18 PM

you can store an array within array.
like:
$academy = array('Carter' => array(120,220,340), 'Barland' => array(120, 220), 'Chase' => array(220));


Is that what youre looking for?

Oh you cna do it this way too
$cad = ['Carter' => [120,220,340], 'Barland' => [120,220], 'Chase' => [220]];


php arrays

This post has been edited by Atspulgs: 11 September 2013 - 03:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 BuhRock  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 255
  • Joined: 21-February 10

Re: Associative array help

Posted 11 September 2013 - 03:23 PM

Well I'm not sure if that would work. The output would work like this:

Say I selected Dr. Chase and Dr. Barland, then the output would be :

Barland: 120, 220
Chase: 220
Was This Post Helpful? 0
  • +
  • -

#4 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 376
  • Joined: 29-July 09

Re: Associative array help

Posted 11 September 2013 - 03:35 PM

Im not quite sure what would be the problem though...

You can do something like:

$cad = ['carter' => ['name' => 'Carter',120,220,340], 'barland' => ['name' => 'Barland',120,220], 'chase' => ['name' => 'Chase' => 220]];



I dont know what else to tell you?
You can always get the keys of the array
and do something like

	$cad = ['Carter' => [120,220,340], 'Barland' => [120,220], 'Chase' => [220]];
	foreach($cad as $key => $value){
		echo '<br/>'.$key.': ';
		for($i=0;$i<count($value);$i++)
 			echo $value[$i].', ';
	}

In that example im using the first array structure rather than the second.

Well you can always do something like
$cad = [['name'=>'Catrer', 'classes'=>'120,220,340'],['name'=>'Barland', 'classes'=>'120,220'],['name'=>'Chase', 'classes'=>'220']];
foreach($cad as $prof) echo $prof['name'].': '.$prof['classes'];


This post has been edited by Atspulgs: 11 September 2013 - 03:43 PM

Was This Post Helpful? 0
  • +
  • -

#5 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 782
  • View blog
  • Posts: 1,662
  • Joined: 30-January 09

Re: Associative array help

Posted 11 September 2013 - 04:09 PM

That code isn't ideal as it doesn't use nested arrays. If you use nested arrays, you need to use implode() to concatenate the nested array contents, like so:
$cad = [['name'=>'Carter', 'classes'=>[120,220,340]],['name'=>'Barland', 'classes'=>[120,220]],['name'=>'Chase', 'classes'=>[220]]];
foreach($cad as $prof)
{
    echo $prof['name'].': '.implode(', ', $prof['classes']);
}


This post has been edited by e_i_pi: 11 September 2013 - 04:11 PM

Was This Post Helpful? 0
  • +
  • -

#6 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 376
  • Joined: 29-July 09

Re: Associative array help

Posted 11 September 2013 - 06:14 PM

Oh yeah, implode would be nice way of removing the nested loop :)
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3382
  • View blog
  • Posts: 9,568
  • Joined: 08-June 10

Re: Associative array help

Posted 11 September 2013 - 10:20 PM

View Poste_i_pi, on 12 September 2013 - 01:09 AM, said:

That code isn't ideal as it doesn't use nested arrays. If you use nested arrays, you need to use implode() to concatenate the nested array contents, like so:
$cad = [['name'=>'Carter', 'classes'=>[120,220,340]],['name'=>'Barland', 'classes'=>[120,220]],['name'=>'Chase', 'classes'=>[220]]];
foreach($cad as $prof)
{
    echo $prof['name'].': '.implode(', ', $prof['classes']);
}


though if you go that far, it may be easier (at least in the long run) to use an array of objects instead an array of arrays.
Was This Post Helpful? 1
  • +
  • -

#8 BuhRock  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 255
  • Joined: 21-February 10

Re: Associative array help

Posted 23 September 2013 - 01:30 PM

Ok, coming back to this as I have a problem with accepting input from a form.

So if you go to https://php.radford....assignment1.php there is the form. Then it goes to another page where I am supposed to process this.

My code on the other page, I had to create an associative array like I mentioned earlier...

I don't see how I can get it to where I can select any checkbox and it will give me some input. As in if I select Carter, it displays all classes that he teaches. Then if I were to select ITEC 120, it would display all professors that teach it.

Right now, if I click ITEC 100, it displays this:

0: I,
1: I,
2: Barland, Carter, Dodson, Davis,

Here is my php code...

<?php
			
			$courses = array(
				"ITEC 100" => array("Introduction to Information Technology", 
					"Introduces students to the fundamental concepts in information technology and mathematical reasoning that provide 
					the technical underpinning for state-of-the-art applications. Widely used computer applications, 
					including office applications, databases, computer communications and networking, serve as examples. ",
					array("Barland", "Carter", "Dodson", "Davis")
				),
				"ITEC 120" => array("Introducstion to Computer Science I",
					"A rigorous, systematic approach to object oriented problem solving and programming.",
					array("Barland", "Pittges")
				),
				"ITEC 220" => array("Introduction to Computer Science II",
					"Continuation, from ITEC 120, of the development of a 
					disciplined approach to programming, with emphasis on data abstraction.",
					array("Barland", "Chase", "Davis")
				),
				"ITEC 340" => array("Database I", "A breadth first introduction to database systems including the 
					relational model, SQL, security, database design, and stored procedures. ",
					array("Barland", "Carter", "Pittges")
				),
				"ITEC 225" => array("Web Programming I",
					"An introduction to web programming for those students that already understand programming fundamentals.
					Includes an introduction to a variety of web based programming languages as well as an
					introduction to operating system environments that support web applications. ",
					array("Davis", "Dodson")
				),
				"ITEC 110" => array("Principles of IT",
					"Provides an introduction to information technology, to the principles that underlie information technology,
					problem solving, and to the fields that make up information technology. ",
					array("Carter", "Dodson"))
			);
			
			$teachers = array(
			"Carter" => array("ITEC 100", "ITEC 340", "ITEC 110"),
			"Barland" => array("ITEC 100", "ITEC 120", "ITEC 220", "ITEC 340",),
			"Dodson" => array("ITEC 100", "ITEC 225", "ITEC 110"),
			"Davis" => array("ITEC 100", "ITEC 225", "ITEC 220"),
			"Pittges" => array("ITEC 120", "ITEC 340"),
			"Chase" => array("ITEC 220"));
			
			
			
		

			$result = $courses[$_POST['cb2']];
		
			foreach($result as $key => $value){
				echo '<br/>'.$key.': ';
				
				for($i=0; $i<count($value); $i++)
					echo $value[$i].', ';
			}
   
			?>

Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3382
  • View blog
  • Posts: 9,568
  • Joined: 08-June 10

Re: Associative array help

Posted 23 September 2013 - 01:59 PM

Quote

Right now, if I click ITEC 100, it displays this:
0: I,
1: I,
2: Barland, Carter, Dodson, Davis,

well, ... letís have a look at your ITEC 100 array:
$itec = array(
    String,
    String,
    Array
)

hence you run 2 strings and 1 array through count() in the for() loop.

question, what happens if you apply count() on a String?

count() said:

Returns the number of elements in var. If var is not an array or not an object with implemented Countable interface, 1 will be returned. There is one exception, if var is NULL, 0 will be returned.

hence for every string, you get one loop cycle which accesses the first character of the string.
Was This Post Helpful? 0
  • +
  • -

#10 BuhRock  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 255
  • Joined: 21-February 10

Re: Associative array help

Posted 23 September 2013 - 02:11 PM

Oh I see, I changed my for loops around to this:

$result = $courses[$_POST['cb2']];
				for($k=0; $k<count($result); $k++){
						echo '<br/>';
						
					if(is_array($result[$k])){
				
						for($i=0; $i<count($result[$k]); $i++)
							echo $result[$k][$i]. ', ';
						}else{
						echo $result[$k];
					}
			
				}


but now this only handles one check box. Do I need another for loop to handle multipe check boxes?
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3382
  • View blog
  • Posts: 9,568
  • Joined: 08-June 10

Re: Associative array help

Posted 23 September 2013 - 02:33 PM

you only ever have one "checkbox" in your PHP 1).


1 - PHP doesn’t even know that this is a checkbox. all it will ever receive is a couple of key-value pairs. the origin of these pairs is of absolutely no interest to PHP. as such, the term "handle a checkbox" is wrong.

This post has been edited by Dormilich: 23 September 2013 - 02:33 PM

Was This Post Helpful? 0
  • +
  • -

#12 BuhRock  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 255
  • Joined: 21-February 10

Re: Associative array help

Posted 23 September 2013 - 08:01 PM

Did you look at my post that had the first page?

This post has been edited by Dormilich: 23 September 2013 - 09:09 PM

Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3382
  • View blog
  • Posts: 9,568
  • Joined: 08-June 10

Re: Associative array help

Posted 23 September 2013 - 09:08 PM

I looked at your given link.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1