How to split one group into seperate using php [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want split one group AS follow
splitGroups("11133355557777") ➞ ["111", "333", "5555", "7777"]
Anyone have idea then let me know

With php version 5 above this will work link to execute
Link to an example https://paiza.io/projects/qfRZ07OP3OviWCVsUbdFtQ
function splitGroups($str){
$arr = [];
$i=0;
$sub = '';
while($i!=strlen($str))
{
$sub .= $str[$i];
if ( strlen($str)-1 == $i || $str[$i] != $str[$i+1] ){
$arr[] = $sub;
$sub='';
}
$i++;
}
return $arr;
}

Related

How to get the position of same elements in php? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need your help to get the position of duplicate or same elements in an array.
For example
$arr =[6,5,3,7,40,45,7,6,3,32,86,40,5,3,7,40];
Result
6 = 0,7
5 = 1,11
3 = 2,8
7 = 3,6,14
40 = 4,11,15
45 =5 and so on.
One simple approach:
<?php
$arr =[6,5,3,7,40,45,7,6,3,32,86,40,5,3,7,40];
$pos = array();
foreach($arr as $k => $v) {
$pos[$v][] = $k;
}
foreach($pos as $k => $v) {
echo $k."=".implode(',', $v)."<br>";
}
?>
Result:
6=0,7
5=1,12
3=2,8,13
7=3,6,14
40=4,11,15
45=5
32=9
86=10

How to make image for each x numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How to replace 200 numbers with an image?
This example :
For example
When user have 200 numbers He takes 1 image
When user have 830 He take 4 image
what php code I need it?
Sorry but I havn't Any code
Thanks in advance
$votes = 10334;
$starCount = intval($votes/ 200);
$starCount = $starCount > 5 ? 5 : $starCount; //if maximum 5 stars
$a = 1;
$starsString = '';
for ($a; $a <= $starCount; $a++) {
$starsString .= '⛤'; // or '<img src="https://i.stack.imgur.com/EhAy4.gif" alt="here">'
}
echo $starsString;

Given three numbers, two are guaranteed equal, find the different number. [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
For example: A = 2, B = 4 & C = 2 then output should be uniqueNumber(A, B, C) = 4
if ($A==$B)
{
return $C;
}
if ($A==$C)
{
return $B;
}
return $A;
This should be a simple logic answer and you can either go with direct comparisons but that might be too hectic in case of more than 3 arguments.
You can try this out as well:
$num_arr = [$a,$b,$c];
for($i=0;$i<count($num_arr)-1; $i++)
{
if(!in_array($num_arr[$i],array_merge(array_slice($num_arr,0,$i),array_slice($num_arr,$i+1))))
return $num_arr[$i];
}
Or simply like this:
$num_arr = [$a,$b,$c];
for($i=0;$i<count($num_arr)-1; $i++)
{
$temp = $num_arr;
unset($temp[$i]);
if(!in_array($num_arr[$i],$temp))
return $num_arr[$i];
}
Tried implementing a more generic approach.
Hope it helps.

PHP check if value is one of other numbers in loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
If i have a loop for example
for ($a=1; $a<6; $a++)
Is it possible to say for the first loop if another variable like $b is the same as one of the other numbers that will come like 2, 3, 4 or 5.
And what if i have a word split, so every letter is a variable like this:
$letter1 = str_split($word)[0];
$letter2 = str_split($word)[1];
$letter3 = str_split($word)[2];
$letter4 = str_split($word)[3];
$letter5 = str_split($word)[4];
How can i check if $a is at 1 in the loop,if it is not letter1 but letter2, 3, 4 or 5?
sure. Just check it against the counter:
for ($a=1; $a<6; $a++){
if ($a===$b){
echo ("b is caught!");
break;
}
}

How To Display Random Results in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Im trying to display 2 or more unique random results from a text file, How do I do that please?
But I need unique results and not 2 or more of the same.
Currently using this code, but it only returns 1 random result:
<?php
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$randArrayIndexNum = array_rand($textArray);
$randPhrase = $textArray[$randArrayIndexNum];
?>
<?php echo $randPhrase ?>
I would use something like that
shuffle($textArray);
echo $textArray[0];
echo $textArray[1];
http://php.net/manual/tr/function.shuffle.php
You can give a shot to this also. Code is not tested. I am collecting the used into an array, and check, is that used before.
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$used = array();
$countOfRandoms = 2;
$randoms = array();
$i = 1;
do {
if ($countOfRandoms == $i) {
break;
}
$randArrayIndexNum = array_rand($textArray);
if (in_array($randArrayIndexNum, $used)) {
continue;
}
$used[] = $randArrayIndexNum;
$random = $textArray[$randArrayIndexNum];
$i++;
} while (true);

Categories