Fill an array with random values and then count them - php

Is there a way to do a Random Filled Array that counts and returns the lowest most repeated number? I know I could use the array_fill and arra_rand but I do not know the correct way to use them.

Instead of using array_fill. You generate a random values and put them in array.
<?php
$arr = [];
for($i=0;$i<10;$i++){
$arr[$i] = rand(1, 5);
}
$a = array_count_values($arr);
$b = array_keys($a, min($a));
print_r($b);
?>

Related

How to set a string as array and set array maximum

I want to make an array with the same value and set the array to be maximum based on my counter.
For example, I want to create an array with a string "hello" and I want it to keep going until the number of array is 3.
How is that possible using php with yii2 framework?
you can use a for loop this way
$array= array();
for($i=0;$i<3;$i++){
$array[] = "hello";
}
You will get $array with 3 hello.
try array_fill
$count = 5;
$a = array_fill(0, $count, 'hello');

PHP renumber array but keep sequence

I have an array like this:
array(13,4,7,1,16);
I want to recount the array, but I want to keep the sequence, like this:
array(4,2,3,1,5);
How can I do this?
If you are trying to sort the array, keeping the keys in the same order as the values, the PHP asort() function does that.
If you want to keep the original array but get the keys in sort order, then you can use something like:
$arr = array(13,4,7,1,16);
asort($arr);
$keys = array_keys($arr);
Then $keys has the keys from the original array sorted in the order of the original values, e.g. $keys = array(4,2,3,1,5);
if you want to get the index of the array with reference to the sorted values
Try this
$numbers = array(13,4,7,1,16);
$numberscopy = $numbers;
sort($numberscopy);
$final = array();
//echo array_search(13, $numbers);
for($a=0 ; $a<count($numberscopy );$a++){
$final[] = array_search($numberscopy[$a], $numbers) + 1;
}
var_dump($final);

What's the most efficient way to array_pop() the last n elements in an array?

What's an efficient way to pop the last n elements in an array?
Here's one:
$arr = range(1,10);
$n = 2;
$popped_array = array();
for ($i=0; $i < $n; $i++) {
$popped_array[] = array_pop($arr);
}
print_r($popped_array); // returns array(10,9);
Is there a more efficient way?
Use array_splice():
If you're trying to remove the last n elements, use the following function:
function array_pop_n(array $arr, $n) {
return array_splice($arr, 0, -$n);
}
Demo
If you want to retrieve only the last n elements, then you can use the following function:
function array_pop_n(array $arr, $n) {
array_splice($arr,0,-$n);
return $arr;
}
Demo
It's important to note, looking at the other answers, that array_slice will leave the original array alone, so it will still contain the elements at the end, and array_splice will mutate the original array, removing the elements at the beginning (though in the example given, the function creates a copy, so the original array still would contain all elements). If you want something that literally mimics array_pop (and you don't require the order to be reversed, as it is in your OP), then do the following.
$arr = range(1, 10);
$n = 2;
$popped_array = array_slice($arr, -$n);
$arr = array_slice($arr, 0, -$n);
print_r($popped_array); // returns array(9,10);
print_r($arr); // returns array(1,2,3,4,5,6,7,8);
If you require $popped_array to be reversed, array_reverse it, or just pop it like your original example, it's efficient enough as is and much more direct.
Why not use array_slice. You can give a start and a length, so if you do 2 from the end you will get the last two items in the array:
$arr = range(1,10);
$n = 2;
$start = count($arr) - $n;
print_r(array_slice($arr, $start, $n));
Thanks for the array_slice comments. I don't know why that didn't immediately come to mind.
It looks (to me) like the easiest way is:
$arr = range(1,10);
$n = 2;
$popped_array = array_slice($arr,-$n);
print_r($popped_array); // returns array(10,9);

Creating an associative array with random values PHP

I am trying to generate an associate array with random values. For example, if I give you this string:
something, anotherThing, foo, bar, baz
(the length of the string is dynamic - so there could be 10 items, or 15);
I would like to create an array based on those values:
$random = rand();
array("something"=>$random, "anotherThing"=>$random, "foo"=>$random, "bar"=>$random, "baz"=>$random);
And it builds the array based on how many values it's given.
I know how to order them into an array like so:
explode(", ", $valueString);
But how can I assign the values to make it an associative array?
Thanks.
NOTE: I am assuming that you want each item to have a different random value (which is not exactly what happens in your example).
With PHP 5.3 or later, you can do this most easily like so:
$keys = array('something', 'anotherThing', 'foo', 'bar', 'baz');
$values = array_map(function() { return mt_rand(); }, $keys);
$result = array_combine($keys, $values);
print_r($result);
For earlier versions, or if you don't want to use array_map, you can do the same thing in a more down to earth but slightly more verbose manner:
$keys = array('something', 'anotherThing', 'foo', 'bar', 'baz');
$result = array();
foreach($keys as $key) {
$result[$key] = mt_rand();
}
print_r($result);
all example are good, but not simple
Init array
$arr = array();
How many values your need?
$m = 10;
save random to all elements of array
for ($i=0;$i<$m;$i++)
{
$arr[$i] = mt_rand();
}
Why make more complex this simple example?
, Arsen
I suppose you have the keys in $key_array. This will make $random the value of each key:
$random = rand();
$array = array_fill_keys($key_array, $random);
If you need a way to apply different random values to each element, here's one (of several) solutions:
$array = array_fill_keys($key_array, 0);
foreach($array as &$a) {
$a = rand();
}

How to compare two arrays and remove matching elements from one for the next loop?

How else might you compare two arrays ($A and $B )and reduce matching elements out of the first to prep for the next loop over the array $A?
$A = array(1,2,3,4,5,6,7,8);
$B = array(1,2,3,4);
$C = array_intersect($A,$B); //equals (1,2,3,4)
$A = array_diff($A,$B); //equals (5,6,7,8)
Is this the simplest way or is there a way to use another function that I haven't thought of? My goal is to have an array that I can loop over, pulling out groups of related content (I have defined those relationships elsewhere) until the array returns false.
You've got it. Just use array_diff or array_intersect. Doesn't get much easier than that.
Edit:
For example:
$arr_1 = array_diff($arr_1, $arr_2);
$arr_2 = array_diff($arr_2, $arr_1);
Source
Dear easy and clean way
$clean1 = array_diff($array1, $array2);
$clean2 = array_diff($array2, $array1);
$final_output = array_merge($clean1, $clean2);
See also array_unique. If you concatenate the two arrays, it will then yank all duplicates.
Hey, even better solution: array _ uintersect.
This let's you compare the arrays as per array_intersect but then it lets you compare the data with a callback function.
Try to this
$a = array(0=>'a',1=>'x',2=>'c',3=>'y',4=>'w');
$b = array(1=>'a',6=>'b',2=>'y',3=>'z');
$c = array_intersect($a, $b);
$result = array_diff($a, $c);
print_r($result);

Categories