0

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
)
1
  • If getIssuerDNProp() returns one or more elements, you can spread those while pushing so that you get a flat result array. array_push($finalArray, ...$x509->getIssuerDNProp('CN')); Commented Mar 20 at 1:36

1 Answer 1

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

9 Comments

sandbox.onlinephpfunctions.com/code/… check this code, it's similar than yours excepts if I missed something.
yes, that code works but its not the same as what i need. I am using external library to parse data which then create the arrays $ICN, I need to merge each instance of $ICN in the interation
just thinking out loud, is another way to do this by saving the data $ICN for each iteration of the loop then merge them?
i updated my post to show you the results of your original solution but as you can see, only one value ended up in the array and not all three of them ?
I've update my code. If it's still not working, do a var_dump($matches[0]) before the foreach loop and post the result to investigate.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.