paultje182
paultje182

Reputation: 159

Array merge between three arrays

I have the following array output:

[38] => x._domainkey    14400   IN  TXT ( "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApYA+ELn4SxsERDFgUB9pUnB4PjbcUlSJoLHAlCq1MLVA3NxgLKSAz+lSczyMH97Y/xOpJf1X1LSoGgNh5GVRn0t"
[39] =>                     "pm1QOPfKbNHx8VMNn4L/KZSPGFyySDUK/UPnF1QoMckoU5a/LgtI8x1ttry5oySDTY9KprVAf92uK6pOFQJA3QlzV2P+R4tx/zho5KlDHmc8YKjL7rLjgQVX7GUBi9cHr0+BQdVFgFL8SwBY8fn/CQ"
[40] =>                     "42JMsEWcMjjEkbAwi9yscGQoTih+kctjr4W8LDF28vyJixozbaFpG8pJ+jGDAho+Zjt/XReOZQP0h51Y8md3/x/F0+gST4mn0mCskNtIQIDAQAB" )

How can I get this in de following construction?

[38] => x._domainkey    14400   IN  TXT ( "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApYA+ELn4SxsERDFgUB9pUnB4PjbcUlSJoLHAlCq1MLVA3NxgLKSAz+lSczyMH97Y/xOpJf1X1LSoGgNh5GVRn0tpm1QOPfKbNHx8VMNn4L/KZSPGFyySDUK/UPnF1QoMckoU5a/LgtI8x1ttry5oySDTY9KprVAf92uK6pOFQJA3QlzV2P+R4tx/zho5KlDHmc8YKjL7rLjgQVX7GUBi9cHr0+BQdVFgFL8SwBY8fn/CQ42JMsEWcMjjEkbAwi9yscGQoTih+kctjr4W8LDF28vyJixozbaFpG8pJ+jGDAho+Zjt/XReOZQP0h51Y8md3/x/F0+gST4mn0mCskNtIQIDAQAB" )

On one rule.

I tried hard and get the output in multiple array's, but not on one rule:

Array ( 
  [0] => x._domainkey 
  [1] => 14400 
  [3] => TXT 
  [4] => ( "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApYA+ELn4SxsERDFgUB9pUnB4PjbcUlSJoLHAlCq1MLVA3NxgLKSAz+lSczyMH97Y/xOpJf1X1LSoGgNh5GVRn0t" 
) 

Array ( 
  [0] => 
  [1] => 
  [2] => 
  [3] => 
  [4] => 
  [5] => "pm1QOPfKbNHx8VMNn4L/KZSPGFyySDUK/UPnF1QoMckoU5a/LgtI8x1ttry5oySDTY9KprVAf92uK6pOFQJA3QlzV2P+R4tx/zho5KlDHmc8YKjL7rLjgQVX7GUBi9cHr0+BQdVFgFL8SwBY8fn/CQ" 
) 

Array ( 
  [0] => 
  [1] => 
  [2] => 
  [3] => 
  [4] => 
  [5] => "42JMsEWcMjjEkbAwi9yscGQoTih+kctjr4W8LDF28vyJixozbaFpG8pJ+jGDAho+Zjt/XReOZQP0h51Y8md3/x/F0+gST4mn0mCskNtIQIDAQAB" ) 
)

I want both array [5] on one rule after [4], is this possible?

Upvotes: 1

Views: 54

Answers (2)

ishegg
ishegg

Reputation: 9937

You need to concatenate the elements after doing some cleanup (removing quotes, parentheses, etc). In this specific case, I removed the last character from the first element (to remove the trailing quote, can be trim() or trim() too), and used trim() to remove leading and trailing quotes on the next two elements.

If the position of these three elements is dynamic, search first by a value that might be unique to your use case (in my example I use domainkey) and use that key as the base for the concatenation:

<?php
$records = [
2 => "gibberish",
5 => "garbage",
38 => 'x._domainkey    14400   IN  TXT ( "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApYA+ELn4SxsERDFgUB9pUnB4PjbcUlSJoLHAlCq1MLVA3NxgLKSAz+lSczyMH97Y/xOpJf1X1LSoGgNh5GVRn0t"',
39 =>                     '"pm1QOPfKbNHx8VMNn4L/KZSPGFyySDUK/UPnF1QoMckoU5a/LgtI8x1ttry5oySDTY9KprVAf92uK6pOFQJA3QlzV2P+R4tx/zho5KlDHmc8YKjL7rLjgQVX7GUBi9cHr0+BQdVFgFL8SwBY8fn/CQ"',
40 =>                     '"42JMsEWcMjjEkbAwi9yscGQoTih+kctjr4W8LDF28vyJixozbaFpG8pJ+jGDAho+Zjt/XReOZQP0h51Y8md3/x/F0+gST4mn0mCskNtIQIDAQAB"'
];
foreach ($records as $i => $record) {
    if (stripos($record, "domainkey") !== false) {
        $initialIndex = $i;
        break;
    }
}
echo substr($records[$initialIndex], 0, -1).trim($records[$initialIndex + 1], '"').trim($records[$initialIndex + 2], '"')."\")";

Demo

Upvotes: 2

Barmar
Barmar

Reputation: 781088

This isn't array merging, it's string concatenation.

$array[38] .= $array[39] . $array[40]; // combine the 3 lines into one
array_splice($array, 39, 2); // remove the continuation lines

You need to do this whenever a line contains ( before the value. You have to get all the lines until the matching ) and concatenate them.

Upvotes: 1

Related Questions