Jump to content

cycling through a multidimensional array


Shadowing

Recommended Posts

Hey guys im trying to insert these values from this array into the distance function I made and then it turns it into a string. Took me like 8 hours of problem solving just to find out this wasnt working. really could use some help with this. $arr is the array

 


Array ( [0] => Array ( [b_x] => 85 [b_y] => 71 ) [1] => Array ( [b_x] => 436 [b_y] => 130 )

$b_x = 295;
$b_y = 163;
	foreach($arr as $db) {

			$db_x = $db['b_x'];
			$db_y = $db['b_y'];

			$distance .= distance($b_x,$b_y,$db_y,$db_x) . "-";
		}

end result is suppose to look like $distance-$distance-$distance and so forth

Link to comment
Share on other sites

Not sure if this gets you any closer to what you're looking for.

<?php
$arr=array(
0 => Array ( 'b_x' => 85, 'b_y' => 71 ), 
1 => Array ( 'b_x' => 436, 'b_y' => 130 )
);

$b_x = 295;
$b_y = 163;	
$distance="";
foreach($arr as $db) {
$db_x = $db['b_x'];	
$db_y = $db['b_y'];	
$distance .= "distance"."($b_x,$b_y,$db_y,$db_x)" . "-";	
}
echo "$distance";
?>

Link to comment
Share on other sites

thanks for the responce Drummin

 

What i did was i just pasted what the Print_r was off the array so you would have an idea what the array looks like.

 

 

 

$db_y = $db['b_y'];

 

if I echo  $db_y i get the return of 1

 

This isnt reading the array

$db_x = $db['b_x'];

$db_y = $db['b_y'];

Link to comment
Share on other sites

All that I saw was missing was the start the variable $distance with.

$distance="";

And as I don't see the code for the function "distance" I just modified the line so I could see the results which echoed out.

distance(295,163,71,85)-distance(295,163,130,436)- 

So as far as I can see, each array value is being called.

 

Not sure why you see the one.  I'll look more.

Link to comment
Share on other sites

More than likely your array $arr is not closed.

You have.

Array ( [0] => Array ( [b_x] => 85 [b_y] => 71 ) [1] => Array ( [b_x] => 436 [b_y] => 130 )

I have

Array ( [0] => Array ( [b_x] => 85 [b_y] => 71 ) [1] => Array ( [b_x] => 436 [b_y] => 130 ) ) 

Link to comment
Share on other sites

OMG I figured out my problem, thanks alot drummin

 

There was nothing wrong with me fliping through the array like you said.

 

the db_x and db_y was backwards the x needed to go first. See I was getting the wrong math back from the function and i cant believe i didnt notice that after 12 hours of deep problem solving omg. I was about to go insane cause i knew nothing was wrong

 

$distance .= distance($b_x,$b_y,$db_x,$db_y) . "-";

Link to comment
Share on other sites

Also i had this all wraped in a function so to echo i had to pass it into a list of returns. so i should of tossed the function to the side really echo everything good. Cause for some reason I couldnt echo distance out side the function it was showing up as 1. not sure what the deal is with that.

 

 

Edit: ohhh its cause im trying to return a string using array() then list(). and the string didnt have quotes around it.

Link to comment
Share on other sites

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.