two dimension array with assoc and for loop php - php

I write that code but i got error
$tablica = [
'xd'=>['jabłka', 'wiśnie', 'gruszki'],
'xd'=>['jabłska', 'wisssśnie', 'grussssszki']
];
$n=count($tablica);
echo "<table border=1>";
for($i = 0; $i <count($tablica); ++$i) {
echo "<tr>";
for($x = 0; $x < count($tablica[$i]); $x++){
echo "<td>".$tablica[$i][$x]."</td><br>";
}
echo "</tr>";
}
echo "</table>";
it's simple but when i add assoc for 1st dimension it's broke with that error
Notice: Undefined offset: 0 in C:\wamp64\www\projects\xd\1.php on line 10
Warning: count(): Parameter must be an array or an object that implements Countable in C:\wamp64\www\projects\xd\1.php on line 10

You are adding complexity when there is no need to.
When using associative arrays, foreach is usually a best way to iterate.
You can replace your code with:
$tablica = [
'xd'=>['jabłka', 'wiśnie', 'gruszki'],
'xd'=>['jabłska', 'wisssśnie', 'grussssszki']
];
echo "<table border=1>";
foreach($tablica as $tr) {
echo "<tr>";
foreach($tr as $td) {
echo "<td>".$td."</td>";
}
echo "</tr>";
}
echo "</table>";
PS: you have duplicated key named xd. Then PHP will only get the last one.

If you want to stick with using for() loops, then the problem is that your array has xd and xd1 as the keys, but your trying to access them with $i which is 0 and 1. What you can do is create an array of the keys (I've set $keys using array_keys()) and then index that using $keys[$i]...
$tablica = [
'xd'=>['jabłka', 'wiśnie', 'gruszki'],
'xd1'=>['jabłska', 'wisssśnie', 'grussssszki']
];
$n=count($tablica);
$keys = array_keys($tablica);
echo "<table border=1>";
for($i = 0; $i < $n; ++$i) {
echo "<tr>";
for($x = 0; $x < count($tablica[$keys[$i]]); $x++){
echo "<td>".$tablica[$keys[$i]][$x]."</td><br>";
}
echo "</tr>";
}
echo "</table>";

Related

max() function can't find correct value. php

I'm trying to get the max value in the array.
But using max() for the entire array it can't find the highest value:
The return Key is 2, which is not the correct key, obviously the highest value is in array(3).
Please help me to find my mistake.
Here bellow is my code:
<?php
$hex_splited = [
['00','00','00'],
['10', '11', '10'],
['F0', '1A', 'C3'],
['0F', 'FE', 'F4'],
];
$arr_rgb = [];
$count2 = count($hex_splited);
for ($i=0; $i < $count2; $i++) {
$inn_count = count($hex_splited[$i]);
for ($j=0; $j < $inn_count; $j++) {
$val = hexdec($hex_splited[$i][$j]);
$arr_rgb[$i][$j] = $val;
}
}
$max = max($arr_rgb);
var_dump($max);
$key = array_search($max, $arr_rgb);
echo "<pre>";
echo "array key is: " . $key;
echo "</pre>";
echo "<table border='1'>";
for ($m=0; $m < $count2; $m++) {
echo "<tr>";
for ($k=0; $k < $inn_count; $k++) {
echo "<td>";
echo $arr_rgb[$m][$k];
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
The problem is that max() doesn't always behave as you would expect when using multidimensional arrays.
This code does the processing in two stages, first finds the max value. As this is only a 2 dimensional simple array, you can use...
$max = max(array_map("max", $hex_splited));
This applies max to each of the sub arrays and then gets the max of those values.
So in your case, this will be 'FE'. Then as part of the loop which converts the data to decimal, it compares the original value with the max already found and stores the key...
$key = 0;
for ($i=0; $i < $count2; $i++) {
$inn_count = count($hex_splited[$i]);
for ($j=0; $j < $inn_count; $j++) {
$val = hexdec($hex_splited[$i][$j]);
$arr_rgb[$i][$j] = $val;
// Check if found the max value
if ( $max == $hex_splited[$i][$j] ) {
$key = $i;
}
}
}

How to pull in an array to a PHP MySQL output?

I am trying to pull in this date for each part of my database, but I am missing some element to make it work and not sure what I am missing?
$modified[] = array(date("m/d/y", strtotime($row[audit_modify_date]))."<br />".date("g:i a", strtotime($row[audit_modify_date])),);
//query
for ($i=0; $i < count($result); $i++) {
echo "<tr>";
echo "<td>$modified</td>";
echo"<tr>";
}
They output just says Array.
You are printing the array, not the elements in the array
echo "<td>$modified</td>";
either
for ($i=0; $i < count($result); $i++) {
echo "<tr>";
echo "<td>".$modified[$i]."</td>";
echo"<tr>";
}
or
foreach ($modified as $m) {
echo $m;
}
edit actually what? I don't understand what you're doing
Since you are using $modified[ ] = array(....), it becomes multidimensional array.
Use below code
for ($i=0; $i < count($result); $i++) {
echo "<tr>";
echo "<td>$modified[0][$i]</td>";
echo"<tr>";
}
Or just
echo "<td>$modified[0][0]</td>";
Other thing is instead of creating a array, make it a variable like shown below
$modified=date("m/d/y", strtotime($row[audit_modify_date]))."<br />".date("g:i a", strtotime($row[audit_modify_date]));
//query
for ($i=0; $i < count($result); $i++) {
echo "<tr>";
echo "<td>$modified</td>";
echo"<tr>";
}
Note: use the appropriate according to your requirement.

Loop through a PHP Multidimensional Array

Im trying to create a 2D array with a specific number of rows and cols which I have stored as the variable $n ; so if $n was 5, I would have 5 rows and 5 cols, all with random numbers. I have created a for loop (as shown below) that generates the correct amount of rows but I cannot figure out how to do the same with the columns at the same time. The code I have at the moment is shown below.
<?php
$n = 3;
for($i=0; $i<=$n; $i++) {
$value[$i][0] = rand(1,20);
$value[$i][1] = rand(1,20);
$value[$i][2] = rand(1,20);
$value[$i][3] = rand(1,20);
}
print "<table>";
for($j=0; $j<$n; $j++) { // Runs the loop times $n
print "<tr>";
for($k=0; $k<$n; $k++) { // Runs the loop times $n
print "<td>" . $value[$j][$k] . "</td>";
}
print "</tr>";
}
print "</table>";
?>
Any help would be appreciated in learning to create this loop of the array. Thanks in advance.
You need a second loop inside the first loop:
for($i=0; $i<=$n; $i++) { // rows
for($j=0; $j<=$n; $j++) { // columns
$value[$i][$j] = rand(1,20);
}
}
Try this instead :
$n = 3;
for($i=0; $i<=$n; $i++) {
$value[$i][0] = rand(1,20);
$value[$i][1] = rand(1,20);
$value[$i][2] = rand(1,20);
$value[$i][3] = rand(1,20);
}
print "<table>";
foreach($value as $row){
print "<tr>";
foreach($row as $cell){
print "<td>" . $cell . "</td>";
}
print "</tr>";
}
print "</table>";
And using a for loop :
print "<table>";
for($i=0; $i<=$n; $i++) { // rows
print "<tr>";
for($j=0; $j<=(count($value[$i])-1); $j++) { // columns
echo "<td>".$value[$i][$j]."</td>";
}
print "</tr>";
}
print "</table>";

Php Undefined Offset Error While Printing Table

I'm tried to call the variable but I get this error Notice: Undefined offset: 6 in /Applications/XAMPP/xamppfiles/htdocs/databasetwo/untitled.php on line 113
This code print a table to Html I want to do it programmatically so the array can print the amount selected.
$id = "table" ;
// echo "<table id=$id><tr><th>Title</th><th>Price</th><th>Number</th></tr>";
echo "<table id=$id>";
foreach($rows as $row){
echo "<tr>";
for ($i = 0 ; $i <= $amount ; $i++) {
echo "<td>{$row[$i]}</td>";
/*echo "<td>{$row[1]}</td>";
echo "<td>{$row[2]}</td>";
echo "<td>{$row[3]}</td>";
echo "<td>{$row[4]}</td>";
echo "<td>{$row[5]}</td>";
echo "<td>{$row[6]}</td>"; */
}
echo "</tr>";
}
echo "</table>";
If I leave it $row[0], 1, 2 etc. Will Work. The error I'm getting is Notice: Undefined offset: 6 in /Applications/XAMPP/xamppfiles/htdocs/databasetwo/untitled.php on line 113
If you are starting from 0, make sure for ending criteria you add value - 1.
for ($i = 0 ; $i <= $amount - 1 ; $i++) {
Or remove = in loop like this,
for ($i = 0 ; $i < $amount ; $i++) {

PHP POST array to table

I new to PHP. I'm trying to make an Incidence of Coincidence (IC) calculator from bits and pieces of code that i've patched together from other peoples work. It's all working fine except for the part where it is posted to table. I want it to stop making new cells after 50.
I've been looking at it for hours and have tried several other methods but i've confused myself. Any help please?
Here's the code that's writes the table:
<?php
if ( $showTable ) {
// display tabulated values
echo "<table class=\"trans\" width=\"1000\" border=\"1\" cellpadding=\"0\">";
for ( $i = 1; $i < 50; $i++ ) {
echo "<tr>";
for ($c=0; $c <$cols; $c++)
{
echo "<td>";
echo($cells.': '.round($history[$i+$c], 3));
$cells++;
echo "</td>";
}
}
echo "</tr>";
$i += $c;
}
echo "</table>";
?>
If I understood you correctly,
<?php
if ( $showTable ) {
// display tabulated values
echo "<table class=\"trans\" width=\"1000\" border=\"1\" cellpadding=\"0\">";
for ( $i = 1; $i < 50; $i++ ) { // 50 rows will be created
echo "<tr>";
for ($c=0; $c <50; $c++) // each row will contain 50 <td>
{
echo "<td>";
echo($cells.': '.round($history[$i+$c], 3));
$cells++;
echo "</td>";
$i += $c;
}
echo "</tr>";
}
}
echo "</table>";
?>

Categories