RyanFitz Posted April 1, 2023 Share Posted April 1, 2023 How would I loop though this array, I need payload1 for each custno in my database, and I only get the first record. while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { $custno = $row['cust_no']; $latitude = $row['latitude']; $long= $row['longitude']; $lat = doubleval($latitude); $lon = doubleval($long); $payload1= array( "services" => array( array( "id" => "ss1", "name" => "visit-Joe", "address" => array( "location_id" => "$custno", "lon" => $lon, "lat" => $lat ), "time_windows" => array( array( "earliest" => 1554805329, "latest" => 1554806329 ) ) ), ), "configuration" => array( "routing" => array( "calc_points" => true, "snap_preventions" => array( "motorway", "trunk", "tunnel", "bridge", "ferry" ) ) ) ); } $payload3 = json_encode($payload). ',' . '{"Services":'.json_encode($array).'}'; $payload4 = array_merge($payload, $payload1); //echo json_encode($payload4); //print_r ($payload1); $data = json_decode($json); print_r ($payload1); Quote Link to comment https://forums.phpfreaks.com/topic/316076-loop-though-array-with-sql-variables/ Share on other sites More sharing options...
RyanFitz Posted April 1, 2023 Author Share Posted April 1, 2023 I can use $payload1[$record] $record++ but it adds array to my array and my json post doesn't accept it Quote Link to comment https://forums.phpfreaks.com/topic/316076-loop-though-array-with-sql-variables/#findComment-1606990 Share on other sites More sharing options...
RyanFitz Posted April 1, 2023 Author Share Posted April 1, 2023 Here is what I need [services] => Array ( [0] => Array ( [id] => s-1 [name] => visit-Joe [address] => Array ( [location_id] => 13.375854_52.537338 [lon] => 13.375854 [lat] => 52.537338 ) [size] => Array ( [0] => 1 ) [time_windows] => Array ( [0] => Array ( [earliest] => 1554805329 [latest] => 1554806329 ) ) ) [1] => Array ( [id] => s-2 [name] => serve-Peter [address] => Array ( [location_id] => 13.393364_52.525851 [lon] => 13.393364 [lat] => 52.525851 ) [size] => Array ( [0] => 1 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/316076-loop-though-array-with-sql-variables/#findComment-1606991 Share on other sites More sharing options...
RyanFitz Posted April 1, 2023 Author Share Posted April 1, 2023 If I add $payload1[$record] I get this There is an extra Array ( [] => Array ( Array ( [] => Array ( [services] => Array ( [0] => Array ( [id] => ss1 [name] => visit-Joe [address] => Array ( [location_id] => 34705 [lon] => -76.43676 [lat] => 39.53483 ) [time_windows] => Array ( [0] => Array ( [earliest] => 1554805329 [latest] => 1554806329 ) ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/316076-loop-though-array-with-sql-variables/#findComment-1606992 Share on other sites More sharing options...
RyanFitz Posted April 1, 2023 Author Share Posted April 1, 2023 (edited) I got it I had to create a variable for each array then join them with - $payload4 = array_merge($payload, $services); it works now, displays a json array for each db row $payload1[] = array( 'LineID' => $custno, 'Qty' => $latitude, 'SetCost' => $row['Setcost'], 'MonCost' => $row['Moncost'], 'Postcode' => $row['postcode'] ); $services['services'][] = array( 'id' => $row['firstname'], 'name' => $latitude, "address" => array( "location_id" => "berlin", "lon" => $lon, "lat" => $lat ), ); Edited April 1, 2023 by RyanFitz Quote Link to comment https://forums.phpfreaks.com/topic/316076-loop-though-array-with-sql-variables/#findComment-1606995 Share on other sites More sharing options...
ginerjm Posted April 1, 2023 Share Posted April 1, 2023 (edited) I see a whole lot of arrays in your multiple posts. Which one are you concerned with 'looping through'? Assuming the query results array would be my thought but I don't know why that is the question since you have already begun a loop to go thru that one. So - it must be one of your very own arrays and if you are having trouble looping thru any of them how about just not making it so complex? Haven't seen the query string so don't know how complex your database is but if you are managing to gather all the data you need into a readable query result, then why make all those arrays upon arrays upon arrays? Edited April 1, 2023 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/316076-loop-though-array-with-sql-variables/#findComment-1607001 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.