I am having a problem returning random array keys if the specified number of entries is the same as the number of items in the array.
$rdm = array_rand($similar_product_array, 4);
will always return key values 0, 1, 2, 3 if there is 4 items in the array.
for example:
// Items in array
array (size=4)
0 => string 'Batman Heroes Edition Nendoroid' (length=31)
1 => string 'Oberyn' (length=6)
2 => string 'White Walker' (length=12)
3 => string 'Avengers Age of Ultron Hulk' (length=27)
// "randomly" generated array keys is always 0 , 1, 2, 3
array (size=4)
0 => int 0
1 => int 1
2 => int 2
3 => int 3
however, if i have:
$rdm = array_rand($similar_product_array, 3);
// Returns randomly as expected
array (size=3)
0 => int 0
1 => int 2
2 => int 3
it will return randomly generated keys as it should.
What could i be doing wrong here?
You misunderstood purpose of array_rand() function, it is supposed to give you random entries from array, but not in random order. That means that if you are asking for 4 random items from array with 4 items, it will always return all the items (in the original order).
If you just need to change randomly the order of array entries, use shuffle() function, for example in this way:
$array_copy = $array;
shuffle($array_copy);
$rdm = array_rand($array_copy, <how_many_you_need>);
Related
I have a site with a search feature but am trying to improve the search for a fuzzy search.
So far what I've done is query all of my products and use similar_text() to see how close they are to the actual search term
I then create an array where the key is how similar it is and the value is the product id. I then sort this by the key/similarity.
$term = $_GET['search_term'];
$similar_array = [];
$sql = "SELECT * FROM products";
$stmt = DB::run($sql);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$id = $row["pro_id"];
$result = $row['product'];
similar_text($term,$result,$similarity);
$similar_array[$similarity][] = $id;
}
$closest_match = array_keys($similar_array);
rsort($closest_match);
$match_count = count($closest_match);
for($i=0; $i<$match_count; $i++){
var_dump($similar_array[$closest_match[$i]]);
}
This gives me something like:
array (size=9)
0 => int 28
1 => int 1628
2 => int 1665
3 => int 1666
4 => int 1667
5 => int 1668
6 => int 1669
7 => int 1670
8 => int 1671
C:\wamp64\www\V2\search.php:65:
array (size=2)
0 => int 37
1 => int 38
C:\wamp64\www\V2\search.php:65:
array (size=1)
0 => int 481
C:\wamp64\www\V2\search.php:65:
array (size=3)
0 => int 27
1 => int 1009
2 => int 1620
C:\wamp64\www\V2\search.php:65:
array (size=14)
0 => int 30
1 => int 104
2 => int 131
3 => int 134
4 => int 168
5 => int 169
6 => int 170
7 => int 557
8 => int 1011
9 => int 1014
10 => int 1661
11 => int 1662
12 => int 1663
13 => int 1664
I have a show_products() function that I just need to pass an array of ID's, so what I want to do now is take the multiple arrays of ID's from above and merge it in this specific order and then crop the array to a certain number so it won't have every product but will cut off after so many results.
As per your query you can use follow below steps
you merge all array by array_merge
sort the array using rsort (highest merge come first);
for display you can use array_slice or array_chunk
to be precise I have:
$foo = [1, 4, 1, 5, 8, 1, 3, 5, 1, 4, 1, 3, 7, 2];
sort($foo);
$bar = array_count_values($foo);
for ($i = 1; $i < count($bar); $i++) {
if (!isset($bar[$i])) {
$bar[$i] = 0;
}
}
Actual result:
array (size=8)
1 => int 5
2 => int 1
3 => int 2
4 => int 2
5 => int 2
7 => int 1
8 => int 1
6 => int 0
Expected result:
array (size=8)
1 => int 5
2 => int 1
3 => int 2
4 => int 2
5 => int 2
6 => int 0
7 => int 1
8 => int 1
Why my key value pair 6 => 0 appears in the bottom of an array instead of a specific place?
It's because you are appending a new value to the array.
To "fix" that, use ksort($bar);.
As #Rob Ruchte wrote in comments, the array returned by array_count_values() is associative, which means there is no order with the keys.
http://php.net/manual/en/function.array-count-values.php
Returns an associative array of values from array as keys and their count as value.
I am trying to pass variables to the PHP pecl extension's 'trader' project's trader_stochrsi() function.
here is my use example :
$stochrsi = trader_stochrsi(array(5.5), 14, 3, 3);
var_dump($stochrsi);
I get the following read-out in the var_dump :
bool(false)
--Any thoughts as to why this may be happening?
Thanks,
GS
You are specifying 14 intervals the function must have to be able to give an RSI value, but your array only contains one interval i.e. 5.5.
You should put 15 items in your array. It will use the first 14 to calculate a value and output it for 16th interval.
For me it works after 19 elements:
array (size=19)
0 => float 1.298E-5
1 => float 1.246E-5
2 => float 1.129E-5
3 => float 1.091E-5
4 => float 1.015E-5
5 => float 1.075E-5
6 => float 1.056E-5
7 => float 1.046E-5
8 => float 1.07E-5
9 => float 1.046E-5
10 => float 1.113E-5
11 => float 1.163E-5
12 => float 1.216E-5
13 => float 1.253E-5
14 => float 1.295E-5
15 => float 1.356E-5
16 => float 1.285E-5
17 => float 1.43E-5
18 => float 1.426E-5
->>>
array (size=2)
0 =>
array (size=1)
18 => float 100
1 =>
array (size=1)
18 => float 100
I've been searching for similar question but haven't found no one with similar problem or possible solution.
I have preg_match_all function which finds me 5 matches and returns them in array. How can I count strings that are in array ?
preg_match_all("/\)\">(.*?)<\/a>/s",$value,$counter);
here is var_dump(counter[1]):
array (size=1)
0 => string 'word1' (length=..)
array (size=1)
0 => string 'word2' (length=..)
array (size=1)
0 => string 'word3' (length=..)
array (size=1)
0 => string 'word4' (length=..)
array (size=1)
0 => string 'word5' (length=..)
As you seen it returns me 5 matches, but i cant print that number neither with array_count_values, count()... I've tried imploding then to get strings but count() wont work either.
count($counter[1]) returns me ->
int 1
int 1
int 1
int 1
int 1
I need to get number of matches and I dont have any more ideas, so anything is very welcome.
Update: Here is var_dump($value);
string '<h2>word1</h2><div class="show"><p class="nums"><span class="phone">03 839 11 00</span></p>' (length=170)
string '<h2>word2</h2><div class="show"><p class="nums"><span class="phone">03 839 12 00</span><span class="fax">03 839 12 18</span><span class="email email-mod"><a title="info#golte.si" href="mailto:info#golte.si"><img alt='info#golte.si' src='Txt2Image.ashx?AAARAAcASQAAAB0AEgAGADMAEwAGAB8AAAAMAF0ABwAAAFUAGQAGABcAEQBUAAAAAAAQAB8AEQANAFUAEgAGAB0AAABUADIABgAAABIAGABSAEIAQABSAAMADABSAFAARQAtAEcARQBeADYATwALABwAGAANAEMARABZAEMARQBdADIARQA=' /></a></s'... (length=938)`
string '<h2>word3</h2><div class="show"><p class="nums"><span class="phone">03 839 12 12</span></p>' (length=162)
string '<h2>word4</h2><div class="show"><p class="nums"><span class="phone">03 839 12 14</span></p>' (length=168)
string '<h2>word5</h2><div class="show"><p class="nums"><span class="phone">03 839 61 28</span></p>' (length=162)
preg_match_all itself returns the number of full pattern matches (which might be zero), or FALSE if an error occurred.
$number = preg_match_all("/\)\">(.*?)<\/a>/s",$value,$counter);
<?php
/* function strlen1($val)
{
return strlen($val);
} */
//get the each value count
$b = array_map('strlen', $counter);
print_r($b);
I have an array (sample below) containing 3 arrays with very similar content. I was wondering if there was an easy way to put everything into one array, rather than 3 separate ones. Everything I try seems to just overwrite the data and I'm left with the info from the 3rd array only.
array
0 =>
array
'america' => int 19
'music' => int 6
'states' => int 5
'bank' => int 5
1 =>
array
'america' => int 19
'home' => int 3
'society' => int 2
'writers' => int 2
2 =>
array
'america' => int 19
'lutheran' => int 4
'church' => int 4
'national' => int 4
'cruises' => int 3
Ideally the end result will look like this:
array
'america' => int 19
'music' => int 6
'states' => int 5
'bank' => int 5
'america' => int 19
'home' => int 3
'society' => int 2
'writers' => int 2
'america' => int 19
'lutheran' => int 4
'church' => int 4
'national' => int 4
'cruises' => int 3
Any solutions? thanks
You won't be able to have an associative array with the same key values (i.e. 3 keys of "America", etc.). When you try to set those keys in the array, you will simply overwrite the previous value.
I was wondering if there was an easy way to put everything into one
array
Short answer is no.
You could make a class with a variable for the original int and a value for the number of times it occurs and then have the array point to objects of the class. Same idea could be implemented using a linked list, though both ways are more complex than what you already have.
For example the key 'america' would point to an object with one variable set to 19 and the other variable set to 3.
Question: do you need to know which of the original arrays a value comes from? If yes then what I mentioned above would loose that property.
http://php.net/manual/en/function.array-merge.php
function array_2d_to_1d($array)
{
$my_val=array();
$my_key=array();
foreach ($array as $value) {
array_push($my_val, $value['cat_title']);
array_push($my_key, $value['cat_id']);
}
$result = array_combine($my_key, $my_val);
return $result;
}