Jump to content

Recommended Posts

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);

 

Link to comment
https://forums.phpfreaks.com/topic/316076-loop-though-array-with-sql-variables/
Share on other sites

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
                        )

                )

 

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
                                        )

                                )

                        )

                )

 

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 by RyanFitz

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 by ginerjm
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.