Shadowing Posted March 25, 2012 Share Posted March 25, 2012 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 https://forums.phpfreaks.com/topic/259693-cycling-through-a-multidimensional-array/ Share on other sites More sharing options...
Drummin Posted March 25, 2012 Share Posted March 25, 2012 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 https://forums.phpfreaks.com/topic/259693-cycling-through-a-multidimensional-array/#findComment-1330982 Share on other sites More sharing options...
Shadowing Posted March 25, 2012 Author Share Posted March 25, 2012 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 https://forums.phpfreaks.com/topic/259693-cycling-through-a-multidimensional-array/#findComment-1330985 Share on other sites More sharing options...
Shadowing Posted March 25, 2012 Author Share Posted March 25, 2012 The end of the array looks like this [14] => Array ( [b_x] => 435 [b_y] => 171 ) ) 1 I dont understand how or why there is a 1 on the end. so im thinking thats where this 1 is coming from Link to comment https://forums.phpfreaks.com/topic/259693-cycling-through-a-multidimensional-array/#findComment-1330986 Share on other sites More sharing options...
Drummin Posted March 25, 2012 Share Posted March 25, 2012 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 https://forums.phpfreaks.com/topic/259693-cycling-through-a-multidimensional-array/#findComment-1330990 Share on other sites More sharing options...
Drummin Posted March 25, 2012 Share Posted March 25, 2012 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 https://forums.phpfreaks.com/topic/259693-cycling-through-a-multidimensional-array/#findComment-1330992 Share on other sites More sharing options...
Shadowing Posted March 25, 2012 Author Share Posted March 25, 2012 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 https://forums.phpfreaks.com/topic/259693-cycling-through-a-multidimensional-array/#findComment-1331006 Share on other sites More sharing options...
Shadowing Posted March 25, 2012 Author Share Posted March 25, 2012 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 https://forums.phpfreaks.com/topic/259693-cycling-through-a-multidimensional-array/#findComment-1331010 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.