I have some numbers which will be part of an sql query, like this...
152258, 152258, 152258, 152258, 152261, 152261, 152261, 152261, 152261, 152270,
152270, 152270, 152287, 152287, 152287, 152287
My query is quite complex and the list is quite long and I need to produce a unique list of numbers, can I do this with a regex?
If not how ?
Here is how you can make your string have unique numbers:
http://codepad.org/cziNtZOS
<?php
$myString = "152258,152258,152258,152258,152261,152261,152261,152261,152261,152270,152270,152270,152287,152287,152287,152287";
$array = explode(",",$myString);
$unique = array_unique ( $array );
$myUniqueString = implode(",",$unique);
echo $myUniqueString ;
?>
I was going to write a function for you, turns out PHP has one already: array_unique()
Edit: in any case, this is one of the alternative ways of doing it:
function uniquify_array($a)
{
$b = array(); // I wish I could write just $b = {};
foreach ($a as $i)
$b[$i] = $i;
return $b;
}
Note that this function works only with values, whereas the built-in array_unique() preserves the keys in the original array.
Generate some set of numbers and then just use array_unique function
$nums = array();
for ($i = 0; $i < $limit; $i++) {
$nums[] = rand() * 10000;
$nums = array_unique($nums);
}
Sultan
If you want a range of numbers use PHP's range() method
If you want a unique list of numbers, you can use PHP's rand() method
range() : http://php.net/manual/en/function.range.php
rand() : http://php.net/manual/en/function.rand.php
EDIT
And as #RobertPitt points out, uniqid can be used for generating IDs as well : http://php.net/manual/en/function.uniqid.php
USE RANGE()
REFERENCE
Related
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);
?>
I've created a collection of variable variables using a foreach loop and I want to find out how many there are; and then select one with pseudo randomness.
Example code:
$count = 0;
foreach ($a as $key => $b) {
$count++;
$new_stuff["stuff" . $count] = $b;
}
extract($new_stuff);
I can then echo out each stuff, like this:
echo $stuff1[0];
or...
echo implode($stuff1); (this one gives me double the result)
I want to find out how many $stuff variables there are* and then pick one with one with rand(). Is it somehow possible to construct a variable from two parts, like this pseudocode: $stuff = "stuff" and attaching, say, number 5 so that it becomes $stuff5?
*I can find out the number of elements that create the various $stuff variables by using this:
$shift = rand(0, count($a));
array_rand is an option but rand() is not a very reliable random number generator.
Assuming your keys are numeric, it would be better to use mt_rand() with count().
$random = $a[ mt_rand(0, count($a)-1) ];
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);
i want to get static length to get any random value from array.
PHP CODE:
in this below code how to get 5 random value from array?
$arr_history = array(23, 44,24,1,345,24,345,34,4,35,325,34,45,6457,57,12);
$lenght=5;
for ( $i = 1; $i < $lenght; $i++ )
echo array_rand($arr_history);
You can use array_rand() to pick 5 random keys and then use those to intersect with the array keys; this keeps the original array intact.
$values = array_intersect_key($arr_history, array_flip(array_rand($arr_history, 5)));
Demo
Alternatively, you can first shuffle the array in-place and then take the first or last 5 entries out:
shuffle($arr_history);
$values = array_slice($arr_history, -5);
This has the advantage that you can take multiple sets out consecutively without overlaps.
Try this :
$rand_value = array_rand($arr_history);
echo $rand_value;
REF: http://php.net/manual/en/function.array-rand.php
OR use :
shuffle($arr_history)
This will shuffle the order of the array : http://php.net/manual/en/function.shuffle.php
I am not sure if its a good idea, but i just thought it would be less tedious and much easier to declare variables on the fly using a for loop:
$val.$i = $row1[$i];. Now after trying this, this obviously isn't the right thing to do. Is there anyway i can improve this and not declare separate variables.
Maybe this will give a clearer picture:
for($i = 1; $i < 5; $i++) {
$val.$i = $row1[$i];
}
Now i want to achieve $val1 using $val.$i.
As others have posted, using an associative or 0-based array would be a far better implementation, but you can implement the solution just as you have requested using PHP's variable variable names:
for ($i = 1; $i <= 5; $i++)
{
${"val".$i} = "this is value " . $i;
}
echo "$val1<br />$val2<br />$val3<br />$val4<br />$val5";
Will output:
this is value 1
this is value 2
this is value 3
this is value 4
this is value 5
In PHP you can define variables by name.
Example:
$foo = 'bar';
$$foo = 'baz';
echo $bar; // echoes 'baz'
So in your case, it would look like:
$var = 'val'.$i;
$$var = $arr[$i];
Why you would do that, I have no idea.
A better system (imho) is to use list() construct:
list($val1, $val2, $val3) = $arr;
You could create an associative array and then use extract:
$arr = array();
// ...
$arr[$val.$i] = $row1[$i];
// ...
extract($arr);
Mind you, it will probably be better to use the array in the first place.
I believe you're trying to get all the data you are reading from your database into one easy to access place. All you need to do is shove each row into an array:
$allTheThings[] = $row1;
You'll end up with a two dimensional array where the first key is the row number and the second key is the column number.
I am not getting full picture of what you are trying to do, but to convert arrays into objects you would do this:
$val = (object) $row1;