I have a two dimensional array with unknown number of elements.
$two_darray[row][column]; //there will be an unknown integer values instead of row and column keywords
If I were to write a for loop as follows, how can I determine how many rows and columns in my $two_darray. Can you please tell me if there is a library function in php that can tell me the value inside [????] [????]
for($row=0; $row<………; $row++)
{
for($column =0; $column <………; $ column ++)
{
echo $two_darray[$row][$column];
}
echo “\n end of one column \n”;
}
I really need to know the value of rows and columns in order to perform other calculations.
foreach ($two_darray as $key => $row) {
foreach ($row as $key2 => $val) {
...
}
}
No need to worry about how many elements are in each array, as foreach() will take care of it for you. If you absolutely refuse to use foreach, then just count() each array as it comes up.
$rows = count($two_d_array);
for ($row = 0; $row < $rows; $row++) {
$cols = count($two_darray[$row]);
for($col = 0; $col < $cols; $col++ ) {
...
}
}
This is what i do:
My superheroes' array:
$superArray[0][0] = "DeadPool";
$superArray[1][0] = "Spiderman";
$superArray[1][1] = "Ironman";
$superArray[1][2] = "Wolverine";
$superArray[1][3] = "Batman";
Get size :
echo count( $superArray ); // Print out Number of rows = 2
echo count( $superArray[0] ); // Print Number of columns in $superArray[0] = 1
echo count( $superArray[1] ); // Print Number of columns in $superArray[1] = 4
php
For a php Multidimensional Array, Use
$rowSize = count( $arrayName );
$columnSize = max( array_map('count', $arrayName) );
If you need to know an actual number, then you can use the sizeof() or count() functions to determine the size of each array element.
$rows = count($two_darray) // This will get you the number of rows
foreach ($two_darray as $row => $column)
{
$cols = count($row);
}
the quick way for normal,non-mixed 2-dim Array,
$Rows=count($array);
$colomns=(count($array,1)-count($array))/count($array);
for a php indexed two-dimensional array:
$arName = array(
array(10,11,12,13),
array(20,21,22,23),
array(30,31,32,33)
);
$col_size=count($arName[$index=0]);
for($row=0; $row<count($arName); $row++)
{
for($col=0; $col<$col_size; $col++)
{
echo $arName[$row][$col]. " ";
}
echo "<br>";
}
Output:
10 11 12 13
20 21 22 23
30 31 32 33
Related
Is there a function which can be used to get the number of columns of a 2D array?
I've come to realize that count() function will display the number of rows inside the 2D array but am interested in getting the number of columns inside each array.
How can I use the count() function or any other function to get the number of elements inside an array contained inside another array.
Here is a sample of the code that am working with:
<?php
$people = array(
array("Rodrick","Java","PHP"),
array("Jane","Python","Javascript"),
array("Tom","Python","R"),
array("Wangari","Ruby","Kotlin"),
);
for($row = 0 ; $row < count($people) ; $row++){
echo "The Programmers ".$row;
echo "<ol>";
for($col = 0 ; $col < 3 ;$col++){
echo "<li>".$people[$row][$col]."</li>";
}
echo "</ol>";
}
You can get the size of the second dimension by:
$ncols = count($people[$row]);
Your code could be like this:
<?php
$people = array(
array("Rodrick","Java","PHP"),
array("Jane","Python","Javascript"),
array("Tom","Python","R"),
array("Wangari","Ruby","Kotlin"),
);
for($row = 0 ; $row < count($people) ; $row++){
echo "The Programmers ".$row;
echo "<ol>";
$ncols = count($people[$row]);
for($col = 0 ; $col < $ncols ; $col++){
echo "<li>".$people[$row][$col]."</li>";
}
echo "</ol>";
}
?>
Do you really need to know the size of nested array?
Here is solution with foreach used:
$people = array(
array("Rodrick","Java","PHP"),
array("Jane","Python","Javascript"),
array("Tom","Python","R"),
array("Wangari","Ruby","Kotlin"),
);
foreach($people as $human){
echo "<ol>";
foreach($human as $data){
echo "<li>".$data."</li>";
}
echo "</ol>";
}
The number of columns is determined here on the basis of the first line.
$numberOfColumns = count(reset($people));
The calculation can be used for numerical and also for associative arrays.
I want to initializing a 2 dimensions Array in PHP and I don't know How to declare a 2d array of size (1,N) in php with all values as Zeros?
$Orders_C = array(1,N);
I am not sure if this is correct syntax or not.
PHP doesnot have 2d array ,but instead it has array of array. You can use the code below to initialize as you said.
$Orders_C=array_fill(0,1, array_fill(0, N,0));
Here the array_fill first return an array filled with 0 from index 0 to N.And again the same array is filled to the new array upto index 1.Hence you will get an array within array.
Generic solution for any count of columns and rows could be:
$maxRows = 5;
$maxCols = 5;
$orders = [];
for ($col = 0; $col < $maxCols; $col++) {
for ($row = 0; $row < $maxRows; $row++) {
$orders[$col] = $row;
}
}
And because you want 2d array like (1, N) then you can simplify it
$orders = [];
for ($i = 0; $i < $maxRows; $i++) {
$orders[0][] = 0;
}
In a comma separated csv file, I want to count the number of rows only where the the first number is same.
Following is the example data, I want to get number of rows which start with 2 (2 rows), and the rows which start with 4 (3 rows). This is just an example, the numbers are random.:
2,0,0
2,1,0
4,0,0
4,3,0
4,4,0
I'm trying following code, I can count only all rows of the file but do not know how can I count only the rows which have same first number.
$i = 0;
while ($i < 5) { //fixed number of times
$i++;
$rows = 0;
$fp = fopen("test.csv", "r");
while (fgetcsv($fp)) { //don't want all rows
$rows++;
}
fclose($fp);
echo $rows;
}
Edit:
Sorry, I forgot to mention the numbers in above file are random, they are not always 2 or 4.
You will need an array to get statistics.
$i = 0;
$agg = []; // to count stats
while ($i < 5) {
$i++;
$rows = 0;
$fp = fopen("test.csv", "r");
while ($line = fgetcsv($fp)) {
$number = $line[0]; // get first number
if(isset($agg[$number])){ // if there is the number in stats
$agg[$number]++; // count new one
} else {
$agg[$number] = 1; // mark as found one
}
}
fclose($fp);
print_r($agg);
}
You could probably try something like this :
$i = 0;
while ($i < 5) { //fixed number of times
$i++;
$rows = array();
$fp = fopen("test.csv", "r");
while ($data = fgetcsv($fp)) {
$first = $data[0];
if(isset($rows[$first]) {
$rows[$first] += 1;
} else {
$rows[$first] = 1;
}
}
fclose($fp);
print_r($rows);
}
I didn't test my code, so it may contains errors.
After the execution, $rows will contain an associative array with the form 'first number' => 'count'
What I don't understand however is why you are doing it five times and also why you are not using a for loop for that instead of a while ?
Assuming you know how to get the contents of the text file, something like this should work:
$ar = explode("\n",$txtfileContents);
$counts = array();
foreach($ar as $row){
$counts[$row[0]]++;
}
$counts now contains an array of all first characters and how many times they appeared. You can now simply access them like this:
echo "'2' appeared ".$count['2']." times<br/>";
echo "'4' appeared ".$count['4']." times";
Step by step:
Split the text file into an array of individual rows:
$ar = explode("\n",$txtfileContents);
(Try alternatively file() here, which may be more reliable than explode)
Loop through it:
foreach($ar as $row){
}
Then check the first character inside the loop and count each character separately:
$counts[$row[0]]++;
I have a two dimensional array with unknown number of elements.
$two_darray[row][column]; //there will be an unknown integer values instead of row and column keywords
If I were to write a for loop as follows, how can I determine how many rows and columns in my $two_darray. Can you please tell me if there is a library function in php that can tell me the value inside [????] [????]
for($row=0; $row<………; $row++)
{
for($column =0; $column <………; $ column ++)
{
echo $two_darray[$row][$column];
}
echo “\n end of one column \n”;
}
I really need to know the value of rows and columns in order to perform other calculations.
foreach ($two_darray as $key => $row) {
foreach ($row as $key2 => $val) {
...
}
}
No need to worry about how many elements are in each array, as foreach() will take care of it for you. If you absolutely refuse to use foreach, then just count() each array as it comes up.
$rows = count($two_d_array);
for ($row = 0; $row < $rows; $row++) {
$cols = count($two_darray[$row]);
for($col = 0; $col < $cols; $col++ ) {
...
}
}
This is what i do:
My superheroes' array:
$superArray[0][0] = "DeadPool";
$superArray[1][0] = "Spiderman";
$superArray[1][1] = "Ironman";
$superArray[1][2] = "Wolverine";
$superArray[1][3] = "Batman";
Get size :
echo count( $superArray ); // Print out Number of rows = 2
echo count( $superArray[0] ); // Print Number of columns in $superArray[0] = 1
echo count( $superArray[1] ); // Print Number of columns in $superArray[1] = 4
php
For a php Multidimensional Array, Use
$rowSize = count( $arrayName );
$columnSize = max( array_map('count', $arrayName) );
If you need to know an actual number, then you can use the sizeof() or count() functions to determine the size of each array element.
$rows = count($two_darray) // This will get you the number of rows
foreach ($two_darray as $row => $column)
{
$cols = count($row);
}
the quick way for normal,non-mixed 2-dim Array,
$Rows=count($array);
$colomns=(count($array,1)-count($array))/count($array);
for a php indexed two-dimensional array:
$arName = array(
array(10,11,12,13),
array(20,21,22,23),
array(30,31,32,33)
);
$col_size=count($arName[$index=0]);
for($row=0; $row<count($arName); $row++)
{
for($col=0; $col<$col_size; $col++)
{
echo $arName[$row][$col]. " ";
}
echo "<br>";
}
Output:
10 11 12 13
20 21 22 23
30 31 32 33
This is driving me nuts! I need to fill up and HTML table with values that I pulled out from a DB.
The HTML table has 100 one td for each value.
I have an array that I populate doing this:
$x = 1;
$ArrayA = array();
for ($x = 1; $x <= 100; $x++) {
$ArrayA[$x] = 0;
}
So now I have an array with 100 values, right?
Then I query my DB, and get the result that I "want"
(select num from table1)
6 rows with the following values:
1,3,14,50,100.
Then I insert the values from my query into the $ArrayA that I populated before with one hundred 0's
$x = 1;
while ($row = mysql_fetch_array($result))
{
extract($row);
$ArrayA[$x] = "$num";
$x++;
}
Now $ArrayA has this:
1,3,14,17,100,0,0,0,0,0,0,0,0,0,0,0,0.....
And my goal is to replace with a 0 where I should have a "next" number, hard to explain..
I need this result:
1,0,3,0,0,0,0,0,0,0,0,0,0,14,0,0,17,0....
I tried to use PHP array_splice, but it did not work.
I tried with some if statements but my logic and programing experience is not that good.
Any suggestions?
Thanks
If i understand this right, you want to add a zero after every element returned by your query? Right? If so, why not add a zero right after you add an element into the array?
$x = 1; while ($row = mysql_fetch_array($result)) {
extract($row);
$ArrayA[$x] = "$num";
$x++;
$ArrayA[$x] = "0"; //adds zero after every $num insert
}
Maybe you can clarify if this isn't what you're asking...
$match_rows = mysql_fetch_array($result);
$rows = range(1,100);
foreach( $rows as $row){
echo '<td>';
if( in_array($row, $match_rows) ){
echo $row;
}else{
echo 0;
}
echo '</td>' . PHP_EOL;
}
Sorry, that is untested - it could be shorter or neater, but like that possibly illustrates another way of achieving what you want.
I think this is what you want for your second code block:
while ($row = mysql_fetch_array($result))
{
extract($row);
if($num > 0 && $ <= 100 )
{
$ArrayA[$num] = "$num";
}
}
If your DB values are 3, 10, 15, 22, 100, you'd end up with 0,0,3,0,0,0,0,0,0,10,0,0,0,0,15, etc etc.