在PHP中的数组变量中分配数组

时间:2017-10-12 12:39:05

标签: php

我想在PHP中将数组指定为另一个变量的值。第一个数组是

<?php 
    $jsonData = array(
       'EndUserIp' => $ipAddress,
       'TokenId' => 'a58c1052-c08f-4f40-9a9c-8841de585a14',
       'AdultCount' => 1,
       'ChildCount' => 0,
       'InfantCount' => 0,
       'DirectFlight' => 1,
       'OneStopFlight' => 0,
       'JourneyType' => 1,
       'Segments'
    );
?>

第二个数组是:

<?php
    $segmentVal = array(
       'Origin' => 'DEL', 
       'Destination' => 'CCU',
       'FlightCabinClass' => 2, 
       'PreferredDepartureTime' => '2017-10-13T00:00:00', 
       'PreferredArrivalTime' => '2017-10-13T00:00:00'
    );
?>

我想在第一个数组中将第二个数组指定为Segment变量的值。

3 个答案:

答案 0 :(得分:3)

尝试$ jsonData ['Segments'] = $ segmentVal;

答案 1 :(得分:0)

按顺序进行数组合。

1)

<?php
$segmentVal = array(
      'Origin' => 'DEL',
      'Destination' => 'CCU',
      'FlightCabinClass' => 2,
      'PreferredDepartureTime' => '2017-10-13T00:00:00',
      'PreferredArrivalTime' => '2017-10-13T00:00:00'
   );


  $jsonData = array(
      'EndUserIp' => $ipAddress,
      'TokenId' => 'a58c1052-c08f-4f40-9a9c-8841de585a14',
      'AdultCount' => 1,
      'ChildCount' => 0,
      'InfantCount' => 0,
      'DirectFlight' => 1,
      'OneStopFlight' => 0,
      'JourneyType' => 1,
      'Segments' => $segmentVal
   );
?>

2)

<?php

    $jsonData = array(
          'EndUserIp' => $ipAddress,
          'TokenId' => 'a58c1052-c08f-4f40-9a9c-8841de585a14',
          'AdultCount' => 1,
          'ChildCount' => 0,
          'InfantCount' => 0,
          'DirectFlight' => 1,
          'OneStopFlight' => 0,
          'JourneyType' => 1,
          'Segments' => array(
               'Origin' => 'DEL',
               'Destination' => 'CCU',
               'FlightCabinClass' => 2,
               'PreferredDepartureTime' => '2017-10-13T00:00:00',
               'PreferredArrivalTime' => '2017-10-13T00:00:00'
            )
       );

    ?>

答案 2 :(得分:-1)

我提出了两种可行的方法

第一个是使用json对第二个数组进行编码,如下所示

$jsonData = array(
    'EndUserIp' => "192.168.0.10",
    'TokenId' => 'a58c1052-c08f-4f40-9a9c-8841de585a14',
    'AdultCount' => 1,
    'ChildCount' => 0,
    'InfantCount' => 0,
    'DirectFlight' => 1,
    'OneStopFlight' => 0,
    'JourneyType' => 1,
    'Segments' => json_encode($segmentVal),
);

第二个是类似的 您可以序列化$segmentVal,如下所示

$jsonData = array(
    'EndUserIp'     => "192.168.0.10",
    'TokenId'       => 'a58c1052-c08f-4f40-9a9c-8841de585a14',
    'AdultCount'    => 1,
    'ChildCount'    => 0,
    'InfantCount'   => 0,
    'DirectFlight'  => 1,
    'OneStopFlight' => 0,
    'JourneyType'   => 1,
    'Segments'      => serialize($segmentVal),
);