Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

melx's avatar
Level 4

How to Pass multiple array in External Api

Kindly i want to pass multiple array in external api, and get the response

This is what i tried but it return on one array 0 => "7200408497". instead of 12 array

dd($jsonData)

          array:4 [▼
            "error" => null
            "payload" => array:2 [▶]
            "count" => 2
            "total" => 0
          ]

dd($idsArray)

              array:12 [▼
                0 => "7200408497"
                1 => "7200408223"
                2 => "7291213211"
                3 => "7200408373"
                4 => "7200408652"
                5 => "7291213314"
                6 => "7200408397"
                7 => "7200408375"
                8 => "7200408204"
                9 => "7200408437"
                10 => "7200408639"
                11 => "7200408141"
                ]

My Controller

          public function DeviceStatus(Request $request)
        {

          $queryinput =$request->deviceNumber;
          $idsArray =explode(" ", $queryinput);
          dd($idsArray);

          // $jsonData = [];
          foreach ($idsArray as $key=>$id) {

            try {
                  $response = Http::get('http://http://127.0.0.1:8000/extapi/v12/DeviceData/ByDeviceDataSerial/'.$id.'');
                  $jsonData = $response->json();
                  // array_push($jsonData,$response);

            } catch (\Exception $exception) {
                  // toastr()->Error($exception->getMessage());
            return back()->withError($exception->getMessage());
            }


            dd($idsArray);

            return view('tracedevice.ectsdevicestatus',compact('jsonData'));

          }

What can i do to loop that and get that data

0 likes
5 replies
CarlEOgden's avatar

Hi

Complete guess here, but what if you do:-

return response()->json($idsArray);

Instead of returning a view at the end?

melx's avatar
Level 4

["7200408497","7200408223","7291213211","7200408373","7200408652","7291213314","7200408397","7200408375","7200408204","7200408437","7200408639","7200408141"]

this what i get sir

CarlEOgden's avatar

What about:-

$response = []; $response['idArray'] = $jsonData; return response()->json($response);

melx's avatar
Level 4

it return one idArray

frankielee's avatar

If I understand correctly, you are trying to pass the $idsArray to the API. Instead of using a foreach loop, you want to get all the device data by calling the API 1 time only.

Reference: https://laravel.com/docs/8.x/http-client#get-request-query-parameters

Make sure the external API accept array

$response = Http::get('http://127.0.0.1:8000/extapi/v12/DeviceData/ByDeviceDataSerial', [
    'id' =>  [
                0 => "7200408497"
                1 => "7200408223"
                2 => "7291213211"
                3 => "7200408373"
                4 => "7200408652"
                5 => "7291213314"
                6 => "7200408397"
                7 => "7200408375"
                8 => "7200408204"
                9 => "7200408437"
                10 => "7200408639"
                11 => "7200408141"
                ]
]);

Please or to participate in this conversation.