I tried to create a table that calculating the data which stored in mutli-dimensional arrays' value and show the result by table format.
I don't know how to count the values which stored in the array's array(third level array's value.
Can I using the function to count values in the mutli-dimensional arrays' value? Hope anyone can teach me, thanks.
<?php
$itemList = array
("book"=>array("ID"=>"14567",
"name"=>array("History"=>array("American"=>12,"Europe"=>2), "SF"=>array("Space"=>32), "Chinese"=>array("kungFu"=>10))),
"stationary"=>array("ID"=>"24547", "name"=>array("HB"=>array("ABC"=>123, "MC"=>161,"GCD"=>26)))
);
$item = "<table border=1>";
$item .= "<td>item count(s)</td>";
foreach($itemList as $key => $value){
$item .= "<tr>";
$item .= "<td align=center>" .
/*count the categories of the items(e.g. American,Europe,Space,Kungfu show
the result: "4")*/
. "</td>";
$item .= "</tr>";
}
$item .= "</table>";
echo $item;
?>
foreach($itemList['book']['name'] as $key => $value)
{
foreach ($itemList['book']['name'][$key] as $key1 => $value1) {
$category[] = $key1;
$value2[] = $value1;
}
}
echo count($category);
here you got the total numebr of count of your category
Related
I have a csv file that should be converted to HTML table based on column values , and I have to put the values into separate table based on first column of csv . And I think the problem is because of '\n' in first column.
So its like this:
Here in Result column, in one row I have three values separated using comma (W,M,P). In the code I wrote it is considered as separate table headers .
Can anyone please help me with this?
This is my code:
<?php
$csv="FinalResult.csv" ;
$csvcontents=file_get_contents($csv);
$csv_array = explode("\n", $csvcontents);
$tables = [];
foreach($csv_array as $key => $value) {
if ($key == 0) {
continue;
}
$line = explode(',', $value);
if (array_key_exists($line[0], $tables)) {
$tables[$line[0]][] = $line;
} else {
$tables[$line[0]] = [$line];
}
}
foreach ($tables as $key => $value) {
echo '<h1> ' .$key. ' </h1>'; // YOUR TITLE (Team)
echo "<table>";
echo '<tr>';
foreach (explode(',', $csv_array[0]) as $keyHeader => $valueHeader) {
if (in_array($keyHeader, [0, 1])) {
continue;
}
echo "<th>$valueHeader</th>";
}
echo '</tr>';
foreach ($value as $keyRow => $valueRow) {
echo '<tr>';
foreach ($valueRow as $keyValue => $valueValue) {
if (in_array($keyValue, [0, 1])) {
continue;
}
echo "<td>$valueValue</td>";
}
echo '</tr>';
}
echo '</table>';
}
?>
I refereed in stack overflow , but there they were giving only single values to a column and not providing multiple values .
But am getting output like this ,
so it takes the value of Result column after '-' as a new heading , i tried but am not able to solve this , can anyone really help me in this matter .
Here is how my output should look like:
I marked in yellow where all the data am getting in same column
This is my csv file :
Team,Date,Opponent,Result
MIN,May-03,UTA,a.b.c=d-e.f-g.h=log4j2-i.xml-j -k -a4j.k=tp_r-RR.xml -
MIN,May-04,SEA,"L,Q,J"
SAC,May-03,DAL,L
SAC,May-04,TOR,W
NYN,May-05,BAL,L
NYN,May-07,MIA,W
Here is the code
<table>
<?php
$csvValues= array_map('str_getcsv', file('FinalResult.csv'));
$counter = 0;
// Header
echo "<tr>";
foreach($csvValues[0] as $headers){
echo "<th>".$headers."</th>";
}
echo "</tr>";
// Content
foreach($csvValues as $values){
echo "<tr>";
if($counter >0){
foreach($values as $data){
echo "<td>".$data."</td>";
}
}
echo "</tr>";
$counter++;
}
?>
</table>
I'm using foreach loops to access records in a nested array:
while ($row = mysql_fetch_assoc($result)){
$test_groups[$row['group_name']][] = $row['lab_test'];
}
foreach($test_groups as $group_name => $tests){
echo "<tr><td><span class='test_group_name'>" . $group_name . "</span></td></tr>";
foreach($tests as $test){
echo "<tr><td>" . $test . "</td></tr>";
}
echo "<tr><td> </td></tr>";
}
echo '</table>';
This works OK. Now I need to add another level to the nested array, like:
$departments[$row['department_name']][$row['group_name']][] = $row['lab_test'];
Would this work and how should I adjust the for each loops above to cater for this?
Assign to array should be with [] at the end
$departments[$row['department_name']][$row['group_name']][] = $row['lab_test'];
Foreach loop will be:
foreach ($departments as $dep_name => $groups) {
echo $dep_name;
foreach ($groups as $group_name => $tests) {
echo $group_name;
foreach ($tests as $test) {
echo $test;
}
}
}
It's just with echoes, make there HTML as you need.
I have a PHP report which goes out to an array of servers to get their uptime. Then present the uptime in a table. Very simple.
I'm looking to see how to sort this array so that the highest uptime is at the top of the list.
I know about arsort() but I don't know how to apply it to this statement because of the foreach building the table dynamically.
Here's my code:
$servers = server1,server2,server3
foreach ($servers as $srv) {
$output = array(); // Reset the $output array each time
exec("/root/get_report_uptime.sh $srv",$output,$retval);
echo "<tr>";
echo "<td><a href='http://$srv/' target='_blank'>$srv</a></td>";
echo "<td>$output[0] days</td>";
echo "</tr>";
}
$output[0] returns a number like "100". Looking to sort by $output[0] while keeping the $srv linked to it.
$days = array()
foreach ($servers as $srv) {
$output = array(); // Reset the $output array each time
exec("/root/get_report_uptime.sh $srv",$output,$retval);
$days[$srv] = $output[0];
}
arsort($days);
foreach($days as $srv => $day) {
echo "<tr>";
echo "<td><a href='http://".$srv."/' target='_blank'>".$srv."</a></td>";
echo "<td>".$day." days</td>";
echo "</tr>";
}
I have a multidimensional array where each key holds another array as value. I printed each key value pairs in separate tables. But I get unwanted space between tables (spacing between any two tables are not the same). How can I eliminate spacing as a whole?
foreach ($stockist as $key => $value)
{
echo "<table align='center' border='1'>";
echo "<tr><td align = 'center'> <font color = 'blue'> $key</td></tr>";
foreach($value as $key1 => $value1)
{
echo "<tr><td align ='center'>$value1</td></tr><br>";
}
echo "</table>";
}
This should get it all into a single table.
PHP
echo '<table>';
foreach ($stockist as $key => $value)
{
echo '<tr><td>'.$key.'</td></tr>';
foreach($value as $key1 => $value1)
{
echo '<tr><td>'.$value1.'</td></tr>';
}
}
echo '</table>;
I'm pulling 3 colummns from one table and storing them in an array as such to populate a dropdown menu. The ref_code is used to decide which dropdown it will go to ('Module','Customer','Application') while id is the select value and ref_desc is the display text.
foreach ($test as $t) {
$aa[] = array($t->ref_code => array('id'=>$t->id, 'ref_desc'=>$t->ref_desc));
};
I've been trying to retrieve them using $aa['ref_code'] but have not been getting any success. Help please.
This question is similar, but I am unable to retrieve the values I want.
I found a solution on another forum but I will also give your solutions a try later. Thank you very much!
foreach ($aa as $a => $d) {
foreach ($d as $ref_code => $dd) {
echo "<p>". $ref_code ."</p>";
echo "<p>". $dd['ref_desc'] ."</p>";
echo "<p>". $dd['id'] ."</p>";
};
};
Why don't you write it like this? The ref_code (if it's unique) is the key of the array.
foreach ($test as $t) {
$aa[$t->ref_code] = array('id'=>$t->id, 'ref_desc'=>$t->ref_desc);
};
you need a three dimensional array:
dropdown => ids => displaytext
here we go:
// fill in:
$aa = array();
foreach ($test as $t) {
if ( !isset($aa[$t->ref_code]) )
$aa[$t->ref_code] = array();
$aa[$t->ref_code][$t->id] = $t->ref_desc;
};
// have a drop down:
$myDropDown = 'Customer';
foreach ( $aa[$myDropDown] as $id => $displaytext )
{
echo "<option value=\"" . $id . "\">" . $displaytext . "</option>";
}
.... etc.