drewrockshard
drewrockshard

Reputation: 2071

PHP Array - Return specific elements of array and display them on one line

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

Upvotes: 0

Views: 55

Answers (3)

Franco Gimenez
Franco Gimenez

Reputation: 55

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.

Upvotes: 1

Mike Miller
Mike Miller

Reputation: 3129

Yeah man over complication. Can't you just do

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

Upvotes: 1

user557846
user557846

Reputation:

um.. just:

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

}

Upvotes: 3

Related Questions