I have array like this
Array ( [0] => F [1] => F [2] => N [3] => )
How to run the two loops with above array ?
for ($x = 0; $x <= 2; $x++) {
//In this loop i need the output like above
F-01
F-02
}
I have an loop im fetching some data in it like
for ($x = 0; $x <= 1; $x++) {
//In this loop i need the output like above
N-01
}
For that on you need to do something like below
<?php
$myarray = Array ( 'F' ,'F' ,'N' , 'l');
$j =0;
for($x = 0; $x < 2; $x++ ){
echo $myarray[$x].'-'.$j.$x;
echo "<br>";
}
echo "<br>";
$m=0;
for($k=2;$k<=3;$k++){
echo $myarray[$k].'-'.$j.$m;
echo "<br>";
}
?>
I hope this will help you.
Related
I have a probrem but I don't know how to fix it,
here is two array
first is$X = A,B,C,D
second likes this a[1]a[2]a[3]a[4]a[5]a[6]a[7]a[8]
I want to loop likes this
Aa[1]a[2]
Ba[3]a[4]
Ca[5]a[6]
Da[7]a[8]
now i write likes this
foreach($X as $key) {
for ($i = 0; $i < 8; $i++) {
$a[$i];
}
$g=$a[$key][$i];
}
But it isn't that thing I want.
how can I fix it? thank you for your help.
You can try for
<?php
$X = ['A','B','C','D'];
$Y = ['a[1]a[2]','a[3]a[4]','a[5]a[6]','a[7]a[8]'];
$result = [];
for ($i=0; $i < sizeof($X) ; $i++) {
$result[] = $X[$i].$Y[$i];
}
?>
Output
Array
(
[0] => Aa[1]a[2]
[1] => Ba[3]a[4]
[2] => Ca[5]a[6]
[3] => Da[7]a[8]
)
i wanna use Repeat loop (for , while and etc ... )in array of array of in php :
for($x = 0; $x < $arrlength; $x++) {
$poets = array(
'keyboard' => array(
array($saves[$x]) // this is just one array i don't want this !
),
);
$url= "https://api.telegram.org/bot".$token."/sendMessage?
chat_id=".$chat_id."&text=".$text."&reply_markup=".$jsonPoets;
file_get_contents($url);
}
this is not working , i wanna do like this :
for($x = 0; $x < $arrlength; $x++) {
$poets = array(
'keyboard' => array(
//i wanna do like this ...
array($saves[$x])
//array($saves[$x])
//array($saves[$x])
.
.
.
)
);
$url= "https://api.telegram.org/bot".$token."/sendMessage?
chat_id=".$chat_id."&text=".$text."&reply_markup=".$jsonPoets;
file_get_contents($url);
}
it means i wanna make multi array in array with loop repeat .
$poets= array('keyboard' => array());
for($x = 0; $x < $arrlength; $x++) {
$poets['keyboard'][] = array($saves[$x]);
}
Like this?
I'm trying to build a script that does two thing.
1) The Numbers should not repeat.
2) The numbers should be from an array called $id.
<?php
$a = array(); // final array which will have our id's to display
$id = array(1, 3, 5, 7, 9); //final array should contain a number only from this list
$itemstoshow = 3; // how many items to display
for($i = 0; $i < $itemstoshow; $i++) {
do {
$a[$i] = assignid(9);
$chkid = checkeer($a[$i], $i);
$chkdata = chkdata($a[$i], $i);
} while($chkdata == "nonexist" or $chkid == "repeatedid");
}
// display numbers in the array
for($i = 0; $i < $itemstoshow; $i++) {
echo "Item " . $a[$i] . "--------";
}
// check for redundancy function
function checkeer($x, $y)
{ //first parameter is query aray second is counter
global $a;
$err = 0;
// check if repeating number
for($i = 0; $i <= $y - 1; $i++) {
if($x == $a[$i]) {
$err = 1;
}
}
if($err == 1) {
return "repeatedid";
}
}
//check if array $a holds value from $id or not
function chkdata($x, $y)
{
global $a;
global $id;
for($i = 0; $i <= $y - 1; $i++) {
if($x !== $id[$i]) {
return "nonexist";
}
}
}
//assign id function
function assignid($x)
{
return rand(1, $x);
}
problem number 1 solved problem number 2 still not solved please help me.
the code should show 3 numbers from 1 to 9 which donot repeat and are in the array $id
You could use a combination of array_rand and array_map to get random values from the array that has the values your basing the randomization. Take a look:
$id = array(1,3,5,7,9); //final array should contain a number only from this list
$itemstoshow = 3;
$values = array_map(function($item) use($id){
return $id[$item];
}, array_rand($id, $itemstoshow));
print_r($values);
Output:
Array
(
[0] => 1
[1] => 3
[2] => 9
)
Running again:
Array
(
[0] => 3
[1] => 7
[2] => 9
)
You can use array_rand that selects random keys:
$id = array(1,3,5,7,9);
$result = array_intersect_key($id, array_flip(array_rand($id, 3)));
Or you can shuffle the array and take for example the 3 first items:
$id = array(1,3,5,7,9);
$temp = $id;
shuffle($temp);
for ($i = 0; $i < 3; $i++) {
$result[] = $temp[$i];
}
I am trying to create an array from 2 other arrays. I am having a problem getting them merged outside of their loops.
Here is the code to build the 2 arrays:
$redDogs = '3';
$blueDogs = '2';
$i = 1;
// build red dogs array
$redDogs = (int)$redDogs;
while($i <= $redDogs) {
$reds[] = 'red_dog_' . $i++;
}
foreach ($reds as $red) {
print '<pre>'; print_r($red); print '</pre>';
}
$j = 1;
// build blue dogs array
$blueDogs = (int)$blueDogs;
while($j <= $blueDogs) {
$blues[] = 'blue_dog_' . $j++;
}
foreach ($blues as $blue) {
print '<pre>'; print_r($blue); print '</pre>';
}
How could I create an array like:
print_r($alldogs);
To produce the following output:
Array {
[0] => red_dog_1
[1] => red_dog_2
[2] => red_dog_3
[3] => blue_dog_1
[4] => blue_dog_2
}
I have tried array_merge($red, $blue) but don't seem to get any values.
Any help is greatly appreciated.
$redDogs = '3';
$blueDogs = '2';
$i = 1;
// build red dogs array
$redDogs = (int)$redDogs;
while($i <= $redDogs) {
$reds[] = 'red_dog_' . $i++;
}
$j = 1;
// build blue dogs array
$blueDogs = (int)$blueDogs;
while($j <= $blueDogs) {
$blues[] = 'blue_dog_' . $j++;
}
print_r(array_merge($reds, $blues));
Merge your array before looping through the array.
Your foreach statements were converting your array to strings.
I have been trying to generate distinct random numbers using procedural code. But i am failing to do it. Please help me in this regard. thanks in advance.
<?php
$rnd_array = array(0,0,0,0,0,0,0,0,0,0);
$a = 1;
for ($i=0; $a <= 10 ; $i++)
{
echo $rnd = rand() % 10;
echo "\t \t";
for ($j=0; $j < 10 ; $j++)
{
if ($rnd == $rnd_array[$j])
{
$flag = true;
break;
}
else
{
$flag = false;
}
}
if ($flag == false)
{
$rnd_array[$a] = $rnd;
$a++;
}
}
echo "<br> <br>";
for ($k=0; $k < 10 ; $k++)
{
echo $rnd_array[$k];
echo "\t \t";
}
?>
I would use a somewhat different logic - start with an empty array, generate a random number, put it into the array if the array doesn't already contain it. Repeat until your array contains as many elements as you want it to.
Here's a simple example:
$num = 10;
$min = 1;
$max = 100;
$array = [];
while (count($array) < $num) {
$random = mt_rand($min, $max);
if (!in_array($random, $array)) {
$array[] = $random;
}
}
10 distincts random numbers
<?php
$numbers = array();
$i = 0;
while($i < 10){
$new = rand();
$known = false;
foreach($numbers as $key => $value){
if($value == $new ){
$known = true;
break;
}
}
if(!$known){
$numbers[$i]=$new;
$i++;
}
}
var_dump($numbers);
?>
Because array length is equal to range of generated random value
$rnd_array = range(0,9);
shuffle($rnd_array);
print_r($rnd_array);
Array
(
[0] => 9
[1] => 0
[2] => 5
[3] => 3
[4] => 6
[5] => 4
[6] => 2
[7] => 7
[8] => 1
[9] => 8
)