我正在通过PHPExcel解析Excel文件。 Excel文件有两列A和B.如下所示:
________________________
| A | B |
________________________
| 987654321 | 55,12,11 |
| 789456123 | 1,2,3 |
| 998877665 | 10,11,12 |
| 112233345 | 8 |
________________________
$array1
:
Array
(
[0] => "987654321","55,12,11"
[1] => "789456123","1,2,3"
[2] => "998877665","10,11,12"
[3] => "112233345","8"
)
我使用此代码来解析$array1
:
$res = array();
foreach ($array1 as $key => $value)
{
if ($arr != '')
{
$res[$key] = explode(",", str_replace('"', '', $value));
}
}
我想得到这个结果:
Array
(
[0] => Array
(
[0] => 987654321
[1] => 55,12,11
)
[1] => Array
(
[0] => 789456123
[1] => 1,2,3
)
) //etc...
但我得到了:
Array
(
[0] => Array
(
[0] => 987654321
[1] => 55
[2] => 12
[3] => 11
)
[1] => Array
(
[0] => 789456123
[1] => 1
[2] => 2
[3] => 3
)
) //etc..
我如何得出结论?
答案 0 :(得分:1)
您可以使用:
$res[$key] = explode('","', trim($value, '"'));
请注意,只要您尝试分隔字符串而不是整数,解析这样的逗号分隔部分就会失败。
答案 1 :(得分:1)
将其视为CSV以及它来自Excel的方式:
foreach($array1 as $key => $value) {
$res[$key] = str_getcsv($value);
}