Here is my code:
$answer = explode(" ", $row2['answer']);
foreach($tags as $i =>$key) {
$i >0;
echo $i.' '.$key .'</br>';
}
the output is
0 a
1 b
2 c
3 d
4 e
5 f
I'd like that the output be random, but doesn't repeat twice.
For example:
e 4
a 0
c 2
f 5
d 3
b 1
Any idea please ?
Thank you.
The simplest way I can think of to achieve this would be to use the shuffle method on an array collection. This however does not guarantee non-sequentialness:
$range = range(1,5);
shuffle($range);
foreach($range as $int){
echo $int;
}
Related
I need to make a combination of all course input by the user such as the user add 5 courses it will be as this template:
Course 1
Course 2
Course 3
Course 4
Course 5
After that, I need to create a combination for all course with array of grade:
array('A+', 'A', 'B+', 'B', 'C+', 'C','D+','D','F')
Actually, I found what I need in this topic: Create every possible combination in PHP but there's something I don't understand how I can do.
Until now this code after edit:
$dict = [
'1' => ['A+', 'A', 'B+', 'B', 'C+', 'C','D+','D','F'],
'2' => ['Course 1', 'Course 2', 'Course 3', 'Course 4', 'Course 5'],
];
$str = '2 : 1';
class SentenceTemplate implements IteratorAggregate
{
private $template;
private $thesaurus;
public function __construct($str, $dict)
{
$this->thesaurus = [];
$this->template = preg_replace_callback('/\w+/', function($matches) use ($dict) {
$word = $matches[0];
if (isset($dict[$word])) {
$this->thesaurus[] = $dict[$word];
return '%s';
} else {
return $word;
}
}, $str);
}
public function getIterator()
{
return new ArrayIterator(array_map(function($args) {
return vsprintf($this->template, $args);
}, $this->combinations($this->thesaurus)));
}
private function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return array();
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}
// get combinations from subsequent arrays
$tmp = $this->combinations($arrays, $i + 1);
$result = array();
// concat each array from tmp with each element from $arrays[$i]
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ? array_merge(array($v), $t) : array($v, $t);
}
}
return $result;
}
}
$sentences = new SentenceTemplate(1, $dict);
$i = 1;
foreach ($sentences as $sentence) {
echo "COMBINATION $i : \n";
$sentences2 = new SentenceTemplate(2, $dict);
foreach ($sentences2 as $sentence2) {
echo "$sentence2 : "."$sentence\n";
}
echo "----------\n";
$i++;
}
Output
COMBINATION 1 :
Course 1 : A+
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
----------
COMBINATION 2 :
Course 1 : A
Course 2 : A
Course 3 : A
Course 4 : A
Course 5 : A
----------
COMBINATION 3 :
Course 1 : B+
Course 2 : B+
Course 3 : B+
Course 4 : B+
Course 5 : B+
----------
COMBINATION 4 :
Course 1 : B
Course 2 : B
Course 3 : B
Course 4 : B
Course 5 : B
----------
COMBINATION 5 :
Course 1 : C+
Course 2 : C+
Course 3 : C+
Course 4 : C+
Course 5 : C+
----------
COMBINATION 6 :
Course 1 : C
Course 2 : C
Course 3 : C
Course 4 : C
Course 5 : C
----------
COMBINATION 7 :
Course 1 : D+
Course 2 : D+
Course 3 : D+
Course 4 : D+
Course 5 : D+
----------
COMBINATION 8 :
Course 1 : D
Course 2 : D
Course 3 : D
Course 4 : D
Course 5 : D
----------
COMBINATION 9 :
Course 1 : F
Course 2 : F
Course 3 : F
Course 4 : F
Course 5 : F
----------
And now how I can build more combinations such as:
COMBINATION 10 :
Course 1 : B+
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
COMBINATION 11 :
Course 1 : B
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
COMBINATION 12 :
Course 1 : C+
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
COMBINATION 13 :
Course 1 : C
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
COMBINATION 14 :
Course 1 : D+
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
COMBINATION 15 :
Course 1 : D
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
COMBINATION 16 :
Course 1 : F
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
.......
The procedure is for every course has a different grade with other courses and other courses has the same procedure, anyone has an idea to build this?
This code may help. It is a PHP program to print all possible strings of length k.
<?php
// PHP program to print all possible strings of length k
// Driver method to test below methods
function main() {
$set = ['A+', 'A', 'B+', 'B', 'C+', 'C','D+','D','F'];
$k = 5;
$n = sizeof($set);
printAllKLengthRec($set, "", $n, $k);
}
// The main recursive method to print all possible strings of length k
function printAllKLengthRec($set, $prefix, $n, $k) {
// Base case: k is 0, print prefix
if ($k == 0) {
echo $prefix."\n";
return;
}
// One by one add all characters from set and recursively
// call for k equals to k-1
for ($i = 0; $i < $n; ++$i) {
// Next character of input added
$newPrefix = $prefix . $set[$i];
// k is decreased, because we have added a new character
printAllKLengthRec($set, $newPrefix, $n, $k - 1);
}
}
main();
?>
You just need to modify the printing part.
The code bellow may fulfill your purpose.
<?php
// PHP program to print all possible strings of length k
// Driver method to test below methods
function main() {
$set = ['A+', 'A', 'B+', 'B', 'C+', 'C','D+','D','F'];
$k = 5;
$n = sizeof($set);
printAllKLengthRec($set, "", $n, $k);
}
// The main recursive method to print all possible strings of length k
function printAllKLengthRec($set, $prefix, $n, $k) {
// Base case: k is 0, print prefix
if ($k == 0) {
$c = 1;
for($p=0; $p<strlen($prefix); $p++) {
echo "Course $c: ". $prefix[$p];
if(($p+1) < strlen($prefix) && ($prefix[$p+1] == '+' || $prefix[$p+1] == '-')) {
echo $prefix[$p+1];
$p++;
}
echo "\n";
$c++;
}
return;
}
// One by one add all characters from set and recursively
// call for k equals to k-1
for ($i = 0; $i < $n; ++$i) {
// Next character of input added
$newPrefix = $prefix . $set[$i];
// k is decreased, because we have added a new character
printAllKLengthRec($set, $newPrefix, $n, $k - 1);
}
}
main();
?>
I have a code in which foreach inside another foreach.
$order_id = '1,1,8';
$order_no_first= 'F,SH,C';
$order_id1 = explode(",", $order_id);
$order_no_first1 = explode(',', $order_no_first);
foreach($order_id1 as $ord_id){
foreach($order_no_first1 as $ord_no_first){
if($ord_id != '') {
$this->receipt->chageBagStatus($ord_id, $ord_no_first);
$add = $this->receipt->addJobOrderNew($ord_id, $ord_no_first, $bag_no);
}
}
}
Now the above code iterates 3 times resulting 9 rows in mysql.
//Current Output
order_id orderr_no_first
-------- ---------------
8 C
8 SH
8 F
1 C
1 SH
1 F
1 C
1 SH
1 F
The above output is wrong. I want the output as below,
//Required Output
order_id orderr_no_first
-------- ---------------
8 C
1 SH
1 F
I know it's because am using nested foreach. but I don't know how to solve this issue. Is there any solution. Thankyou.
just use one foreach like this,
foreach($order_id1 as $key => $ord_id){
if($ord_id != '') {
$this->receipt->chageBagStatus($ord_id, $order_no_first1[$key]);
$add = $this->receipt->addJobOrderNew($ord_id, $order_no_first1[$key], $bag_no);
}
}
I hope this will work for you
$order_id = '1,1,8';
$order_no_first= 'F,SH,C';
$order_id1 = explode(",", $order_id);
$order_no_first1 = explode(',', $order_no_first);
rsort($order_id1);
asort($order_no_first1);
$i = 0;
foreach($order_id1 as $ord_id){
echo $ord_id." ".$order_no_first1[$i]."<br/>";
$i++;
}
`
I have a database which has 6 column A B C D E X, for each combination of ABCDE I have a different value of X.
I need a way to search through, that will allow all values of X for different combinations (for example all X when A=1, or all X when A=1 and B=2 etc)
My thought was to translate it into a 5-D array which looks like this:
Array[A][B][C][D][E]=X;
But now I'm trying to extract sub arrays, when I don't know how may of the dimensions will be constant. So I need to be able to extract all value of X for Array[1][5][][][] or Array[2][4][5][][]… etc.
And I'm totally stuck.
I'm trying to do 6 loops one inside another but I don't know how to handle those that are constant.
Help with ideas will be very very helpful.
Edit
Database:
A B C D E X
1 1 1 1 1 53
1 1 2 3 2 34
2 1 1 4 2 64
Turned it into an array:
Array[1][1][1][1][1]=53
Array[1][1][2][3][2]=34
For
Input: A=1
Output 53,34
Input A=1,B=1,C=1
Output: 53,
etc
Try this then
<?php
$arr = array();
$result = mysql_query("SELECT A,B,C,D,E,X FROM table_name ORDER BY A ASC,B ASC,C ASC,D ASC,E ASC");
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)) {
array_push($arr,$row);
}
}
function search($arr,$values) {
$return = array();
foreach($arr AS $key => $value) {
$ok = true;
foreach(array('A','B','C','D','E') AS $letter) {
if(array_key_exists($letter,$values)) {
if($value[$letter] != $values[$letter]) {
$ok = false;
break;
}
}
}
if($ok) array_push($return,$value['X']);
}
return (($return) ? implode(',',$return) : false);
}
echo '<pre>';
print_r(search($arr,array('A' => 1)));
echo '</pre>';
?>
I'm trying to display a random generated string on my page (php), but I have absolutely no idea how to do this.
I only want the following letters and digits to be used:
B C D F G H J K M P Q R T V W X Y Z 2 3 4 6 7 8 9
In the following format:
XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
Can anyone help me out, and give me a script which I can put on my page? Help would be really appreciated!
I tried this but it's not even displaying on my page for some odd reason.
$tokens = 'BCDFGHJKMPQRTVWXYZ2346789';
$serial = '';
for ($i = 0; $i < 5; $i++) {
for ($j = 0; $j < 5; $j++) {
$serial .= $tokens[rand(0, 35)];
}
if ($i < 3) {
$serial .= '-';
}
}
echo $serial;
You were almost there. Here's a few fixes to your code;
<?php
$tokens = 'BCDFGHJKMPQRTVWXYZ2346789';
$serial = '';
for ($i = 0; $i < 5; $i++) {
for ($j = 0; $j < 5; $j++) {
$serial .= $tokens[rand(0, strlen($tokens) - 1)];
}
if ($i < 4) {
$serial .= '-';
}
}
echo $serial;
?>
I can't say for sure why your page isn't showing, but in your original code you were missing <?php at the top of the page.
Edit: Here's a quick explanation of some of the changes I made to your code.
Your code had rand(0, 35). But since you may change the characters in $tokens in the future, it's better to simply calculate the length of the $tokens using strlen($tokens) - 1 (the -1 being important because strlen() starts counting at 1, whereas $tokens[INDEX] starts counting at 0).
Your code had if ($i < 3), but you actually want four dashes, so I changed it to if ($i < 4).
<?php
$charsPerGroup = 5;
$groups = 5;
$groupDelimiter = '-';
$tokens = explode(' ', 'B C D F G H J K M P Q R T V W X Y Z 2 3 4 6 7 8 9'); // from your question, format this however you want
$tokens = array_flip($tokens);
$resultArray = array();
for($i=0;$i<$groups;$i++) {
$resultArray[] = join(array_rand($tokens, $charsPerGroup));
}
echo join($groupDelimiter, $resultArray);
<?php
$enters = explode(' ', "B C D F G H J K M P Q R T V W X Y Z 2 3 4 6 7 8 9");
$entry = rand(0,count($enters)-1);
echo $enters[$entry];
$output = "";
for($i=1; $i++; $i<=25) {
$entry = rand(0,count($enters)-1);
$output .= $enters[$entry] . ($i % 5 == 0 && $i < 25 ? '-' : '' );
}
echo $output;
?>
ok, this is driving me insane, the solution must be easy but I've hit the proverbial wall,
here it is:
suppose I have this Array: a, b, c, d
I want to find all the contiguous letters in the array without mixing them, such as:
a b c d
a b c
a b
b c d
b c
c d
I've done many tests, but for some reason I just can't get it right. Any hint would be greatly appreciated.
$arr = Array('a', 'b', 'c', 'd');
for ($i = 0; $i < count($arr); $i++) {
$s = '';
for ($j = $i; $j < count($arr); $j++) {
$s = $s . $arr[$j];
if (strlen($s) > 1) {
echo $s.' ';
}
}
}
OUTPUT:
ab abc abcd bc bcd cd