0

I'm about ready to go crazy with this one. I can't seem to figure out how to print out elements of an array and then put all the results on one line (with a specific output).

Here's my array data (after running it through a foreach statement:

Array
(
    [hostname] => server01
    [server_id] => 4
    [disk_mountpoint] => /
    [disk_datetime] => 1426395600
    [disk_device] => /dev/mapper/VolGroup00-LogVol00
    [disk_capacity] => 15G
    [disk_used] => 5.3G
)
Array
(
    [hostname] => server01
    [server_id] => 4
    [disk_mountpoint] => /var
    [disk_datetime] => 1426395600
    [disk_device] => /dev/mapper/VolGroup01-LogVol00
    [disk_capacity] => 107G
    [disk_used] => 52G
)
Array
(
    [hostname] => server01
    [server_id] => 4
    [disk_mountpoint] => /opt
    [disk_datetime] => 1426395600
    [disk_device] => /dev/mapper/VolGroup02-LogVol00
    [disk_capacity] => 156G
    [disk_used] => 127G
)

Here's my foreach statement:

foreach ($storage_report as $item) {
                print_r($item);
}

I've spent a couple of hours trying to wrap my mind around a multi-dementional array as well as looking at some other peoples questions on StackOverflow, but nothing seems to fit what I'm trying to do.

By the way, the array above is actually output from a MySQL query and it returns only one unique hostname, so where it has the same hostname as an element, that is correct (same with the server_id).

I even tried a nested foreach loop and that didn't work well and I got confused again, so I didn't want to post it here.

Basically, with the data above, I want the output to look like:

$disk_mountpoint = $disk_used

With that being said, the ultimate output (if you were to parse what I just wrote), the final output would look like:

/ = 5.3G
/var = 52G
/opt = 127G

I feel like I'm over complicating things and wanted to know if someone could just point me in the right direction and help me out.

Thanks! Drew

3 Answers 3

3

um.. just:

foreach ($storage_report as $item) {
    echo $item['disk_mountpoint'] .'='.$item['disk_used'];

}
Sign up to request clarification or add additional context in comments.

3 Comments

Dang it ... I told you I was over-complicating things. I even have this working somewhere else in my code that I did with another set of data. I think I over complicated things because I originally was getting all data from mysql, and so I was grouping by the server_id and so it became a multi-dementional array. Therefore, I've been thinking with that mentality the entire time instead of just looking at what is obviously an easy single dementional array.
Especially after staring at code all day. You could also look at implode() for this sort of shiz
@Dagon He did, and spared us all.
1

Yeah man over complication. Can't you just do

foreach ($storage_report as $item) {
   echo $item['disk_mountpoint']."=".$item['disk_used'];
}

1 Comment

Yea, I commented on @Dagon answer. I had two different ideas going on and just got overwhelmed.
1

If it is that I undestand, so:

foreach ($storage_report as $item) 
{
      if (isset($item['disk_mountpoint']) && isset($item['disk_used']))
      {
          echo $item['disk_mountpoint'] .'='. $item['disk_used']; 
      }
}

That will iterate the entire collection and will read the content of each cell only if it is set.

2 Comments

Even-though this code helps, please explain it a little bit more.
What part needs explain? I thinks that $item can have nulls cells like disk_mountpoint or disk_used if it can't be read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.