While loop output print as a 2d array

Hello,

I want to print while loop output as a 2d array and want to access it outside loop. In my code $data is an associative array and i want my output as 2d array Here is my code -

include_Once("postgresconnection.php"); 
$T1   = $_POST['time1'];
$T2   = $_POST['time2'];
$venue   = $_POST['venue'];
$building   = $_POST['buildingname'];
$floor     = $_POST['floorlevel'];

if (isset($T1)&&($T2)){
$result = pg_query($db,"SELECT landmark,radio FROM fingerprinttimelog WHERE starttime = '" . trim($T1) . "' AND endtime = '" . trim($T2) . "' AND venue = '" . trim($venue) . "' AND buildingname = '" . trim($building) . "' AND floorlevel = '" . trim($floor) . "'");  	
}
else{
	$result = pg_query($db,"SELECT landmark,radio FROM fingerprinttimelog WHERE venue = '" . trim($venue) . "' AND buildingname = '" . trim($building) . "' AND floorlevel = '" . trim($floor) . "'");  
}
  
$i=0;
while($row=pg_fetch_assoc($result)){echo "<tr>";  
$data[$i] = array('landmark'=>$row['landmark'], 'radio'=>$row['radio']);

  $i++;
}
print_r  ($data);

I’m a bit confused at what you’re asking, can you elaborate on the question please?

Here i’m getting output like this -
Array ( [0] => Array ( [landmark] => 5 [radio] => 2.4 ) [1] => Array ( [landmark] => 5 [radio] => 5 ) [2] => Array ( [landmark] => 11 [radio] => 2.4 ) [3] => Array ( [landmark] => 11 [radio] => 5 ) [4] => Array ( [landmark] => 29 [radio] => 2.4 ) [5] => Array ( [landmark] => 29 [radio] => 5 ) [6] => Array ( [landmark] => 31 [radio] => 2.4 ) [7] => Array ( [landmark] => 31 [radio] => 5 ) [8] => Array ( [landmark] => 32 [radio] => 2.4 ) [9] => Array ( [landmark] => 32 [radio] => 5 ) )

i want to compare this data so when i’m comparing using for loop -
for($row=1; $row<40; $row++){ for($col=0; $col<2;$col++){ if($row==$data[$col][0]){ if($data[$j][1]==2.4) { //My Code Here } else{ echo " not getting data"; } } }
I want to compare like these code but here i just have 1d array ($data) so i’m not able to do these.

And where does $j come from in that code snippet? And for that matter how do the other variables compare to the layout of your new array? Are you just running through looking for array elements where “radio” = 2.4?

foreach ($data as $index => $subar) {
  if ($subar['radio'] == 2.4) {
    // your code here
    }
  else {
    // not getting data
    }
  }

If that’s not it, can you give a bit more detail about what you’re trying to achieve?

Thanks droopsnoot,

I need to compare radio ==2.4 && radio ==5. But the thing is in my output value i have same value multiple times like 5 is twice and it is checking radio value just once. so first i want to compare $i == $subar[‘Landmark’] then want to check single landmark count like for 5 is 2, for 11 its 2 or the radio values for that landmark like for 5 i have radio values 2.4 && 5.

Post your HTML form. This will help us to understand your parmeters.

In my html form, I have 3 parameters -
venue, buildingname, floorlevel but fromdatabase i’m fetching landmark and radio.
$result = pg_query($db,“SELECT landmark,radio FROM fingerprinttimelog WHERE venue = '” . trim($venue) . “’ AND buildingname = '” . trim($building) . “’ AND floorlevel = '” . trim($floor) . “'”);

That won’t happen, radio cannot be both 2.4 and 5 at the same time.

I’m not sure your explanation makes it much clearer, to be honest. You have the same landmark value more than once, but the foreach loop will look at every entry, whether it’s the same or different landmark values.

Maybe if you posted a sample of what you want to end up with, it might make it easier to understand.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.