I want to make an array with a random amount of values between 2 and 5.
Each value would be 50.
What's the easiest way to do this?
eg:
$array = array(50,50,50,50)
$array = array(50,50)
$array = array_fill(0,rand(2,5), 50) is the simplest I can think of.
Try with:
$array = array_fill(0, rand(2, 5), 50);
$array = array();
for($i = 0, $max = rand(2,5); $i < $max; $i++) $array[] = 50;
This will create an array with 2 to 5 items with the value 50.
Edit: An easier solution is to use array_fill():
$array = array_fill(0, rand(2, 5), 50);
Take a look at mt_rand(). To generate a random number between 2 and 5 (inclusive):
$random = mt_rand(2, 5);
Related
I have an array of n elements and I need to get random 20% of those elements into another array. Is there any function which can achieve this?
Currently what I can think of is this:
foreach ($orders as $order) {
if (rand(1, 100) > 80) {
echo('20%');
} else {
echo('80%');
}
}
Is there a more optimal way?
You could shuffle the array and then take the first 20% elements.
$array = [1, 2, 3, 4];
shuffle($array);
$twenty = array_slice($array, 0, floor(count($array) / 5));
$eighty = array_slice($array, floor(count($array) / 5));
The simplest solution is probably to use shuffle:
shuffle($orders);
for ($i = 0; $i < count($orders) / 5; $i++) {
// do something with the first 20% of elements
}
for (; $i < count($orders); $i++) {
// do something with the rest of the array
}
To get two arrays by one function call, use array_splice function. After
shuffle($array);
$twenty = array_splice($array, floor(count($array) / 5 * 4));
$twenty will held 1/5 of source array and $array - other items
Shuffle is the simplest solution for this case
$array = [1,2,3,4]
shuffle($array);
$firstSlice = array_slice($array , 0 , count($array)/2);
$secondSlice = array_slice($array , count($array)/2 , count($array));
Why is 0 returned from array_rand()? How can I filter out 0? I just want to get 1-20.
$range = range(1, 20);
$total = array();
for($i = 0; $i < 100; $i++){
shuffle($range);
array_push($total, array_rand($range, 3));
}
array_rand() returns the key(s) from the array, and range() built an array with a starting key of 0. Try:
$range = array_combine($range = range(1, 20), $range);
Or to treat the keys as the values and not use the actual values:
$range = array_flip(range(1, 20));
However, you have already shuffled the array so no need for array_rand(), just slice 3 from the beginning:
$range = range(1, 20);
for($i = 0; $i < 100; $i++){
shuffle($range);
$total[] = array_slice($range, 0, 3);
}
i have 2 arrays of PHP, one having ids of online users and second having ids of idle users, but in idle users array some ids of online user's also exist, i want to compare both arrays and remove user ids from 2nd array in php. how can i do this?
use
$idleWithoutOnline = array_diff($idleUsers, $onlineUsers);
http://php.net/manual/en/function.array-diff.php
yes thats possible by array_diff
have a look at
http://php.net/manual/en/function.array-diff.php
Use this
$array1 = array('[param1]' ,'demo' ,'[param2]' ,'some' ,'[param3]');
$array2 = array('value1' ,'demo' ,'value2' ,'some' ,'value3');
array_unique( array_merge($arr_1, $arr_2) );
or you can do:
$arr_1 = array_diff($arr_1, $arr_2);
$arr_2 = array_diff($arr_2, $arr_1);
$online_users = array(1, 5, 7, 8);
$idle_users = array(6, 4, 5, 8, 9);
for($i = 0; $i < count($idle_users); $i++)
{
foreach($online_users as $v)
{
if($idle_users[$i] == $v)
{
unset($idle_users[$i]);
}
}
}
print_r($idle_users); // This will give you 6,4,9 as idle users
Given the following array:
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
And assuming $n = 2, what is the most efficient way to get a count of each value in the array within $n of each value?
For example, 6 has 3 other values within $n: 5,7,7.
Ultimately I'd like a corresponding array with simply the counts within $n, like so:
// 0,0,1,2,2,5,6,7,7,9,10,10 // $arr, so you can see it lined up
$count_arr = array(4,4,4,4,4,3,3,4,4,4, 2, 2);
Is a simple foreach loop the way to go? CodePad Link
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
$n = 2;
$count_arr = array();
foreach ($arr as $v) {
$range = range(($v-$n),($v+$n)); // simple range between lower and upper bound
$count = count(array_intersect($arr,$range)); // count intersect array
$count_arr[] = $count-1; // subtract 1 so you don't count itself
}
print_r($arr);
print_r($count_arr);
My last answer was written without fully groking the problem...
Try sorting the array, before processing it, and leverage that when you run through it. This has a better runtime complexity.
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
asort($arr);
$n = 2;
$cnt = count($arr);
$counts = array_pad(array(), $cnt, 0);
for ($x=0; $x<$cnt; $x++) {
$low = $x - 1;
$lower_range_bound = $arr[$x]-$n;
while($low >= 0 && ($arr[$low] >= $lower_range_bound)) {
$counts[$x]++;
$low--;
}
$high = $x + 1;
$upper_range_bound = $arr[$x]+$n;
while($high < $cnt && $arr[$high] <= $upper_range_bound) {
$counts[$x]++;
$high++;
}
}
print_r($arr);
print_r($counts);
Play with it here: http://codepad.org/JXlZNCxW
I have an array which I want to slice in 4 other arrays because I want to display the content of the first array on four columns.
I have tried the code above, but what I get is N columns with 4 items.
$groups = array();
for ($i = 0; $i < count($menu); $i += 4) $groups[] = array_slice($menu, $i, 4);
Can this be modified in order to get exactly 4 columns and distribute the values so they fit?
Like Michael Berkowski suggested:
$groups = array_chunk($menu,4);
Should give you what you need. If you're more into "manual labour":
$groups = array();
while($groups[] = array_splice($menu,0,4))
{//no need for any code here ^^ chunks the array just fine
printf('This loop will run another %d times<br/>',(int)ceil(count($menu)/4));
}
Update:
I see I got this a bit wrong... want to chunk into 4 arrays, not into arrays of four:
$groups = array_chunk($menu,(int)ceil(count($menu)/4));
You can try
// Some Random array
$array = range(1, 20);
// Split it 4 Chuncks
$array = array_chunk($array, 4);
// Slice The first 4 Chunks
$array = array_slice($array, 0, 4);
// Output Result
foreach ( $array as $set ) {
printf("<li>%s</li>", implode(",", $set));
}