1

I have an array created from loop which is returning the correct data

$ku = array();
    $oid = array('id-ce-keyUsage');
    if(isset($chain['tbsCertificate']['extensions'])) {
        $count = count($chain['tbsCertificate']['extensions']);
        for($i = 0; $i < $count; $i++) {
            $count2 = count($chain['tbsCertificate']['extensions'][$i]['extnValue']);
            for($j = 0; $j < $count2; $j++) {
                if(array_key_exists('extnId', $chain['tbsCertificate']['extensions'][$i]) &&
                in_array($chain['tbsCertificate']['extensions'][$i]['extnId'], $oid)) {
                $value = $chain['tbsCertificate']['extensions'][$i]['extnValue'][$j];
                $ku[] = $value;
                }
            }
        }
    }
print_r($ku);

The above code produces this, which is correct.

Array
(
    [0] => keyEncipherment
    [1] => digitalSignature
)
Array
(
    [0] => cRLSign
    [1] => keyCertSign
)
Array
(
    [0] => cRLSign
    [1] => keyCertSign
)

However, I would like to be able to print the values of $ku on their own, like so:

keyEncipherment, digitalSignature
cRLSign, keyCertSign
cRLSign, keyCertSign

I tried the following code but the results, and although the results look accurate, its actually adding the results of each iteration together rather then keeping them separate.

Here is the code im trying:

foreach ($ku as $val) {
    $temp[] = "$val";
    $key_usage = implode(', ', $temp);
    }
    echo $key_usage;

and here is the result:

keyEncipherment, digitalSignature
keyEncipherment, digitalSignature, cRLSign, keyCertSign
keyEncipherment, digitalSignature, cRLSign, keyCertSign, cRLSign, keyCertSign

Would appreciate some assistance. Happy to share more code if needed.

-UPDATE-

This code seems to help but hoping to find a better solution where i can just echo a string without []

$len=count($ku);
    for ($i=0;$i<$len;$i++)
    echo $ku[$i].', ';  
0

2 Answers 2

1

You don't need to store value in to another array, as your value is array itself.

After the discussion in chat:

 $new_ku = implode(',',$ku); 
 echo $new_ku;   
Sign up to request clarification or add additional context in comments.

9 Comments

i get this error Warning: implode(): Invalid arguments passed in C:\xampp\htdocs\labs\getcertdetails2\get_cert16.php on line 737 this line is $key_usage[] = implode(',', $val);
Also, i need a solution that uses echo instead of print_r as i will be using it as part of html code
you can echo in loop , if it is your requirement.
thx but still getting the above mentioned error.. i wonder if there is an issue because i have foreach within foreach.. can you see the full code here? pastebin.ca/3010376 - the lines in question are 154 to 156
with your latest solution, i am getting this warning: Warning: implode(): Invalid arguments passed in C:\xampp\htdocs\labs\getcertdetails2\get_cert16.php on line 737
|
0

try this code,

array_walk($ku, create_function('$k, $v', 'echo $v[0].", ".$v[1];'));

If you have PHP version >= 5.3 then you can use..

array_walk($ku, function($i, $v){ echo $v[0].", ".$v[1]; });

3 Comments

create_function() should really not be used anymore.
@Ja͢ck it's has security issue but supported in older PHP versions, OP can use modern callback function...
And by old you probably mean ancient! 5.3 was released in 2009.

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.