Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a array with 2500 elements..I want simple algorithm to get the following.
If the input is 1 , then it should return 1st 50 elements.
If the input is 2 it should return 51 to 100th element.
if input is 3 it should return 101 to 150th element.
I am too much confused with this algorithm and my mind is not giving any idea,Some one please help me.
Note: i am coding my own pagination for 2500 url..And i need just algorithm idea..not coding
<?php
$ar = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];
function return_frame($arr, $multiplier, $frame_size=50, $preserve_keys = false) {
return array_slice($arr, ($multiplier - 1)*$frame_size, $frame_size, $preserve_keys);
}
print_r(return_frame($ar, 2, 15, false));
?>
Array
(
[0] => 16
[1] => 17
[2] => 18
[3] => 19
[4] => 20
[5] => 21
[6] => 22
[7] => 23
[8] => 24
[9] => 25
[10] => 26
[11] => 27
[12] => 28
[13] => 29
[14] => 30
)
Take a look at: http://php.net/manual/en/function.array-slice.php
i think you just can add position and range on your sql code like this :
"select * from blabla order by post desc limit $position,$range"
You have indexes from 0 to 2499 right.
you want a $count of 50.
Let me show you the thinking process.
so if you get 1 as a $paramter you want $start to be 0 and $end to be 49.
lets see:
1-$parameter is $start right. and 50-$paramter is $end . ok
for 2 you want $start to be 50 and $end to be 99;
the above does not work here. lets play with the $count
$parameter*count; is almost $end.. we have to play for the 0 indexing. so:
$end will be $parameter*$count-1. we can see that this is true for 1,2 and also 3 as $parameters.
$start will be ($parameter-1)*$count . just quickly in the head that gives 0,50,100. just what we want.
as #cars10 suggested we do not need the $end. with array_slice($arr,($parameter-1)*$count,$count);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
$db_string = "|1||3||1||5||1||3||8||1||10||2|2||13||2||15|";
$array = array_filter(explode("|", $db_string));
if(in_array("1", $array)){
echo"<li>
Honeymoon Packages
</li>";
}
RESULT is Coming
* Honeymoon Packages
* Honeymoon Packages
* Honeymoon Packages
* Honeymoon Packages
because in array 1 is there 4 times, how do I group this in array and print result one time without duplicate?
I do prefer splitting with a regex, so no empty values will ever occur.
$db_string = "|1||3||1||5||1||3||8||1||10||2|2||13||2||15|";
$array = preg_split('/\|/', $db_string, 0, PREG_SPLIT_NO_EMPTY);
Now the array has an element for each number in the listed order. With the help of array map we use the empty $data array and build a key for unique element. So we know how many entries are there per item.
$data = [];
array_map(function($e) use (&$data) { isset($data[$e]) ? $data[$e]++ : $data[$e] = 1; }, $array);
Array
(
[1] => 4
[3] => 2
[5] => 1
[8] => 1
[10] => 1
[2] => 3
[13] => 1
[15] => 1
)
You can use the keys to have each item uniquely.
print_r(array_unique(array_keys($data)));
Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 8
[4] => 10
[5] => 2
[6] => 13
[7] => 15
)
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
The parameter for index 5 needs to be 0 in below code.
But The value for echo($a[5]) comes 1.2325951644078E-32.
The loop prints correct values for all other parameters except for 5th index (which needs to be 0 according to me).
Can anybody tell me why is that happening??
for($x=-2;$x<2.1;$x+=0.4){
$a[] = $x*$x;
}
echo($a[5]); //this is not printing 0 why?
Output is:-
Array
(
[0] => 4
[1] => 2.56
[2] => 1.44
[3] => 0.64
[4] => 0.16
[5] => 1.2325951644078E-32
[6] => 0.16
[7] => 0.64
[8] => 1.44
[9] => 2.56
[10] => 4
)
it should be zero..
Modify your code like this and try..
$a[] = number_format($x*$x,2);
Bro you forget to convert to number format before math.
for($x=-2;$x<2.1;$x+=0.4){
$a[] = number_format($x)*number_format($x);
}
echo($a[5]); // 0
Hope help this.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a table with locations stored in the format:
30°28'25"
68°28'43"
In the process of converting from DMS to DEC, I'd need a function to convert the string 68°28'43" (no spaces among values-symbols) into
$deg = 68
$min = 28
$sec = 43
Or something similar. Thanks
Simply use preg_match_all :)
the following regex simply captures every digits (\d) which are contains exactly 2 numbers ({2}) and group them (the braces around the expression)
<?php
$value = "68°28'43\"";
preg_match_all('/(\d{2})/', $value, $matches);
print_r($matches);
$matches would be an array with the captured results (the digits)
Array
(
[0] => Array
(
[0] => 68
[1] => 28
[2] => 43
)
[1] => Array
(
[0] => 68
[1] => 28
[2] => 43
)
)
And then you can simply assign it to your variables with the list() function as follow:
list($deg, $min, $sec) = $matches[0];
echo $deg; // 68
echo $min; // 28
echo $sec; // 43
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an array
Array
(
[0] => 34
[1] => 04:32 PM
[2] => 05:32 PM
[3] => MNL | ITLY
[4] => 2h 10m
[5] => PHP 9,222
[6] => 33
[7] => 04:32 PM
[8] => 04:32 PM
[9] => ITLY | MNL
[10] => 2h 10m
[11] => PHP 7,227
)
how can i perform arithmetic operation on value of index 5 and 11.
By using preg_replace you can strip the 'PHP' part and the comma from value.
echo preg_replace('| PHP (\d+),(\d+)|', '$1$2', $array[5]) + preg_replace('| PHP (\d+),(\d+)|', '$1$2', $array[11]);
or if the number of spaces is random '\s' is any whitespace char
echo preg_replace('|\s+PHP\s+(\d+),(\d+)|', '$1$2', $array[5]) + preg_replace('|\s+PHP\s+(\d+),(\d+)|', '$1$2', $array[11]);
or str_replace()
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I believe there is something like 67 million combination of two character strings using only the letters of the alphabet.
I basically want an array in PHP containing something like the following
Array
(
[1] => AA
[2] => AB
[3] => AC
[4] => AD
[5] => AE
[6] => AF
[7] => AG
[8] => AH
[9] => AI
[10] => AJ
.... and so on
)
with two loop you can do this:
<?php
$arr = array('A', 'B', 'C');
foreach ($arr as $value1) {
foreach ($arr as $value2) {
echo $value1.$value2;
}
}
?>
Then you can put the result in an array.