array_rand why will get 0 in range(1, 20) - php

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);
}

Related

Ways of getting 20% of elements in array - PHP

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));

How to get common minimum value in two arrays

I have a function where is checks on two arrays and returns the common minimum value in both the arrays. IT returns the correct answer when both the arrays have equal number of elements. But is the other other is bigger it does not return the correct one. How to overcome this?
<?php
$A = array(0);
$B= array(1,0);
$n = sizeof($A);
$m = sizeof($B);
sort($A);
sort($B);
$i = 0;
for ($k = 0; $k < $n; $k++) {
if ($i < $m - 1 AND $B[$i] < $A[$k])
$i += 1;
if ($A[$k] == $B[$i])
echo $A[$k];
}
echo "end";
?>
Thanks
A way simpler way would be to take the minimum value of the intersection of your arrays :
$array = array (5,6,7,8,9);
$array2 = array (9,7,5,3,4,1);
$min = min(array_intersect($array, $array2));
echo $min; // 5
Leverage PHP function array_intersect() to extract common values in two arrays, then fetch the minumum value by using min()
Like so:
<?php
$arrayOne = [1, 3, 5, 6, 7];
$arrayTwo = [0, 9, 2, 3, 4, 5];
echo min(array_intersect($arrayOne, $arrayTwo)); // 3
I would use array_intersect. It will give you an array with only values that are present in both arrays you are looking through, then you can find the lowest value in the array
$A = array(0,5,4,3,2,1,4);
$B = array(0,1,3,4);
$C = array_intersect($A, $B);
$low = $C[0];
for($i = 0; $i < count($C); $i++) {
if($C[$i] < $low)
$low = $C[$i];
}
echo "Lowest shared number is $low";

Given an array of integers, what's the most efficient way to get the number of other integers in the array within n?

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

Merge two flat indexed arrays of equal size so that values are pushed into the result in an alternating fashion

Suppose I have two arrays:
$a1 = array(0, 1, 2);
$a2 = array(3, 4, 5);
I want to be able to do a merge technique that alternates the array values and not just concatenate them. I want this result:
array(0, 3, 1, 4, 2, 5);
Is there a native way to do this as performance is an issue here since I need to do this thousands of times
Please note, I know I can do it like this:
for (var $i = 0; $i < count($a1); $i++) {
newArray[] = $a1[$i];
newArray[] = $b1[$i];
}
I'm looking for a built in way if there is a faster one.
$count = count($a1);
for ($i = 0; $i < $count; $i++) {
$newArray[] = $a1[$i];
$newArray[] = $b1[$i];
}
My work here is done.
$a1 = array(0,1,2);
$a2 = array(3,4,5);
$start = microtime(TRUE);
for($t = 0; $t < 100000; $t++)
{
$newArray = array();
$count = count($a1);
for ($i = 0; $i < $count; $i++)
{
$newArray[] = $a1[$i];
$newArray[] = $a2[$i];
}
}
echo round(microtime(TRUE) - $start, 2); # 0.6
$a1 = array(0,1,2);
$a2 = array(3,4,5);
$start = microtime(TRUE);
for($t = 0; $t < 100000; $t++)
{
$newArray = array();
for ($i = 0; $i < count($a1); $i++)
{
$newArray[] = $a1[$i];
$newArray[] = $a2[$i];
}
}
echo round(microtime(TRUE) - $start, 2); # 0.85
So pre-counting array size will be ~1/4 [citation needed] (on freakin' 100.000 iterations you will gain 0.2 in total) faster. If you put count() inside loop, it will recount on every iteration. 1/4 seems to me a reasonably faster. If you are looking for compiled function, you can stop.
P.S. Benchmark is like bikini, it shows you everything, and nothing.
Since you are "zippering" two indexed arrays, you can "transpose and flatten". I expect that this will not be quite as fast as using a for() or foreach(), but on small input arrays, there will be no noticeable drag on performance. In other words, when coding style or minimal declared variables is sought, then the following technique should be considered.
Code: (Demo)
$a1 = [0, 1, 2];
$a2 = [3, 4, 5];
var_export(
array_merge(...array_map(null, $a1, $a2))
);
Output:
array (
0 => 0,
1 => 3,
2 => 1,
3 => 4,
4 => 2,
5 => 5,
)
As a funky little function-less approach, you can push the $a1 value from the foreach() value declaration and inside the loop's body, you can push the $a2 value. Feast your eyes... (Demo)
$result = [];
foreach ($a1 as $index => $result[]) {
$result[] = $a2[$index];
}
var_export($result);
// same output as earlier snippet
For anyone that is hunting for an associative-safe technique, you could make looped slice or splice calls. Be aware that splice() will mutate/consume the input arrays in the process. (Slice Demo) (Splice Demo)
$result = [];
for ($i = 0, $count = count($a1); $i < $count; ++$i) {
$result += array_slice($a1, $i, 1)
+ array_slice($a2, $i, 1);
}
or
$result = [];
while($a1 && $a2) {
$result += array_splice($a1, 0, 1)
+ array_splice($a2, 0, 1);
}
p.s. I even build this utility snippet to offer more dynamic tooling of how many elements should be inserted and how frequently from each array while merging. See Insert elements from one array (one-at-a-time) after every second element of another array (un-even zippering).

Convert Python for -loop to PHP

How can you convert the following code to PHP?
summat = [sum(arra[i:i+4]) for i in range(0,len(arra),4)]
My attempt
$summat = array()
foreach ( range(0, $arra.length, 4) as $i) {
$summat = array ( array_sum( array_slice( $array, $i, $i+5) ) ) // don't know how to append the sums the array
$sum = array();
foreach(range(0, count($a), 4) as $i)
$sum []= array_sum(array_slice($a, $i, 4));
"[]=" is an append-to-array operator
slice's second parameter is slice length, not the last index
or even simpler
$sum = array_map('array_sum', array_chunk($a, 4));
To append a value to an array, use:
$summat[] = array_sum(...);
The PHP way of doing ranges is similar to the C way:
for($i = 0; $i < count($arra); $i += 4) {
// ...
}

Categories