【问题标题】:Add array to subarray in php在php中将数组添加到子数组
【发布时间】:2016-08-31 11:03:30
【问题描述】:

我有一个数组,如下所示

[0] = Array
    (
        [title] => Khamna,
        [booking_number] = 200003852,
        [quantity] =1,
        [unit_price] = 5.00,
        [gross_total] = 5.00,
    )

[1] = Array
    (
        [title] = Khamna
        [booking_number] = 200003857
        [quantity] = 2
        [unit_price] = 5.00
        [gross_total] = 410.00
    )

[2] = Array
    (
        [title] = Khamna
        [booking_number] = 200003857
        [quantity] = 2
        [unit_price] = 200.00
        [gross_total] = 410.00
    )

[3] = Array
    (
        [title] = Khamna
        [booking_number] = 200003858
        [quantity] = 1
        [unit_price] = 200.00
        [gross_total] = 200.00
    )

我希望结果输出,例如 booking_number 将是数组的键,子数组将基于“unit_price”和“quantity”.. unit_price 和数量作为子数组大于 1 或 2 或 3

[200003852] => Array
    (
        [title] =Khamna
        [gross_total] = 5.00
        [detail] = Array
            ( 0 => array([quantity] = 1
                [unit_price] = 5.00),
        )

    )

[200003857] = Array
    (
        [title] = Khamna
        [gross_total] = 410.00,
        [detail] = Array
            ( 0 => array([quantity] = 2
                [unit_price] = 5.00),
              1 => array([quantity] = 2
                [unit_price] = 200.00),                 
            )
    )

[200003857] = Array
    (
        [title] = Khamna
        [gross_total] = 10.00
        [detail] = Array
            ( 0 => array([quantity] = 2
                [unit_price] = 5.00),

            )
     )

【问题讨论】:

  • 只需迭代您的数组并创建一个看起来像您想要的方式的新数组。到目前为止,您尝试过什么?

标签: php arrays


【解决方案1】:

这会帮助你...

$result = array();
foreach($array as $row) {
    if(!isset($result[$row['booking_number']])) {
        $result[$row['booking_number']] = array(
            'title' => $row['title'],
            'gross_total' => $row['gross_total']
        );
    } 
    $result[$row['booking_number']]['details'][] = array(
        'quantity' => $row['quantity'],
        'unit_price' => $row['unit_price']
    );
}

【讨论】:

    【解决方案2】:

    此代码将帮助您。

    $res = [];
    foreach($array as $v){
      $res[$v['booking_number']] = 
      [
      'title'=>$v['title'],
      'gross_total'=>$v['gross_total'],
      'detail'=>[['quantity'=>$v['quantity']],
                 ['unit_price'=>$v['unit_price']]]
      ];
    }
    echo '<pre>'; print_r($res);
    

    【讨论】:

      【解决方案3】:

      在你的原始数组之后运行它

      foreach ($arra as $key) {
       $arr[$key['booking_number']]=array('title'=> $key['title'],'gross_total'  
           =>$key['gross_total'],'detail'=>array('0' => 
           $key['unit_price'],'1' => $key['quantity']) );
      
               }
      

      注意:$arra 是你的原始数组

      它将使用 echo "&lt;pre&gt;"; print_r($arr); 像这样打印您的数组

          Array
      (
          [200003852] => Array
              (
                  [title] => Khamna
                  [gross_total] => 5
                  [detail] => Array
                      (
                          [0] => 5
                          [1] => 1
                      )
      
              )
      
          [200003857] => Array
              (
                  [title] => Khamna
                  [gross_total] => 410
                  [detail] => Array
                      (
                          [0] => 5
                          [1] => 2
                      )
      
              )
      
      )
      

      【讨论】:

        猜你喜欢
        • 2012-06-18
        • 1970-01-01
        • 1970-01-01
        • 2018-11-22
        • 1970-01-01
        • 2012-01-17
        • 2013-02-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多