0

I have unix disk output that I want to convert into an associated array for each line so a user can pick with available disks to use for the next vg creation.

Output of current array item. There is only 1 white space between each column I just did some tab's so it was easier to read.

array(11) { 
[0]=> string(141) "vg1              LVM  136G   /dev/cciss/c0d0p2        N/A                               N/A       LOCAL N/A  N/A     NO " 
[1]=> string(141) "vg2              LVM  1G     /dev/mapper/mpath28p1   60060e80166fa70000016fa700000013 /dev/dm-33 R700    LS1000  0013    YES " 
[2]=> string(141) "vg3              LVM  60G    /dev/mapper/mpath27p1 60060e80166fa70000016fa700000012 /dev/dm-34 R700  LS1000  0012    YES " 
[3]=> string(141) "vg4              LVM  60G    /dev/mapper/mpath29p1 60060e80166fa70000016fa700000014 /dev/dm-35 R700  LS1000  0014    NO " 
[4]=> string(141) "***AVAILABLE***  FREE 2G     /dev/mapper/mpath21p1 60060e80167220000001722000000048 /dev/dm-37 R700  LS2000  0048    YES " 
[5]=> string(141) "***AVAILABLE***  FREE 2G     /dev/mapper/mpath22p1 60060e80167220000001722000000049 /dev/dm-36 R700  LS2000  0049    YES " 
[6]=> string(141) "***AVAILABLE***  FREE 90G    /dev/mapper/mpath23p1 60060e80166fa70000016fa70000000e /dev/dm-31 R700  LS1000  000e    YES " 
[7]=> string(141) "***AVAILABLE***  FREE 90G    /dev/mapper/mpath24p1 60060e80166fa70000016fa70000000f /dev/dm-39 R700  LS1000  000f    YES " 
[8]=> string(141) "***AVAILABLE***  FREE 90G    /dev/mapper/mpath25p1 60060e80166fa70000016fa700000010 /dev/dm-30 R700  LS1000  0010    YES " 
[9]=> string(141) "***AVAILABLE***  FREE 90G    /dev/mapper/mpath26p1 60060e80166fa70000016fa700000011 /dev/dm-32 R700  LS1000  0011    YES " 
[10]=> string(141) "***AVAILABLE*** FREE 2G     /dev/mapper/mpath30p1 60060e80167220000001722000000047 /dev/dm-38 R700  LS2000  0047    YES " 

I want this to end up something like this. I've been trying different foreach loops but haven't got it to work yet. Any help/suggestions are appreciated.

[0] => name=>vg1 type=>LVM lun_size=>136G  mpath_name=>/dev/cciss/c0d0p2  flun_id=>N/A  dm_name=>N/A  array_type=>LOCAL  array_name=>N/A  lun_id=>N/A  shared=>NO
[1] => name=>vg2 type=>LVM lun_size=>1G  mpath_name=>//dev/mapper/mpath28p1  flun_id=>60060e80166fa70000016fa700000013  dm_name=>/dev/dm-33  array_type=>R700  array_name=>LS1000  lun_id=>0013  shared=>NO
[2] => ....etc

Thanks

debow
  • 73
  • 7

4 Answers4

0

If they are tabs you should be able to use:

foreach($array as $row) {
    $pieces = explode("\t",$row);
}
Pitchinnate
  • 7,517
  • 1
  • 20
  • 37
0

If it is accurate that the columns are only separated by 1 white space, I could suggest something along the lines of:

$result = Array();
foreach($main_array as $string){
    $array = explode(" ", $string);
    //now here you can sort them in the new array however you wish with the appropriate index and value.
}

Explode by space, " ", not tab, "\t"

Ryan
  • 45
  • 8
0

The solution is:

  • Loop through the original array elements
  • Use explode() function to split the string
  • Store elements in (key,value) pair in a temporary array
  • Push the temporary array at the appropriate position in the original array.

So your code should be like this:

// suppose $arr is your original array
$arrLength = count($arr);
for($i = 0; $i < $arrLength; ++$i){
    $component_arr = explode(" ", $arr[$i]);
    $tmp_arr = array();
    $tmp_arr['name'] = $component_arr[0];
    $tmp_arr['type'] = $component_arr[1];
    $tmp_arr['lun_size'] = $component_arr[2];
    $tmp_arr['mpath_name'] = $component_arr[3];
    $tmp_arr['flun_id'] = $component_arr[4];
    $tmp_arr['dm_name'] = $component_arr[5];
    $tmp_arr['array_type'] = $component_arr[6];
    $tmp_arr['array_name'] = $component_arr[7];
    $tmp_arr['lun_id'] = $component_arr[8];
    $tmp_arr['shared'] = $component_arr[9];

    unset($arr[$i]);
    $arr[$i] = $tmp_arr;
}

// display $arr array
var_dump($arr);
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • I did have diff space's between each column. Depended on the length of column. Fixed that first by making all spaces = 1. Then had to just trim the trailing white space from each row. Thanks for the help everyone. Exactly what I needed. – debow Jan 27 '16 at 22:25
0

The sorting them out is the issue. The following gives me this. I'm not sure if I need to now use another foreach within there a combine of some sort. I'm been looking at examples but haven't found anything that has a string with that many fields.

foreach($disks as $row){
                $arraynew = explode("\t", $row);
                print_r($arraynew);

          }
Array ( [0] => vg00 LVM 136G /dev/cciss/c0d0p2 N/A N/A LOCAL N/A N/A NO )     
Array ( [0] => vg01 LVM 1G /dev/mapper/mpath28p1   60060e80013 /dev/dm-33 R700 LS4P991 0013 YES )
debow
  • 73
  • 7