PHP 以列分隔的字符串数组到关联数组
PHP array of strings seperated in columns to associative array
我有 unix 磁盘输出,我想将其转换为每一行的关联数组,以便用户可以选择可用磁盘以用于下一个 vg 创建。
当前数组项的输出。每列之间只有 1 个白色 space 我只是做了一些标签,这样更容易阅读。
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 "
我希望它以这样的方式结束。我一直在尝试不同的 foreach 循环,但还没有开始工作。任何 help/suggestions 都表示赞赏。
[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
谢谢
如果它们是标签,您应该可以使用:
foreach($array as $row) {
$pieces = explode("\t",$row);
}
如果各列仅由 1 个白色分隔 space 是准确的,我可以提出以下建议:
$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.
}
按 space、“ ”展开,而不是制表符“\t”
解决方法是:
- 遍历原始数组元素
- 使用
explode()
函数拆分字符串
- 将(键,值)对中的元素存储在临时数组中
- 将临时数组压入原数组中合适的位置。
所以你的代码应该是这样的:
// 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);
将它们分类是问题所在。以下给了我这个。我不确定现在是否需要在某种组合中使用另一个 foreach。我一直在查看示例,但没有找到任何包含那么多字段的字符串。
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 )
我有 unix 磁盘输出,我想将其转换为每一行的关联数组,以便用户可以选择可用磁盘以用于下一个 vg 创建。
当前数组项的输出。每列之间只有 1 个白色 space 我只是做了一些标签,这样更容易阅读。
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 "
我希望它以这样的方式结束。我一直在尝试不同的 foreach 循环,但还没有开始工作。任何 help/suggestions 都表示赞赏。
[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
谢谢
如果它们是标签,您应该可以使用:
foreach($array as $row) {
$pieces = explode("\t",$row);
}
如果各列仅由 1 个白色分隔 space 是准确的,我可以提出以下建议:
$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.
}
按 space、“ ”展开,而不是制表符“\t”
解决方法是:
- 遍历原始数组元素
- 使用
explode()
函数拆分字符串 - 将(键,值)对中的元素存储在临时数组中
- 将临时数组压入原数组中合适的位置。
所以你的代码应该是这样的:
// 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);
将它们分类是问题所在。以下给了我这个。我不确定现在是否需要在某种组合中使用另一个 foreach。我一直在查看示例,但没有找到任何包含那么多字段的字符串。
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 )