Jump to content

multidimensional array


aebstract

Recommended Posts

$indexqpH[] = array(regid => "$r2[regid]",
                      firstname => "$r2[firstname]",
                      lastname => "$r2[lastname]",
                      eventid => "$r2[eventid]",
                      q1 => "$r2[q1]",
                      q2 => "$r2[q2]",
                      q3 => "$r2[q2]",
                    );

 

This get's me an array that I should be able to call out by using $indexqpH[1][q1], $indexqpH[1][q2], etc. Right? I have one more variable I would like to set, somehow make it another dimension deep (can do that okay) but want to name it, not sure how? I know this is simple. So that I can have:

 

$indexqpH[class1][1][q1]

$indexqpH[class2][1][q1]

$indexqpH[class2][2][q1]

$indexqpH[class1][2][q1]

$indexqpH[class3][1][q1]

 

So I can do something similar to this.

 

Link to comment
https://forums.phpfreaks.com/topic/199925-multidimensional-array/
Share on other sites

What's the [1] for? You can omit that. On a side note, you should put quotes around all those indexes.

 

And I don't know what you're trying to do that would require the setup you want, but you can do it via:

$indexqpH = array( 'class1' => array(1 => array('q1' => 'test')));

I just had it as an example of how I might use the information.

 

		while($r2=mysql_fetch_array($query2))
	{


if (($r2[q1] < 4.70) && ($r2[q2] < 4.70) && ($r2[q3] < 4.70)) {
$indexqpH = array( '$var' => array(array(regid => "$r2[regid]",
                      firstname => "$r2[firstname]",
                      lastname => "$r2[lastname]",
                      eventid => "$r2[eventid]",
                      q1 => "$r2[q1]",
                      q2 => "$r2[q2]",
                      q3 => "$r2[q2]",
                    )));
             											} else {
$indexqpL = array( '$var' => array(array(regid => "$r2[regid]",
                      firstname => "$r2[firstname]",
                      lastname => "$r2[lastname]",
                      eventid => "$r2[eventid]",
                      q1 => "$r2[q1]",
                      q2 => "$r2[q2]",
                      q3 => "$r2[q2]",
                    )));
             												}


	}

}



$count1 = count($indexqpH[$var]);
$count2 = count($indexqpL[$var]);


for ($i = 1; $i <= $count1; $i++) {
$content .= "$indexqpH[$var][$i][firstname] $indexqpH[$var][$i][lastname]<br />";
}

 

I think I'm getting an endless loop here, when I refresh the page after uploading this it just sits and loads.

Basically, the structure I'm trying to get is:

 

$var = 'class name here';

$indexqpH[$var][#][info]

 

info being firstname, lastname, etc. The number to be the array index for each of said info. It's in a loop where $var will be set to 4 different things, so I should end up with 4 arrays that I can look at based on class.

$query2 = mysql_query("
SELECT registrations.firstname, participants.eventid, registrations.lastname, participants.q1, participants.q2, participants.q3, participants.q1s, participants.q2s, participants.q3s, participants.q1rt, participants.q2rt, participants.q3rt, participants.regid
FROM participants
INNER JOIN registrations ON registrations.id = participants.regid
WHERE participants.eventid = $_GET[event] AND registrations.class = '$var'") or DIE(mysql_error());
if(mysql_num_rows($query)!=0){


	while($r2=mysql_fetch_array($query2))
	{

$var2 = substr($var, 0, 4);

if (($r2[q1] < $var2) && ($r2[q2] < $var2) && ($r2[q3] < $var2)) {
$indexqpL = array($var => array(regid => "$r2[regid]",
                      firstname => "$r2[firstname]",
                      lastname => "$r2[lastname]",
                      eventid => "$r2[eventid]",
                      q1 => "$r2[q1]",
                      q2 => "$r2[q2]",
                      q3 => "$r2[q3]",
                    ));
             											} else {
$indexqpH = array($var => array(regid => "$r2[regid]",
                      firstname => "$r2[firstname]",
                      lastname => "$r2[lastname]",
                      eventid => "$r2[eventid]",
                      q1 => "$r2[q1]",
                      q2 => "$r2[q2]",
                      q3 => "$r2[q3]",
                    ));
             												}


	}

}
echo "<pre>";
var_dump($indexqpH);
echo "</pre>";

 

This query resulted in these results:

 

array(1) {

  ["4.70 Index"]=>

  array(7) {

    ["regid"]=>

    string(3) "217"

    ["firstname"]=>

    string(4) "Jeff"

    ["lastname"]=>

    string(9) "Wilkerson"

    ["eventid"]=>

    string(1) "1"

    ["q1"]=>

    string(5) "5.026"

    ["q2"]=>

    string(5) "4.902"

    ["q3"]=>

    string(5) "4.902"

  }

}

 

array(1) {

  ["5.30 Index"]=>

  array(7) {

    ["regid"]=>

    string(3) "207"

    ["firstname"]=>

    string(5) "Shawn"

    ["lastname"]=>

    string(8) "Mcdonald"

    ["eventid"]=>

    string(1) "1"

    ["q1"]=>

    string(5) "5.546"

    ["q2"]=>

    string(5) "5.546"

    ["q3"]=>

    string(5) "5.546"

  }

}

 

array(1) {

  ["6.00 Index"]=>

  array(7) {

    ["regid"]=>

    string(3) "206"

    ["firstname"]=>

    string(6) "Ricki "

    ["lastname"]=>

    string(9) "Blackwell"

    ["eventid"]=>

    string(1) "1"

    ["q1"]=>

    string(5) "6.085"

    ["q2"]=>

    string(5) "6.085"

    ["q3"]=>

    string(5) "6.045"

  }

}

 

array(1) {

  ["7.00 Index"]=>

  array(7) {

    ["regid"]=>

    string(3) "196"

    ["firstname"]=>

    string(4) "Adam"

    ["lastname"]=>

    string(8) "Stratton"

    ["eventid"]=>

    string(1) "1"

    ["q1"]=>

    string(5) "7.481"

    ["q2"]=>

    string(5) "7.440"

    ["q3"]=>

    string(5) "7.440"

  }

}

 

 

That's 1 person from each class, but there are a lot more than that. 4.70 has 10 people alone. I guess I'm not combining them and it keeps overwriting? I'm wanting to place each of those results IN TO the "class" array as its own index, hmm.. maybe I just need an extra array in the mix?

Erm, getting back some odd results:

 


$query2 = mysql_query("
SELECT registrations.firstname, participants.eventid, registrations.lastname, participants.q1, participants.q2, participants.q3, participants.q1s, participants.q2s, participants.q3s, participants.q1rt, participants.q2rt, participants.q3rt, participants.regid
FROM participants
INNER JOIN registrations ON registrations.id = participants.regid
WHERE participants.eventid = $_GET[event] AND registrations.class = '$var'") or DIE(mysql_error());
if(mysql_num_rows($query)!=0){


	while($r2=mysql_fetch_array($query2))
	{

$var2 = substr($var, 0, 4);

if (($r2[q1] < $var2) && ($r2[q2] < $var2) && ($r2[q3] < $var2)) {
$indexqpL[$var] = array(array(regid => "$r2[regid]",
                      firstname => "$r2[firstname]",
                      lastname => "$r2[lastname]",
                      eventid => "$r2[eventid]",
                      q1 => "$r2[q1]",
                      q2 => "$r2[q2]",
                      q3 => "$r2[q3]",
                    ));
             											} else {
$indexqpH[$var] = array(array(regid => "$r2[regid]",
                      firstname => "$r2[firstname]",
                      lastname => "$r2[lastname]",
                      eventid => "$r2[eventid]",
                      q1 => "$r2[q1]",
                      q2 => "$r2[q2]",
                      q3 => "$r2[q3]",
                    ));
             												}


	}

}
echo "<pre>";
var_dump($indexqpH);
echo "</pre>";

 

 

array(1) {

  ["4.70 Index"]=>

  array(1) {

    [0]=>

    array(7) {

      ["regid"]=>

      string(3) "217"

      ["firstname"]=>

      string(4) "Jeff"

      ["lastname"]=>

      string(9) "Wilkerson"

      ["eventid"]=>

      string(1) "1"

      ["q1"]=>

      string(5) "5.026"

      ["q2"]=>

      string(5) "4.902"

      ["q3"]=>

      string(5) "4.902"

    }

  }

}

 

array(2) {

  ["4.70 Index"]=>

  array(1) {

    [0]=>

    array(7) {

      ["regid"]=>

      string(3) "217"

      ["firstname"]=>

      string(4) "Jeff"

      ["lastname"]=>

      string(9) "Wilkerson"

      ["eventid"]=>

      string(1) "1"

      ["q1"]=>

      string(5) "5.026"

      ["q2"]=>

      string(5) "4.902"

      ["q3"]=>

      string(5) "4.902"

    }

  }

  ["5.30 Index"]=>

  array(1) {

    [0]=>

    array(7) {

      ["regid"]=>

      string(3) "207"

      ["firstname"]=>

      string(5) "Shawn"

      ["lastname"]=>

      string(8) "Mcdonald"

      ["eventid"]=>

      string(1) "1"

      ["q1"]=>

      string(5) "5.546"

      ["q2"]=>

      string(5) "5.546"

      ["q3"]=>

      string(5) "5.546"

    }

  }

}

 

array(3) {

  ["4.70 Index"]=>

  array(1) {

    [0]=>

    array(7) {

      ["regid"]=>

      string(3) "217"

      ["firstname"]=>

      string(4) "Jeff"

      ["lastname"]=>

      string(9) "Wilkerson"

      ["eventid"]=>

      string(1) "1"

      ["q1"]=>

      string(5) "5.026"

      ["q2"]=>

      string(5) "4.902"

      ["q3"]=>

      string(5) "4.902"

    }

  }

  ["5.30 Index"]=>

  array(1) {

    [0]=>

    array(7) {

      ["regid"]=>

      string(3) "207"

      ["firstname"]=>

      string(5) "Shawn"

      ["lastname"]=>

      string(8) "Mcdonald"

      ["eventid"]=>

      string(1) "1"

      ["q1"]=>

      string(5) "5.546"

      ["q2"]=>

      string(5) "5.546"

      ["q3"]=>

      string(5) "5.546"

    }

  }

  ["6.00 Index"]=>

  array(1) {

    [0]=>

    array(7) {

      ["regid"]=>

      string(3) "206"

      ["firstname"]=>

      string(6) "Ricki "

      ["lastname"]=>

      string(9) "Blackwell"

      ["eventid"]=>

      string(1) "1"

      ["q1"]=>

      string(5) "6.085"

      ["q2"]=>

      string(5) "6.085"

      ["q3"]=>

      string(5) "6.045"

    }

  }

}

 

array(4) {

  ["4.70 Index"]=>

  array(1) {

    [0]=>

    array(7) {

      ["regid"]=>

      string(3) "217"

      ["firstname"]=>

      string(4) "Jeff"

      ["lastname"]=>

      string(9) "Wilkerson"

      ["eventid"]=>

      string(1) "1"

      ["q1"]=>

      string(5) "5.026"

      ["q2"]=>

      string(5) "4.902"

      ["q3"]=>

      string(5) "4.902"

    }

  }

  ["5.30 Index"]=>

  array(1) {

    [0]=>

    array(7) {

      ["regid"]=>

      string(3) "207"

      ["firstname"]=>

      string(5) "Shawn"

      ["lastname"]=>

      string(8) "Mcdonald"

      ["eventid"]=>

      string(1) "1"

      ["q1"]=>

      string(5) "5.546"

      ["q2"]=>

      string(5) "5.546"

      ["q3"]=>

      string(5) "5.546"

    }

  }

  ["6.00 Index"]=>

  array(1) {

    [0]=>

    array(7) {

      ["regid"]=>

      string(3) "206"

      ["firstname"]=>

      string(6) "Ricki "

      ["lastname"]=>

      string(9) "Blackwell"

      ["eventid"]=>

      string(1) "1"

      ["q1"]=>

      string(5) "6.085"

      ["q2"]=>

      string(5) "6.085"

      ["q3"]=>

      string(5) "6.045"

    }

  }

  ["7.00 Index"]=>

  array(1) {

    [0]=>

    array(7) {

      ["regid"]=>

      string(3) "196"

      ["firstname"]=>

      string(4) "Adam"

      ["lastname"]=>

      string(8) "Stratton"

      ["eventid"]=>

      string(1) "1"

      ["q1"]=>

      string(5) "7.481"

      ["q2"]=>

      string(5) "7.440"

      ["q3"]=>

      string(5) "7.440"

    }

  }

}

 

Not what I'm after, almost like stabbing in the dark over here but I'm gonna keep trying until I figure something out (or get a little help?)

I've got something I've been working on and have tried 2 different methods. One using a different database setup and another using these arrays, which I can't get them to set correctly. I'm sitting here just wasting time because I can't figure it out and no one has an answer. Though, I'm basically stuck here until I do figure it out.

 

I don't get why this:

$query2 = mysql_query("
SELECT registrations.firstname, participants.eventid, registrations.lastname, participants.q1, participants.q2, participants.q3, participants.q1s, participants.q2s, participants.q3s, participants.q1rt, participants.q2rt, participants.q3rt, participants.regid
FROM participants
INNER JOIN registrations ON registrations.id = participants.regid
WHERE participants.eventid = $_GET[event] AND registrations.class = '$var'") or DIE(mysql_error());
if(mysql_num_rows($query)!=0){

$indexL[$var] = array();

	while($r2=mysql_fetch_array($query2))
	{
$indexL[$var] = array("$r2[firstname]", "$r2[lastname]", "$var");
}
}

 

Is only setting this:

Array

(

    [4.70 Index] => Array

        (

            [0] => Jeff

            [1] => Wilkerson

            [2] => 4.70 Index

        )

 

    [5.30 Index] => Array

        (

            [0] => Ronnie

            [1] => Clayton

            [2] => 5.30 Index

        )

 

    [6.00 Index] => Array

        (

            [0] => Ricki

            [1] => Blackwell

            [2] => 6.00 Index

        )

 

    [7.00 Index] => Array

        (

            [0] => Adam

            [1] => Stratton

            [2] => 7.00 Index

        )

 

)

 

 

 

Even though this isn't what I'm going for, it's still a very basic array just to see what is up. I'm only returning one result per class, but my query doesn't have that limit on it?

Does this work for you:

 

<?php
$indexqpL = $indexqpH = array();
$content = $arr = '';

while ( $r2 = mysql_fetch_assoc($query2) ) {
$arr = 'indexqpL';
if ($r2[q1] < 4.70 && $r2[q2] < 4.70 && $r2[q3] < 4.70) {
	$arr = 'indexqpH';
}
${$arr} = array(
	$var => array(
		array(
			'regid' => $r2['regid'],
			'firstname' => $r2['firstname'],
			'lastname' => $r2['lastname'],
			'eventid' => $r2['eventid'],
			'q1' => $r2['q1'],
			'q2' => $r2['q2'],
			'q3' => $r2['q2']
		)
	)
);
}

$count1 = count($indexqpH);
$count2 = count($indexqpL);


for ($i = 1; $i <= $count1; $i++) {
$content .= sprintf("%s %s<br />", $indexqpH[$var]['firstname'], $indexqpH[$var]['lastname']);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.