将foreach循环中的多个数组合并到一个数组中

时间:2015-05-19 01:36:45

标签: php

这是我用来运行循环并返回值

的代码
foreach ($matches[0] as $certs) {

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

print_r($ICN);
}

我使用外部库来解析$certs,最后得到$ICN,其中包含我感兴趣的最终值。但是,每个值都在自己的数组中,我想将它们合并为一个数组。

以下是$print_r($ICN)的当前输出:

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

这是所需的输出:

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

- ATTEMPT -

使用@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);

导致此输出结果 - 正如您所看到的,只有最后一个值在数组中 - 前两个不包含在某个原因中?

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

1 个答案:

答案 0 :(得分:2)

$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);