rand() function that should not repeat [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a code that present 4 pictures from a folder.
I use rand() function in order to present each time different picture for each one of these pictures, but sometimes, I receive the same picture in the same page.
what should I do in order to not receive the same picture twice?

I'd save all possible images in an array and shuffle it, something like this:
$images = array('1.png','2.png','3.png','4.png');
shuffle($images);
foreach ($images as $image) {
// do something with the image
}

I have make an function to do this. Use the code below
<?php
function GenRandom($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
return array_slice($numbers, 0, $quantity);
}
foreach(GenRandom(1,4,4) as $test){
echo $test.".png<br>"; // generates unique id everytime
}
?>
Hope this helps you

Generate the array with all the pictures not randomized.
Random the whole array.
If needed save the randomized array in db (or somewhere else).
When showing a picture get the first value from the array to get random name.
Remove first value from the array.
Repeat points 4-5 on each request.
If array is empty go to point 1.

Do you start with all images in an array?
If so, I would personally use shuffle($pictures);
Then perhaps array_chunk($pictures, 4) to get groups of 4.

Related

How to proccessing array in array in laravel [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Can someone help me with this difficult array
In the web im find all cities and regions of Ukraine and im want to display this in select-option tag
I want to show separately regions and cities
This is link of json file
Also im saved this JSON file on my PC
This is my code of proccessing arrays
public function index()
{
$result = json_decode(file_get_contents('5.json'));
foreach ($result as $k => $v)
{
foreach ($v as $item)
{
dd($item);
}
}
return response()->json($item);
}
I think should format $result to array
$result = json_decode(json_encode($result), true);
Laravel provides a fluent wrapper for arrays. See Collections.
Initialize a collection by:
$result = json_decode(file_get_contents('5.json'));
$collection = collect($result);
Looking at your data, I don't know if this will perform well. I had experiences before where performance starts to become a problem, so please be wary of usage.

PHP do-while loop filesize check and stop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So the problem is I'm having is looping to check the file size, for example a file A.txt is 10 bytes and with other php scripts A.txt changes to 20bytes so when the A.txt file is changed I want the iteration to stop.
<?php
$i = 1;
$a = filesize("A.txt");
do {
$c = filesize("A.txt");
if ($a >= $c) {
} else {
echo file_get_contents('chat.log');
$i++;
}
} while ($i <= 2);
?>
What is my mistake?
It seems like you want to implement long-polling for a chat application.
This approach could lead you to lose some chat messages being delivered. And this is not very scalable.
I would suggest you to receive the previously known file size via a GET parameter, and check if the file size is larger than that.
Then I would suggest you to use fopen to open the file and read from the previous pointer (GET parameter) to the end of file, than reading the whole file every time.
you may also want to clearstatcache before each filesize, and add a usleep(100); inside the while loop.

Insert a variable in blank array that variable contains array key and value [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
My variable which retrieve from database is:
$MyVariable="1=>'raju',2=>'rana',3=>'keya',4=>'muaz',5=>'',6=>'Asif'";
My array will be:
$MyArray=array($MyVariable);
Now I want to print a value using a key. Like:
echo $MyArray[2];
My output should be:
rana
But output is nothing!
eval() has security implications, but that's how this string of partial crap is turned into an actual array:
eval('$MyArray = array('.$MyVariable.');');
print_r($MyArray);
You should really store each value as a related row in another table instead of array data.
Your variable $myVariable is a string consisting of array keys.
If you were to write it as an array, it would work without problems:
$myvariable = array(
1 => 'raju',
2 => 'rana',
3 => 'keya'
// More entries
);
If this is the output of a function, you may want to make that function output JSON instead, as that is parseable to an array with just one json_decode call.
If you still want to parse this, you'll have to do some more advanced string parsing:
$data = array();
$bits = preg_match_callback("/(\d+)=>'(.*?)'(\,)?/", function($matches) use ($data){
$data[$matches[1]] = $matches[2];
}, $MyVariable);

PHP: Match corresponding file names [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This one may be a bit of a doozy.
I have 2 sets of files in separate folders:
SET 1:
celeb1.png
celeb2.png
celeb3.png
celeb4.png
celeb5.png
etc
The user selects an image (radio) and I then set this as a get variable on the url:
Now, I want to match another file.
In another folder, I have a set like this:
SET 2:
celeb1-24_59_250_300.png
celeb2-35_67_200_250.png
celeb3-54_87_300_400.png
celeb4-88_98_250_350.png
celeb5-87_43_300_400.png
etc
I want to use the GET variable (i.e.) celeb1.png, and then select the full filename of the corresponding file, based on the first 5 characters.
In other words if $_GET['celebimg'] is 'celeb1.png', I want to set a second variable (let's say $overlay) to string 'celeb1-24_59_250_300.png'
Any idea on how to achieve that?
I could obviously do a switch, but that means I would have to update the code every time a new celeb image is uploaded.
Is there any dynamic way to achieve this?
Thanks
JG
You can try something like this. Since we haven't seen what you have tried so far. This is just an illustration. However the code has been tested.
<?php
$file='celeb3.png'; // Here is where you will get the param $_GET['set1']
$set2=array(
'celeb1-24_59_250_300.png',
'celeb2-35_67_200_250.png',
'celeb3-54_87_300_400.png',
'celeb4-88_98_250_350.png',
'celeb5-87_43_300_400.png'
);
$set1 = explode('.',$file);
//echo $set1[0];//celeb3
foreach($set2 as $val)
{
if($set1[0]==substr($val,0,strlen($set1[0])))
{
echo $val;//celeb3-54_87_300_400.png
break;
}
}
OUTPUT : celeb3-54_87_300_400.png
Use glob to search a folder
//Get the GET variable
$cimg = $_GET['celebimg'];
//split it so we can just get the name without extention
$nameparts = explode(".",$cimg);
//glob will try to find matches to the string passed
$matches = glob("/dir/path/{$nameparts[0]}-*.png");
$matches will be an array of matched files
you could use other file functions like scandir or readdir etc but those do not filter out the files so you would have to do it.

Make a 1 item array be seen as an array [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 8 years ago.
Improve this question
I am currently working on a php project where I am trying to loop through an array using a foreach. However, sometimes the array may only contain 1 item so when I try and do a foreach it fails as 1 item is just seen as a normal variable.
Is there a way that I can trick php into thinking that a 1 item array is actually an array and not just a variable so that I don't get this error.
Thanks for your help.
foreach will work fine with arrays of size 0,1 or bigger. I suspect your problem is, that the variable doesn't really contain an array, but some scalar value - in this case use something like
if (!is_array($var)) $var=array($var);
foreach ($var as $item) {
//...
}
if(is_array($arr))
$arr2=$arr
else
$arr2=array($arr)
and then you iterate over $arr2
I would recommend just using a standard for loop. It should work no matter what the length of the array is
for($i = 0, $l = count($myArray); $i < $l; $i+=1){
//code in here
}
But chances are you have an issue with your array to begin. Posting the structure would be helpful, or you should var_dump it to make sure it is indeed an array.

Categories