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
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
I have an array like this one:
array (size=1)
0 =>
array (size=33)
0 => int 126
1 => int 43
2 => int 4
3 => int 0
4 => int 3
5 => int 3
6 => int 30
7 => int 15
8 => int 22
9 => int 27
10 => int 22
11 => int 46
12 => int 0
13 => int 8
14 => int 14
15 => int 8
array (size=1)
1 =>
array (size=33)
0 => int 273
1 => int 3
2 => int 4
3 => int 28
4 => int 36
5 => int 19
6 => int 142
7 => int 81
8 => int 59
9 => int 71
10 => int 88
11 => int 47
12 => int 42
13 => int 0
14 => int 12
15 => int 97
(of course it is way longer)
and I need both to sum all the value with the same key and count how many values with the same key are >0 (cause I have to find the avarage of all the numbers >0
My expected result is
0=>
'sum' => 399
'count'=>2
1=>
'sum' =>46
'count'=>2
how can I create this array?
There's an inbuilt function in PHP to count the sum of all the elements of an array. Here, this will give you your expected output :
<?php
$arr = [[10, 20, 30, 40], [10, 20, 30], [10, 20, 30, 4]];
// Let the magic happen...
$yourArray = array_map(function ($el){ return ["sum" => array_sum($el), "count" => count($el)]; }, $arr);
print_r($yourArray);
?>
I have thought about this, and came up with a solution (I think...), it comes in the form of a function and it goes like this:
function getSumAndCount(array $arr)
{
$sum = 0;
$count = 0;
foreach ($arr as $v)
{
$count++;
if (is_array($v))
{
$next = getSumAndCount($v);
$count += $next['count'];
$sum += $next['sum'];
}
else
{
!is_numeric($v) ?: $sum += $v;
}
}
return [ 'sum' => $sum, 'count' => $count ];
}
It has a recursive check to if the array is multidimensional, checks if the value is numeric and sets the count and sum in a return array.
I haven't tested this properly as yet, but please test and let me know if you get the desired output.
EDIT
I will extend this to count dupe keys soon
I'm working on it
$source = mb_convert_encoding('test', "unicode", "utf-8");
$source = unpack('C*', $source);
var_dump($source);
return:
array (size=8)
1 => int 0
2 => int 116
3 => int 0
4 => int 101
5 => int 0
6 => int 115
7 => int 0
8 => int 116
but i want this return:
array (size=8)
1 => int 116
2 => int 0
3 => int 101
4 => int 0
5 => int 115
6 => int 0
7 => int 116
8 => int 0
I want use this return in openssl function for encryption. just $source important to me, i write other code for debugging.
What can i do to solve this problem?
"Unicode" is not a real encoding; it's the name of the overarching standard and used as an alias for UTF-16BE mostly by Microsoft, and apparently PHP supports it for that reason. What you expect is UTF-16LE, so use that explicitly:
$source = mb_convert_encoding('test', 'UTF-16LE', 'UTF-8');
I'm new to php so please take it easy.
I have created an array of integers. 1-100. What I want to do is shuffle the array and remove random numbers from it leaving only lets say 15 numbers.
This is what I have done so far, can't figure out how to remove the random numbers. I know I could possibly use unset function but I'm unsure how could I use it in my situation.
// Create an Array using range() function
$element = range(1, 100);
// Shuffling $element array randomly
shuffle($element);
// Set amount of number to get rid of from array
$numbersOut = 85;
// Remove unnecessary items from the array
var_dump($element);
Just try with:
$element = range(1, 100);
shuffle($element);
$output = array_slice($element, 0, 15);
var_dump($output);
Output:
array (size=15)
0 => int 78
1 => int 40
2 => int 10
3 => int 94
4 => int 82
5 => int 16
6 => int 15
7 => int 57
8 => int 79
9 => int 83
10 => int 32
11 => int 13
12 => int 96
13 => int 48
14 => int 62
Or if you want to use $numbersOut variable:
$numbersOut = 85;
$output = array_slice($element, $numbersOut);
It will slice an array from 85 to the end. Remember - if you will have 90 elements in the array, this method will return just 5 elements.
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;
}