print all values in array from loop in a loop - php

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].', ';

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;

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]; });

Related

How to split evenly and oddly a string to form an array of even and odd results OK Like

I have a php string formed by images and corresponding prices like OK Like
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
I know that if I do:
$myArray = explode(',', $myString);
print_r($myArray);
I will get :
Array
(
[0] => ddb94-b_mgr3043.jpg
[1] => 3800
[2] => 83acc-b_mgr3059.jpg
[3] => 4100
)
But How could I split the string so I can have an associative array of the form?
Array
(
"ddb94-b_mgr3043.jpg" => "3800"
"83acc-b_mgr3059.jpg" => "4100"
)
Easier way to do like below:-
<?php
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
$chunks = array_chunk(explode(',', $myString), 2); //chunk array into 2-2 combination
$final_array = array();
foreach($chunks as $chunk){ //iterate over array
$final_array[trim($chunk[0])] = trim($chunk[1]);//make key value pair
}
print_r($final_array); //print final array
Output:-https://eval.in/859757
Here is another approach to achieve this,
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100,test.jpg,12321";
$arr = explode(",",$myString);
$temp = [];
array_walk($arr, function($item,$i) use (&$temp,$arr){
if($i % 2 != 0) // checking for odd values
$temp[$arr[$i-1]] = $item; // key will be even values
});
print_r($temp);
array_walk - Apply a user supplied function to every member of an array
Here is your working demo.
Try this Code... If you will receive all the key and value is equal it will work...
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
$myArray = explode(',', $myString);
$how_many = count($myArray)/2;
for($i = 0; $i <= $how_many; $i = $i + 2){
$key = $myArray[$i];
$value = $myArray[$i+1];
// store it here
$arra[$key] = $value;
}
print_r($arra);

how to add letters of the name as array elements in php?

i am receiving the name from the $request in php.I want to do something like to add all the letters of the name in the array during the request e.g
$name=$_request['name'];
say $name='test';
i want to save it in an array in this format as array("t","e","s","t").
how can i do it ?
str_split is your friend.
$split_string = str_split($name);
It may be sufficient for you to access the string directly as an array, without the need to format the data:
$a = 'abcde';
echo $a[2];
Will output
c
However you won't be able to perform some array operations, such as foreach
see the php site
so it would be like
$name= 'test';
$arr1 = str_split($name);
would result in a array like:
Array
(
[0] => t
[1] => e
[2] => s
[3] => t
)
Here you go
$i = 0;
while(isset($name[$i])) {
$nameArray[$i] = $name[$i];
$i++;
}
Try this:
$letters = array();
for (int $i=0; $i < strlen($name); $i++){
$letters[] = $name[$i];
}
and you can access it with:
for (int $i=0; $i < strlen($letters); $i++){
$letters[$i];
}

foreach integers into array

since im newbie i have a question
well i have $nUserID where store user's id's and is like
int(1)
int(2)
int(1)
and in $nAuctionID i have items id and they are like
int(150022)
int(150022)
int(150031)
i need put it in 1 array and make it like
array (
[1] => 150022
[2] => 120022,150031
)
which user which item id watch
how to do that ?
i though should use foreach , but i cant imagine how will looks
start with
$u[] = $nUserID;
$i[] = $nAuctionID;`
Grouping them by user ID, the following should result in what you're looking for.
$usersWatching = array();
// The following results in:
// array(1 => array(150022, 150023), 2 => array(150022))
// Which should be way more useful.
foreach ($nUserID as $i => $userID)
{
if (!isset($usersWatching[$userID]))
$usersWatching[$userID] = array();
$usersWatching[$userID][] = $nAuctionID[$i];
}
// To get the comma-separated version, do this:
$usersWatching = array_map(function ($value) {
return implode(',', $value);
}, $usersWatching);
this will work :
$arr = array();
foreach( $nUserID as $key=>$value)
{
$arr[$value] = $nAuctionID[$key] ;
}
print_r($arr);
Most confusingly worded question EVA!
$outputArray = array();
for ($i=0; $i< count($nUserID); $i++) {
$outputArray[$nUserID[$i]] = $nAuctionID[$i];
}
echo "<pre>";
print_r($outputArray);
That is what I got from your question..

Looping through a multidimensional associative array

I've been making an IRC bot in PHP. I've given different users specific access levels between 0 and 5. 0 being a guest and 5 being an admin.
I've been trying to write a command that when a user accesses it, it will send them a list of commands and syntax that they're allowed to use.
So far I have something like this
$array = array
(
"5" => $commands = array
(
"test" => $test2 = array
(
"trigger" => "!test",
"descrip" => "Just testing."
)
"test2" => $test3 = array
(
"trigger" => "!lol",
"descrip" => "another test."
)
)
);
I have no idea how to loop through it so that if ($accessLevel == 5) then show commands for $array[5(and below)]
At the end I want it to send out $array[5][command][trigger] : $array[5][command][descrip]
I don't necessarily need you to code it for me, just a push in the right direction would be helpful.
This should do it... (check the privilege level)
foreach($array as $level => $priv){
// check for privilege level
if($level >= $accessLevel){
// loop through privilege array
foreach($priv as $command => $list){
foreach($list as $trigger => $description)
}
}
}
}
On a side note, instead of using string keys for level you could use array indicies, and that would allow the combined outer foreach/if combination to be written as
for($i = $accessLevel; $i >= 0; $i--){
$priv = $array[$i];
//...
}
for ($i = 5; $i >= 0; --$i) {
//list commands for accesslevel $i
}
Something like this? (Prolly want to add newlines or delimiters)
foreach ($array[5] as $key=>$value) {
echo $key;
echo $value['trigger'];
echo $value['descrip'];
}

Array not populating correctly

I am using the following code to populate an array:
$number = count ($quantitys);
$count = "0";
while ($count < $number) {
$ref[$count] = postcodeUnknown ($prefix, $postcodes[$count]);
$count = $count +1;
}
postcodeUnknown returns the first row from a mysql query, using a table prefix and an element from an array named postcodes. $postcodes contains strings that should return a different row each time though the loop.
Which I'd expect to create an array similar to:
Array ([0] =>
Array ([0] => some data [1] => more data)
[1] =>
Array ([0] => second row [1] => even more...)
)
But it's not. Instead it's creating a strange array containing the first results over and over until the condition is met, eg:
simplified result of print_r($ref);
Array (
[0] => Array ([0] => some data [1] => more data)
)
Array(
[0] => Array (
[0] => the first arrays content...
[1] => ...repeated over again until the loop ends
)
)
And I'm at a loss to understand why. Anyone know better than me.
Try
$number = count ($quantitys);
$count = "0";
while ($count < $number) {
$ref1[$count] = postcodeUnknown ($prefix, $postcodes[$count]);
$ref2[] = postcodeUnknown ($prefix, $postcodes[$count]);
$ref3[$count][] = postcodeUnknown ($prefix, $postcodes[$count]);
$count = $count +1;
}
echo "<pre>";
print_r($ref1);
print_r($ref2);
print_r($ref3);
echo "</pre>";
Try that to see if any of those get an output you are looking for.
Uh, add a print to postcodeUnknown and check if its actually passing different postcodes or the same thing all the time?
ie
function postcodeUnkown($a,$b){
echo var_dump($a).var_dump($b);
//the rest
}
Also, why two different variables? ($quantitys and $postcodes).
Also, you might want to replace the while loop with for:
for($i=0;$i<$number;$i++){
$ref[$i] = postcodeUnknown ($prefix, $postcodes[$i]);
}
your variable count should not be a string, try this
$number = count ($quantitys);
$count = 0;
while ($count < $number) {
$ref[$count] = postcodeUnknown ($prefix, $postcodes[$count]);
$count = $count +1;
}

Categories