Mysql记录拉取并添加到php数组中

时间:2020-12-22 23:13:10

标签: php arrays

这是我的静态数组数据:

$order = \EasyPost\Order::create(array(
    "from_address" => $from_address,
    "to_address" => $to_address,
    "shipments" => array(
        array(
            "parcel" => array("length" => 12.0, "width" => 10.5, "height" => 6.8, "weight" => 12),
         
        ),
        array(
            "parcel" => array("length" => 11.9, "width" => 10.0, "height" => 7.3, "weight" => 18),
            
        ),
    ),
));

我正在尝试从数据库中提取包裹数据并添加到数组中:

$parcels = array();
    while($box = mysqli_fetch_assoc($boxInfo))
    {
        $weight = $box["box_weight"];
        $distance_unit = $box["box_size_unit"];
        $mass_unit = $box["box_weight_unit"];
        $length = $box["package_length"];
        $width = $box["package_width"];
        $height = $box["packag_height"];
    
        $parcels[] = array(
        "length"  => $length, 
        "width" => $width, 
        "height" => $height, 
        "weight" => $weight);
    
    }
$order = \EasyPost\Order::create(array(
    "from_address" => $from_address,
    "to_address" => $to_address,
    "shipments" => array(
        array(
            "parcel" => array($parcels),
            ),
       
    ),
));

似乎不起作用。我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

$parcels[] = array (
                 'parcel' => array(
                                  "length"  => $length, 
                                  "width" => $width, 
                                  "height" => $height, 
                                  "weight" => $weight
                                  )
                  )
 ;

然后稍后分配给“shipments”键

"shipments" => $parcels

如果您的目的是获取静态数据示例中的数据。

相关问题