I have many strings looking like this:
6 (39), 10 (44), 11 (45), 11½ (45.5), 12 (46)
6 (39), 7 (40.5), 8 (42), 8½ (42.5), 9 (43), 10 (44.5), 11 (46)
6 (39), 7 (40.5), 8 (42), 8½ (42.5), 9 (43), 11 (46)
I got these results with this code:
<?PHP
$rscat = mysql_query("SELECT `Sizes` FROM `products` WHERE `Category`='$Cat'");
while($rowscat = mysql_fetch_array($rscat))
{
$CatSizes = $rowscat['Sizes'];
echo "$CatSizes <br>";
}
?>
What I want: as you can see in the last example the string 6 (39) is repeated exactly three times, the string 7 (40.5) is repeated exactly two times.
So I want a result like this:
6 (39) - (3)
7 (40.5) - (2)
Of course I do not need that just for 7 (41) and 10 (44); I need to find all strings which are repeating and display them in just one row and aside to show how many times they are repeated.
I hope you understand me well.
Thanks in advance!
Advance warning: OP edited their post several times, so there are multiple answers below. I'm leaving them all intact in case others find them helpful.
Original answer
You can use the array_count_values() function for exactly this: it returns a new array with each repeated value as the array key, and the number of times that value appears as the array value. Using your original example, you'd need something like this:
$input = <<<EOT
7 (41)
8 (42)
9 (43)
10 (44)
11 (45)
6 (39)
7 (41)
EOT;
$split = explode("\n", $input);
$counted = array_count_values($split);
foreach($counted as $value => $count) {
echo "$value - ($count)\n";
}
Note: I trimmed the number of strings going into $input for conciseness, but you get the point. Output from that script:
7 (41) - (8)
8 (42) - (6)
9 (43) - (6)
10 (44) - (6)
11 (45) - (7)
8½ (42.5) - (1)
12 (46) - (1)
6 (39) - (3)
You might find the PHP documentation for array_count_values() helpful reading.
Update #1
OP edited their post, rendering my original answer incorrect. Using their edited version, the correct code is this:
$input = "6 (39), 10 (44), 11 (45), 11½ (45.5), 12 (46), 6 (39), 7 (40.5), 8 (42), 8½ (42.5), 9 (43), 10 (44.5), 11 (46), 6 (39), 7 (40.5), 8 (42), 8½ (42.5), 9 (43), 11 (46)";
$split = explode(", ", $input);
$counted = array_count_values($split);
foreach($counted as $value => $count) {
echo "$value - ($count)\n";
}
WARNING: Make sure the items are all separated by commas, not by a mix of new lines and commas. OP: you should choose new lines as in your original version, or choose commas, but don't mix the two if you want this code to work.
Update #2
OP has asked to modify the solution so it works directly with their SQL query. This is tricky because I don't know exactly what data is coming out, but based on their previous edits the answer is likely to look something like this:
$rscat = mysql_query("SELECT `Sizes` FROM `products` WHERE `Category`='$Cat'");
$arrayOfSizes = [];
while($rowscat = mysql_fetch_array($rscat)) {
$arrayOfSizes[] = $rowscat['Sizes'];
}
$counted = array_count_values($arrayOfSizes);
foreach($counted as $value => $count) {
echo "$value - ($count)\n";
}
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
for($i=0;$i<=15120;$i+=504){
echo ($i/60/8.4) . " - " .floor($i/60/8.4)."<br>";
}
Result (i make ** at problem) :
0 - 0
1 - 1
2 - 2
3 - 3
4 - 4
5 - 5
6 - 6
7 - 6
8 - 8
9 - 8
10 - 10
11 - 11
12 - 12
13 - 13
14 - 13
15 - 15
16 - 16
17 - 17
18 - 17
etc...
At start i think i apply "Floo" at all calculation but (for line "7-6") also $i = 3528 :
3528 / 60 = 58,8 == Floor ==> 58 / 8 = 7.25
floor of 7.25 =/= 6
it because of PHPs floating point precision
so your 8th calculation for example will not return 7, but something like 6.9999999999999999999999999999
as this is a double, it will be rounded to 7 on output, try this out:
$x = 6.9999999999999999999999999999999999999999999999999999999;
echo $x;
when you use floor() it will (correctly) round it down to 6
This question already has answers here:
Best way to round down in PHP
(3 answers)
Round up to nearest multiple of five in PHP
(12 answers)
Closed 4 years ago.
How can I round an integer down to the nearest multiple of 3 using PHP? And have <3 be 0.
For example:
4 becomes 3
10 becomes 9
9 becomes 9
8 becomes 6
And so on...
Assuming $x is your input:
print $x-$x%3;
Is this what you looking for.
/**
* Round an integer down to the nearest multiple of the given multiple
* #param integer $number
* #param integer $multiple
* #return integer
*/
function round_down_to_multiple_of( int $number, int $multiple = 3 ): int
{
return (int)($multiple * floor( $number/$multiple ));
}
# TESTS
$numbers = [ 10, 9, 8, 1, 0 ];
foreach( $numbers as $number ){
printf( '%d became %d'.PHP_EOL, $number, round_down_to_multiple_of( $number, 3 ) );
}
After running the above test I get the following results:
10 became 9
9 became 9
8 became 6
1 became 0
0 became 0
I know there are good answers here but this one is for larger numbers for the sake of alternative, using bcmath.
function floor_to_multiple($number, $multiplier) {
return bcsub($number, bcmod($number, $multiplier));
}
If you want for 3, you may use function below:
function round_nearest_3($value){
return $value-$value%3;
}
If you want to return function for any value use function below:
function round_nearest_mod($value,$mod){
return $value-$value%$mod;
}
Examples:
echo round_nearest_3(4); // becomes 3
echo round_nearest_3(10); // becomes 9
echo round_nearest_3(9); // becomes 9
echo round_nearest_3(8); // becomes 6
echo round_nearest_mod(4,3); // becomes 3
echo round_nearest_mod(10,3); // becomes 9
echo round_nearest_mod(9,3); // becomes 9
echo round_nearest_mod(8,3); // becomes 6
<?php
//requested number
$num = 10;
//calc
for($i=1;($i*3)<=$num;$i++)$answer[] = $i;
$answer = max($answer)*3;
//print result
print_r($answer);
I misread that even if it is a perfect fit, it should round down to the last preceeding incremental:
4 becomes 3
10 becomes 9
9 becomes 6
8 becomes 6
Therefore, if for some reason you need this; your answer would be:
print $x-($x-1)%3-1;
I made a mistake in comprehending the question but thought this answer was curious enough to be worth posting.
Thanks in advance.
Im uploading a .csv file through php 5.5 script that converts it from Unicode to UTF-8 and finally saves it on a folder.
Then i read this file with PHPExcel_IOFactory and capture some data to an array. So far so good. File uploads and saves ok, but something may be messing the data in the conversion process, because im getting the text strings in the array filled with blank spaces like this:
array(20) {
["A"]=>
string(39) "
l : 1 7 0 4 8 0 8 8 8 6 4 9 2 3 5 9 "
["B"]=>
string(51) " 2 0 1 7 - 0 8 - 0 9 T 0 0 : 2 1 : 5 7 + 0 2 : 0 0 "
["C"]=>
string(41) " a g : 2 3 8 4 2 6 1 5 5 8 3 3 9 0 2 5 8 "
["D"]=>
bool(false)
["E"]=>
string(41) " a s: 2 3 8 4 2 6 1 5 5 8 3 3 7 0 2 5 8"
["F"]=>
string(29) " D E S A R R O L L O W E B "
Maybe its because Delimiters. Opening .csv on sublime reveals two 'white spaces' as delimiters, so if i pass something like this :
$objReader->setDelimiter(' ');
It works and reads data, but filled with empty spaces. Some tip about how to get clean data from file?
NOTE: Using WAMP its working ok converting the file like this:
$conversion = iconv(mb_detect_encoding($conversion, mb_detect_order(),
true), "UTF-8", $conversion);
In production environment not working at all with that conversion (File saves empty)
Finally sorted out the problem. For anyone using PHPExcel and having same issue on character codification, if you really need to save the .xls file on a different character set, try something like this on the conversion:
$objReader->setDelimiter("\t");
$inputFileName = array_shift($inputFileNames);
$conversion = file_get_contents($inputFileName);
$conversion = iconv("WINDOWS-1252", "UTF-8", $conversion);
Thanks!
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.
I'm looking for a function to dump variables and objects, with human readable explanations of their data types. For instance, in php var_dump does this.
$foo = array();
$foo[] = 1;
$foo['moo'] = 2;
var_dump($foo);
Yields:
array(2) {
[0]=>
int(1)
["moo"]=>
int(2)
}
A few examples:
foo <- data.frame(1:12,12:1)
foo ## What's inside?
dput(foo) ## Details on the structure, names, and class
str(foo) ## Gives you a quick look at the variable structure
Output on screen:
foo <- data.frame(1:12,12:1)
foo
X1.12 X12.1
1 1 12
2 2 11
3 3 10
4 4 9
5 5 8
6 6 7
7 7 6
8 8 5
9 9 4
10 10 3
11 11 2
12 12 1
> dput(foo)
structure(list(X1.12 = 1:12, X12.1 = c(12L, 11L, 10L, 9L, 8L,
7L, 6L, 5L, 4L, 3L, 2L, 1L)), .Names = c("X1.12", "X12.1"), row.names = c(NA,
-12L), class = "data.frame")
> str(foo)
'data.frame': 12 obs. of 2 variables:
$ X1.12: int 1 2 3 4 5 6 7 8 9 10 ...
$ X12.1: int 12 11 10 9 8 7 6 5 4 3 ...
Check out the dump command:
> x <- c(8,6,7,5,3,0,9)
> dump("x", "")
x <-
c(8, 6, 7, 5, 3, 0, 9)
I think you want 'str' which tells you the structure of an r object.
Try deparse, for example:
> deparse(1:3)
[1] "1:3"
> deparse(c(5,6))
[1] "c(5, 6)"
> deparse(data.frame(name=c('jack', 'mike')))
[1] "structure(list(name = structure(1:2, .Label = c(\"jack\", \"mike\""
[2] "), class = \"factor\")), .Names = \"name\", row.names = c(NA, -2L"
[3] "), class = \"data.frame\")"
It's better than dump, because dump requires a variable name, and it creates a dump file.
If you don't want to print it directly, but for example put it inside a string with sprintf(fmt, ...) or a variable to use later, then it's better than dput, because dput prints directly.
print is probably the easiest function to use out of the box; most classes provide a customised print. They might not specifically name the type, but will often provide a distinctive form.
Otherwise, you might be able to write custom code to use the class and datatype functions to retrieve the information you want.