繁体   English   中英

Php阵列转换和组合

[英]Php array transformation and combination

所以我有这样的阵列

[
    'custid' => [
        'customer_number_1' => '20098374',
        'customer_number_8' => '20098037',
        'customer_number_15' => '20098297'
    ],
    'destid' => [
        'destination_numbers_1' => [
            (int) 0 => '20024838',
            (int) 1 => '20041339'
        ],
        'destination_numbers_8' => [
            (int) 0 => '20008293'
        ],
        'destination_numbers_15' => [
            (int) 0 => '20016969',
            (int) 1 => '20022919',
            (int) 2 => '20025815',
            (int) 3 => '20026005',
            (int) 4 => '20027083',
            (int) 5 => '20045497'
        ]
    ]
]

目标是成对地将cust id与destid合并,并且应该如此

[
    (int) 0 => [
        'user_id' => (int) 1,
        'sap_customer_id' => '20098374',
        'sap_destination_id' => '20024838'
    ],
    (int) 1 => [
        'user_id' => (int) 1,
        'sap_customer_id' => '20098374',
        'sap_destination_id' => '20041339',
    ],
    (int) 2 => [
        'user_id' => (int) 1,
        'sap_customer_id' => '20098037',
        'sap_destination_id' => '20008293,
    ],
    (int) 3 => [
        'user_id' => (int) 1,
        'sap_customer_id' => '20098297'
        'sap_destination_id' => '20016969',
    ],
...

我已尝试使用下面的代码,但我将destination_id编号作为数组和重复的客户编号。 我也尝试过阵列走,但结果是一样的。

$data = [];
        foreach ($sap_data['custid'] as $custid) {
          foreach ($sap_data['destid'] as $destid) {
            $data[] = [
              'user_id' => 1,
              'sap_customer_id' => $custid,
              'sap_destination_id' => $destid
            ];
          }
        }

感谢帮忙!

你应该以其他方式进行内循环

foreach($sap_data['custid'] as $k => $custid) {
   // Make destination key
   $dkey = str_replace('customer_number', 'destination_numbers', $k);
   // And get array, for example, destination_numbers_1 for customer_number_1
   foreach ($sap_data['destid'][$dkey] as $destid) {
     $data[] = [
              'user_id' => 1,
              'sap_customer_id' => $custid,
              'sap_destination_id' => $destid
            ];
   }
}

在eval.in上演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM