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.
Related
How would one get the integer value of the n-th value from n?
This is hard to phrase so I'll just use English. If I wanted the 3rd integer value from 1...
1 = 1
2 = 3
3 = 4 <- (looking to get 4 using 3)
4 = 8
5 = 16 <- (or 16 using 5)
...
I could just do a lookup table, but I'm sure there's a better solution.
$bitvalue = 5;
$intvalue = 2 ** ($bitvalue - 1);
// gives 16
echo $intvalue;
The ** operator is the power operator. So I'm using powers of 2.
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!
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";
}
Imagine that squares in a TicTacToe grid are numbered in a linear fashion from 1 to 9. A player puts an X on the grid by calling a class method:
$game->putX(1, 1); (the method accepts only integers from 0 to 2).
How do I calculate the linear value of the field where X was placed (here the linear value is 5)?
Your help will be much appreciated.
It's actually just x*3 + y+1. Assuming the games state is saved in an array (indexed 1-9, according to your question), your code could look like this:
// the board: examples:
// x 0 1 2 0 0 -> 1
// y 1 1 -> 5
// 0 1 2 3 2 2 -> 9
// 1 4 5 6
// 2 7 8 9
putX ($x, $y) {
$this->state[$x*3+$y+1] = 'X';
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Understanding PHP's & operator
I was just looking at array_filter() function doc and they had the following code to return odd numbers...
<?php
function odd($var)
{
// returns whether the input integer is odd
return($var & 1);
}
?>
Why does $var & 1 return odd number? how does that work?
& is bitwise and. It acts as a mask on the bits of $var. All odd numbers end with 1
no bit &1
1 001 1
2 010 0
3 011 1
4 100 0
5 101 1
6 110 0
7 111 1
You are using a bitwise function with always returns 1 when anded with an odd number.
A few examples:
11 = 3
01 = 1
----
01 = odd -- return 1 (true)
100 = 4
01 = 1
-----
000 = even -- return 0 (false)
One more:
10101 = 21
01 = 1
-------
00001 = odd -- return 1 (true)
That function return 1 if var is an odd number, 0 otherwise. "&" is the AND binary operator, so it considers the last binary digit of a number.
For example:
5 in binary is 101 -> 101 & 1 = 1 -> odd number.
8 in binary is 1000 -> 1000 & 1 = 0 -> even number.