how to add letters of the name as array elements in php? - 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];
}

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 count exploded array

My string looks like:
244.53.66=1,53.646.77=1,666.534.23=5,664.521.64=3,535.777.54=2,533.886=5,22.153=5,643.786=2
I exploded it like:
$string = "244.53.66=1,53.646.77=1,666.534.23=5,664.521.64=3,535.777.54=2,533.886=5,22.153=5,643.786=2";
$array = explode(",", $string);
$array2=array();
for($i = 0; $i<count($array); $i++)
{
array_push($array2, explode("=",$array[$i]));
}
now $array2 got these values:
$array2[0][0] = "244.53.66";
$array2[0][1] = "1";
$array2[1][0] = "53.646.77";
$array2[1][1] = "1";
etc.
How Can I count all values each type, example:
2 x 1 (two records with value 1), 1x1(1 record with value 1) itd.
I want to store this as array:
$array3[index] = "amount same values";
Can You help me?
You need first to get the IDs in a seperate array, then use array_count_valuesto count the number of the IDs.
Here's the final code:
<?php
$string = "244.53.66=1,53.646.77=1,666.534.23=5,664.521.64=3,535.777.54=2,533.886=5,22.153=5,643.786=2";
$array = explode(",", $string);
$array2=array();
for($i = 0; $i<count($array); $i++)
{
array_push($array2, explode("=",$array[$i]));
}
$array3 = array();
//Iterate through array 2 to extract all IDs
for($i = 0; $i<count($array2); $i++)
{
array_push($array3, $array2[$i][1]);
}
$vals = array_count_values($array3);
print_r($vals);
?>
Which outputs
Array
(
[1] => 2
[5] => 3
[3] => 1
[2] => 2
)
Happy coding on StackOverflow !

PHP - Checking up on string with regex and push to array

I have two variables as follows:
$string1 = '/test/10/25';
$string2 = '/test/[0-9]+/[0-9]+
Is it possible to make PHP compare these two strings and push the actual ID's (10 and 25) into an array like so by using the regex as some sort of guidance?
Array
(
[0] => 10
[1] => 25
)
I tried playing around with preg_match() but this just puts everything into the same array key.
This will work for you
<?php
$string1 = '/test/10/25';
$string2 = '/\/test\/([0-9]+)\/([0-9]+)/';
$matches = [];
preg_match($string2, $string1, $matches);
$array = [];
for($i = 1; $i < count($matches); $i ++)
{
array_push($array, $matches[$i]);
}
print_r($array);
$array will have the matches inside. I had to change the regex string because it was not "valid" for php.

print all values in array from loop in a loop

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

Count delimiter(s) of a string [duplicate]

What is the most efficient way to count all the occurrences of a specific character in a PHP string?
use this:
echo substr_count("abca", "a"); // will echo 2
Can you not feed the character to preg_match_all?
Not sure what kind of a response you're looking for, but here's a function that might do it:
function findChar($c, $str) {
indexes = array();
for($i=0; $i<strlen($str); $i++) {
if ($str{$i}==$c) $indexes[] = $i;
}
return $indexes;
}
Pass it the character you're looking for and the string you want to look:
$mystring = "She shells out C# code on the sea shore";
$mychar = "s";
$myindexes = $findChar($mychar, $mystring);
print_r($myindexes);
It should give you something like
Array (
[0] => 0
[1] => 4
[2] => 9
[3] => 31
[4] => 35
)
or something...
If you are going to be repeatedly checking the same string, it'd be smart to have some sort of trie or even assoc array for it otherwise, the straightforward way to do it is...
for($i = 0; $i < strlen($s); $i++)
if($s[i] == $c)
echo "{$s[i]} at position $i";

Categories