Multiple nested array from MySQL query - php

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.

Related

PHP sort multiple arrays

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>";
}

php foreach loop with continue if value has already been echoed

I have a foreach loop and I need to add a continue if the value has already been echoed. I can't find the right syntax. The loop:
foreach ($results as $result) {
echo $result->date . "<br />";
}
But I need to add in a continue so that if the value has already been echoed, and it comes up again in the loop it gets skipped. I can't quite get the if/continue statement quite right.
Thoughts, suggestions, ideas?
As mentioned by #JonathanKuhn in the comments - here is how you would run that loop:
$already_echoed = array();
foreach ($results as $result) {
if (!in_array($result->date, $already_echoed)) { //check if current date is in the already_echoed array
echo $result->date . "<br />";
}
$already_echoed[] = $result->date; //store all dates in an array to check against.
}
$echoedArray = array();
foreach ($results as $result) {
if (isset($echoedArray[$result->date])) {
continue;
}
echo $result->date . "<br />";
$echoedArray[$result->date] = true;
}
$alreadyOutput = array();
foreach ($results as $result) {
if(in_array($result->date, $alreadyOutput)){
continue;
}
$alreadyOutput[] = $result->date;
echo $result->date . "<br />";
}

How to count the mutli-dimensional arrays' value in PHP

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

foreach with two kind of values

<?php
foreach ($array['response']['data']['Offers'] as $arr) {
$gegevens = array(
$arr['Offer']['id'],
$arr['Offer']['name'],
$arr['Advertiser']['company'],
$arr['Offer']['advertiser_id'],
$arr['Offer']['offer_url'],
$arr['Offer']['default_payout'],
$arr['Offer']['expiration_date']
);
echo '<tr>';
foreach ($gegevens as $value) {
echo "<td>{$value}</td>";
}
echo "</tr>\n";
}
?>
This is the code I have.
How can I search for two kind of values inside the foreach($array['response']['data']?
It has to be foreach($array['response']['data'][**Offers**] and also foreach($array['response']['data'][**Advertisers**].
I need this so that I can echo out $arr['**Offer**']['name'], $arr['**Advertiser**'] ['company']
Can someone help me with this?
In its simplest:
foreach($array['response']['data']['Offers'] as $key => $offer_arr){
$advertiser_arr = $array['response']['data']['Advertisers'][$key];
}
You then have offer array and advertiser array of the same index
Doesn't sound like you need to put them all into an array. This should suffice:
<?php
foreach($array['response']['data']['Offers'] as $arr) {
echo '<tr>';
echo "<td>{$arr['Offer']['name']}</td>";
echo "<td>{$arr['Advertiser']['company']}</td>";
echo "</tr>\n";
}
?>

Outputing 2 object arrays in foreach cycle

below is example how mine arrays do look, i want them combine so i can output both title, votes and ratings in one line.
foreach ($items->items as $item) {
echo $item->title;
foreach ($results->resx as $res) {
echo $res->votes;
echo $res->ratings;
}
I'd like to have this, but i know this isn't right.
foreach ($items as $item) ($results as $res) {
echo $res->votes;
echo $res->ratings;
echo $item->title;
}
You can use array_merge() for that, like so:
foreach (array_merge($results, $items) as $item) {
echo isset($item->title) ? $item->title : $item->votes .'<br>'. $item->ratings;
}
UPDATE:
Changed how to print values as the objects from merged array can only have one of the two groups of properties.
UPDATE 2:
After some OP's notes that made more clear what his scenario is, and now given the assumption that both $results and $items arrays have the same number of elements, an update solution is as follows:
while ((list(, $it) = each($items)) && (list(, $rs) = each($results))) {
echo $it->title;
echo $rs->votes;
echo $rs->ratings . '<br>';
}

Categories