How to get top 3 values from array in PHP [duplicate] - php

This question already has answers here:
Returning first x items from array
(5 answers)
How to remove duplicate values from an array in PHP
(26 answers)
Getting first 3 values of an array in PHP
(3 answers)
Get the first N elements of an array?
(5 answers)
Closed 2 years ago.
I have a sorted array in descending order such as with values 100 , 98, 96, 90 ....
and I am using foreach() loop to iterate over the array and use if condition with limit 3 such as `
foreach($array as $arr){
if(loop_counter<3)
{
echo 'something';
}}
to get top 3 positions. but the problem is that if there exist two same values such as 100 , 98, 98, 96, 90 . . then limits should increase from 3 to 4 so that on position 2 there exist two values 98, 98 and position 3 contain value 90 instead of 2nd 98.
thanks in advance

Related

How to get numbers in range between 001 to 012 in PHP [duplicate]

This question already has answers here:
Zero-pad digits in string
(5 answers)
Closed last year.
I am trying to get the value between 001 to 012 with range function but it's returning 1,2,3....12
range(001,012)
but i want value like this 001,002,003,,,012
Need to loop through and append them:
foreach (range(1, 12) as $number){
$numbers[] = sprintf("%03d", $number);
}
print_r($numbers);

PHP: What is the result of $a+++$a++ [duplicate]

This question already has answers here:
Pre-incrementation vs. post-incrementation
(3 answers)
Closed 2 years ago.
php > $a = 4;
php > echo $a+++$a++;
php > 9
Why is the result equal to 9 not 8 ?
a++ will increment the value of $a, but return the original value that I held before being incremented. So $a++ return 4 and $a return 4, the result should be 8 = 4 + 4 ?
The first time you get a return value from $a++ the value will be 4. But the second time it will be 5, as you already incremented it just before:
9 = 4 + 5
The final value of $a is 6, as it has been incremented twice.

Print numbers as words? [duplicate]

This question already has answers here:
PHP: Express Number in Words [duplicate]
(4 answers)
Closed 8 years ago.
Hi I was wondering how can I print out numbers in their word form ? When I did a Google search it showed other peoples script on how to print 234 as two hundred thirty four .
I need 234 as two three four.
Thank you guys!
I need 234 as two three four.
It would be as simple as creating an array of your number to word map, then taking your number and printing out the corresponding value:
<?php
$num_word = array();
$num_word[0] = 'zero';
$num_word[1] = 'one';
$num_word[2] = 'two'
...
$num_word[9] = 'nine';
$num = 234;
foreach(str_split($num) as $w) {
echo $num_word[$w];
}
?>

Check to see if database contains array values

I am new to writing server code, anyway I have a SQL DB that contains a list of numbers, I am wanting to check to see if an array that contains a list of numbers has any overlap with the DB.
Database:
ID Number
1 3
2 5
3 7
4 11
5 13
6 19
For example, in PHP/psuedocode:
$numbers = $_REQUEST['NUMBERS'] // array of numbers i.e. [3, 7, 20, 54]
This is what I'm looking for:
echo json_encode($result) // returns [3, 7]
Just do a query selecting the rows containing the numbers from the request:
$numbers = implode(',', $_REQUEST['NUMBERS']);
$query = "SELECT Number FROM TableName WHERE Number IN ($numbers)";

Number of indexes in an Array [duplicate]

This question already has answers here:
php - Is it possible to count the number of keys in an array?
(4 answers)
Closed 8 years ago.
I wish to count the number of indexes my new array have.
Below is a screenshot of my code. The values within the violet box is my output. My $High array lists down the values from my accu.php and new.php (within the require).
Now, want to list the unique values of my $High array using my new array, named $result. Then, I want to count the indexes of my $result array. I have tried the count() and sizeof() function and it still displays 9 as seen below.
Below is my code:
<?php
require("accu.php");
require("new.php");
$High = array ($Accu_High1,$Accu_High2,$Accu_High3,$Accu_High4,$Accu_High5,$new1,$new2,$new3,$new4,$new5,$new6,$new7,$new8,$new9,$new0);
$result = array_unique($High); // remove similar
$count = sizeof($result); //count
for ($int = 0; $int<=14; $int++)
{
echo "<b>$int</b> $High[$int]<br>";
}
echo "<br><br><br><b>$count</b>";
?>
And below is my output:
0 22
1 32
2 33
3 32
4 32
5 30
6 31
7 31
8 31
9 30
10 23
11 30
12 31
13 30
14 30
9
Array indexes are unique. So count($array) is all you need to count the 'indexes'.
But, I'm not sure this question display minimal understanding of PHP arrays. The PHP online manual or a book are the answer here. Like how foreach works and such trivial matters.
So use var_dump against your array and analyze the output. Then see what's actually going on. There's many array functions/functionality and you should know them.

Categories