Hello people :)

i'm trying to insert query result into 2 dimensional array

i have tried the following way..

$query=mysql_query("...");
while($row=mysql_fetch_array($query)){
			for($i=0;$i<=$index;$i++){
				if(isset($myarray[$i]['array1'])|| isset($myarray[$i]['array2'])){
					array_unshift($myarray[$i]['array1'],$row['study_period']);
					array_unshift($myarray[$i]['array2'],$row['test']);
				}//endif
			}//endfor
		}//endwhile

But, it failed

i also tried..

$query=mysql_query("...");
while($row=mysql_fetch_array($query)){
		$myarray=array(			array("array1"=>$row['study_period'],"array2"=>$row['test'])
										);	
		}//endwhile

it failed too since it only take the first index of array(myarray[0][]).

I think i'll use the second but i need the array to increment its index as in the first. i wonder how to do this with the second way.

I hope i made my question clear :D

Thank you :)

Can you show us the complete script? I'm doubting that it has something with the $index.

Really? So, you mean that the script should work?

it's the complete code (not the whole code though..)

//$maxIndexC2StudiJM adalah jumlah item maksimal dalam C2 untuk atribut masa studi dan jalur_masuk
$maxIndexC2StudiJM=$maxL1Studi*$maxL1JM;

//$countC2ItemIpkJM adalah jumlah baris masing-masing item C2
$countC2ItemStudiJM=array();

//$strC2ItemIpkJM adalah nama item-item C2
$strC2ItemStudiJM=array();

//mendapatkan seluruh item gabungan IPK dan jalur masuk
$jmlItemL1Ipk=array_count_values($strL1ItemIpk);
$jmlItemL1JM=array_count_values($strL1ItemJM);
foreach($jmlItemL1Studi as $studi=>$jmlStudi){
	foreach($jmlItemL1JM as $jm=>$jmlJM){
		if($angkatan!='0' && $prodi!='0'){
			$query=mysql_query("SELECT dataanalisis.masa_studi,jalurmasuks.tes from dataanalisis INNER JOIN students ON dataanalisis.id_student=students.nim INNER JOIN jalurmasuks ON students.id_jalurmasuk=jalurmasuks.id WHERE students.id_prodi='$prodi' AND students.angkatan='$angkatan' AND masa_studi='$studi' AND tes='$jm'");	
		}
		else if($angkatan=='0' && $prodi!='0'){
			$query=mysql_query("SELECT dataanalisis.masa_studi,jalurmasuks.tes from dataanalisis INNER JOIN students ON dataanalisis.id_student=students.nim INNER JOIN jalurmasuks ON students.id_jalurmasuk=jalurmasuks.id  WHERE students.id_prodi='$prodi' AND masa_studi='$studi' AND tes='$jm'");
		}
		else if($angkatan!='0' && $prodi=='0'){
			$query=mysql_query("SELECT dataanalisis.masa_studi,jalurmasuks.tes from dataanalisis INNER JOIN students ON dataanalisis.id_student=students.nim INNER JOIN jalurmasuks ON students.id_jalurmasuk=jalurmasuks.id WHERE students.angkatan='$angkatan' AND masa_studi='$studi' AND tes='$jm'");	
		}
		else if($angkatan=='0' && $prodi=='0'){
			$query=mysql_query("SELECT dataanalisis.masa_studi,jalurmasuks.tes from dataanalisis INNER JOIN students ON dataanalisis.id_student=students.nim INNER JOIN jalurmasuks ON students.id_jalurmasuk=jalurmasuks.id  WHERE masa_studi='$studi' AND tes='$jm'");	
		}
		
		while($row=mysql_fetch_array($query)){
			for($i=0;$i<=$maxIndexC2StudiJM;$i++){
				if(isset($strC2ItemStudiJM[$i]['masa_studi'])|| isset($strC2ItemStudiJM[$i]['jm'])){
					//$strC2ItemStudiJM=array(
						//					array("masa_studi"=>$row['masa_studi'],"jm"=>$row['tes'])
							//				);
					array_unshift($strC2ItemStudiJM[$i]['masa_studi'],$row['masa_studi']);
					array_unshift($strC2ItemStudiJM[$i]['jm'],$row['tes']);
				}//endif
			}//endfor
		}//endwhile	
	}//endforeach
}
print_r ($strC2ItemStudiJM);

I don't know why this script won't work but this is the probably reason why:

1. Try inserting some echo inside the if statement

if(isset($strC2ItemStudiJM[$i]['masa_studi'])|| isset($strC2ItemStudiJM[$i]['jm'])){

echo "Debug test";

<some following statement...>
}

I think that the initial values you have given to the if statement is false. So therefore I think that its not going inside the IF statement. Inserting that echo will gonna let us know if its really going inside.


2. Try changing the

array_unshift($strC2ItemStudiJM[$i]['masa_studi'],$row['masa_studi']);
		array_unshift($strC2ItemStudiJM[$i]['jm'],$row['tes']);

into something like this

$strC2ItemStudiJM[$i]['masa_studi'] = $row['masa_studi'];
	        $strC2ItemStudiJM[$i]['jm'] = $row['tes'];

But I would go into number 1. since I think that the variables aren't set and will never be until the end of the loop.


I hope this helps!

<?php
$query = mysql_query("...");
$myarray = array();  // Initialise an empty array.
while ($row = mysql_fetch_array($query)) {
  $myarray[] = array($row['study_period'], $row['test']); // Add an array as the next element of the initialised array.
}
?>
<?php
$query = mysql_query("...");
$myarray = array();  // Initialise an empty array.
while ($row = mysql_fetch_array($query)) {
  $myarray[] = array($row['study_period'], $row['test']); // Add an array as the next element of the initialised array.
}
?>

Thank you!! :)
Your code works. But, how to insert $row into myarray[]["array1"]. i want to insert certain value into certain array key. Thank you.

I've fixed it. it's simple like this

$strC2ItemStudiJM[]=array('masa_studi'=>$row['masa_studi'],'jalur_masuk'=>$row['tes']);

Thank you, Nett and Briptoniah :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.