user3436467
user3436467

Reputation: 1775

merge multiple arrays from foreach loop into one array

Here is the code I am using to run through the loop and return the values

foreach ($matches[0] as $certs) {

$x509 = new File_X509();
$chain = $x509->loadX509($certs);
$ICN = $x509->getIssuerDNProp('CN');

print_r($ICN);
}

I am using an external library to parse $certs and finally end up with $ICN which has the final value i am interested in. However, each value is in its own array, I would like to merge them together into one array.

Here is the current output for $print_r($ICN):

Array
(
    [0] => thawte DV SSL CA - G2
)
Array
(
    [0] => thawte Primary Root CA
)
Array
(
    [0] => Thawte Premium Server CA
)

Here is the desired output:

Array
(
    [0] => thawte DV SSL CA - G2
    [1] => thawte Primary Root CA
    [2] => Thawte Premium Server CA
)

-- ATTEMPT --

Using the following code offered by @zeflex

$finalArray = array();

foreach ($matches[0] as $certs) {

    $x509 = new File_X509();
    $chain = $x509->loadX509($certs);
    $ICN = $x509->getIssuerDNProp('CN');
    $finalArray[] = $ICN[0];

}

print_r($finalArray);

Resulted in this output - as you can see, only the last value is in the array - the first two not included for some reason??

Array
(
    [0] => Thawte Premium Server CA
)

Upvotes: 0

Views: 1804

Answers (1)

zeflex
zeflex

Reputation: 1527

$finalArray = array(); 
foreach($cont["options"]["ssl"]["peer_certificate_chain"] as $chain) { 
openssl_x509_export($chain, $pemchain); 
//echo $pem_chain; 
$str = $pemchain; // The certificates string 
preg_match_all('/-----BEGIN CERTIFICATE-----[^-]*-----END CERTIFICATE-----/s', $str, $pems); 
//print_r($matches[0]); // $matches[1] is an array that contains every certificate string 

foreach ($pems[0] as $key => $certs) { 
$x509 = new File_X509(); 
$chain = $x509->loadX509($certs); 
$ICN = $x509->getIssuerDNProp('CN'); 


foreach($ICN AS $k => $v) { 
$finalArray[] = $v; 
} 

} 
} 
print_r($finalArray);

Upvotes: 2

Related Questions