1

So I have array like this one

[
    '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'
        ]
    ]
]

Goal is to merge cust id with destid in pairs and should look like so

[
    (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',
    ],
...

I have tried with code below, but I am getting destination_id number as array and duplicated customer numbers. Also I have tried with array walk but result is same.

$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
            ];
          }
        }

Thx for helping!

2
  • Should 'user_id' always be 1 in the result? Just double checking.. Commented May 10, 2017 at 9:11
  • No, but it will be fix number sent from Form view Commented May 10, 2017 at 9:18

1 Answer 1

3

You should make inner loop in other way

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
            ];
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.