如何将二维数组发布到 API

我面临的问题可能看起来很简单,但是由于我无法尝试,因此变得很麻烦。


我需要将数据发布到 API。我正在使用 cURL PHP 执行此操作,因此我从表单中获取数据,将其存储在变量中,并将这些变量存储在要发送的数组中,但我需要发送的参数之一是数组本身:


此信息旨在描述客户:


"administrative": false,

"alias_numbers": [

   {

      "ac": "858",

      "cc": 34,

      "number_id": 3161,

      "sn": "880961"

   },

   {

      "ac": "958",

      "cc": 34,

      "number_id": 3239,

      "sn": "514"

   },

   {

      "ac": "465",

      "cc": 34,

      "number_id": 3241,

      "sn": "363983"

   },

   {

      "ac": "858",

      "cc": 34,

      "number_id": 3243,

      "sn": "88085692"

   },

   {

      "ac": "951",

      "cc": 34,

      "number_id": 3245,

      "sn": "598217"

   }

],

"customer_id": 1,

我不确定如何将 alias_numbers 分配给我的 dataArray 中的值,最多可以有 10 个,但这是在表单中控制的。这就是我想出的:


$dataArray = array(

    "customer_id" => $subscriberCustomerId,

    "email" => $subscriberEmail,

    "username" => $subscriberComRegNum,

    "external_id" => $subscriberComRegNum,

    "primary_number" => array(

        "cc" => $subscriberPrimaryNumber["cc"],

        "ac" => $subscriberPrimaryNumber["ac"],

        "sn" => $subscriberPrimaryNumber["sn"]

    ),

    for ($i=0; $i < sizeof($arrayAliasNumbers); $i++) { 

        "alias_numbers" => array(

            "cc" => $arrayAliasNumbers[$i]["cc"],

            "ac" => $arrayAliasNumbers[$i]["ac"],

            "sn" => $arrayAliasNumbers[$i]["sn"]

        ),

    }

    "domain_id" => $subscriberDomainId,

    "administrative" => $subscriberAdministrative,

    "status" => $subscriberStatus

);

您可以看到有一个 primary_number 没有造成任何麻烦,因为只能有一个,但是有了这些别名,我完全迷失了。

缥缈止盈
浏览 103回答 1
1回答

吃鸡游戏

单独制作数组,然后将其添加到您的数组中。$tmp = [];for ($i=0; $i < sizeof($arrayAliasNumbers); $i++) {&nbsp;&nbsp; &nbsp; $tmp[] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "cc" => $arrayAliasNumbers[$i]["cc"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ac" => $arrayAliasNumbers[$i]["ac"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "sn" => $arrayAliasNumbers[$i]["sn"]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];}$dataArray = array(&nbsp; &nbsp; "customer_id" => $subscriberCustomerId,&nbsp; &nbsp; "email" => $subscriberEmail,&nbsp; &nbsp; "username" => $subscriberComRegNum,&nbsp; &nbsp; "external_id" => $subscriberComRegNum,&nbsp; &nbsp; "primary_number" => array(&nbsp; &nbsp; &nbsp; &nbsp; "cc" => $subscriberPrimaryNumber["cc"],&nbsp; &nbsp; &nbsp; &nbsp; "ac" => $subscriberPrimaryNumber["ac"],&nbsp; &nbsp; &nbsp; &nbsp; "sn" => $subscriberPrimaryNumber["sn"]&nbsp; &nbsp; ),&nbsp; &nbsp; "alias_numbers"&nbsp; => $tmp,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// <- added here&nbsp; &nbsp; "domain_id" => $subscriberDomainId,&nbsp; &nbsp; "administrative" => $subscriberAdministrative,&nbsp; &nbsp; "status" => $subscriberStatus);
打开App,查看更多内容
随时随地看视频慕课网APP