Php xlsx不会使用SimpleXLSX返回空单元格

Php xlsx不会使用SimpleXLSX返回空单元格,php,xlsx,Php,Xlsx,已解决 您好,目前我正在使用SimpleXLSX解析xlsx(excel文件),在返回数组值时,所有空单元格值都将被删除,但我需要在返回值中保留任何单元格的空/空值 $xlsx = new SimpleXLSX($uploadfile); $j =1; //list($num_cols, $num_rows) = $xlsx->dimension(); //Previous $items = array(); list($col

已解决 您好,目前我正在使用SimpleXLSX解析xlsx(excel文件),在返回数组值时,所有空单元格值都将被删除,但我需要在返回值中保留任何单元格的空/空值

     $xlsx = new SimpleXLSX($uploadfile);
     $j =1;
     //list($num_cols, $num_rows) = $xlsx->dimension();  //Previous
       $items = array();
        list($cols,) = $xlsx->dimension($j);        


    print_r($xlsx->rows());
    //foreach ($xlsx->rows() as $r) { //Previous 
     foreach( $xlsx->rows($j) as $k => $r) {  //fixed
        // if ($i != 0) {
     // for( $i=0; $i < $num_cols; $i++ )//Previous 
       for( $i = 0; $i < $cols; $i++) //fixed
    if(!empty($r[$i]))
                            {
                                $v = $r[$i];
                            }else{

                                $v = '&nbsp;';
                            }
                           // echo $v;
                             $items[$i]  = $v;
                        //   

    }
           $val = implode('|', $items);
 O/P:

 Array
(
[0] => Array
    (
        [0] => Name of the Candidate
        [1] => phone number
        [2] => Postal Address 
        [3] => mobilenumber
        [4] => country
    )

[1] => Array
    (
        [0] => abc
        [2] => 123, 
        [4] => india
    )

[2] => Array
    (
        [0] => fsdfsf
        [1] => 23423423
        [3] => 3223423423
        [4] => us
    )

 )   
$xlsx=newsimplexlsx($uploadfile);
$j=1;
//列表($num_cols,$num_rows)=$xlsx->dimension()//以前的
$items=array();
列表($cols,)=$xlsx->维度($j);
打印($xlsx->rows());
//foreach($xlsx->rows()作为$r){//Previous
foreach($xlsx->rows($j)作为$k=>r){//固定
//如果($i!=0){
//对于($i=0;$i<$num_cols;$i++)//Previous
对于($i=0;$i<$cols;$i++)//已修复
如果(!空($r[$i]))
{
$v=$r[$i];
}否则{
$v='';
}
//echo$v;
$items[$i]=$v;
//   
}
$val=内爆(“|”,$items);
O/P:
排列
(
[0]=>阵列
(
[0]=>候选人的姓名
[1] =>电话号码
[2] =>邮政地址
[3] =>mobilenumber
[4] =>国家
)
[1] =>阵列
(
[0]=>abc
[2] => 123, 
[4] =>印度
)
[2] =>阵列
(
[0]=>fsdfsf
[1] => 23423423
[3] => 3223423423
[4] =>美国
)
)   
未经测试的代码:

<?php

$xlsx = new SimpleXLSX($uploadfile);
list($num_rows, $num_cols) = $xlsx->dimension();

$values = [];
foreach ($xlsx->rows() as $r)
{
    $rowValue = '';

    for ($j = 0; $j < $num_cols; $j++)
    {
        $rowValue .= !empty($r[$j]) && $r[$j] != '' ? $r[$j] : 'null';
    }

    $values[] = $rowValue;
}

var_dump($values);

?>


我不明白……您能提供输入文件的示例吗?它是否包含列名?您是想说$xlsx是一个“锯齿数组”,其中有些行比其他行长?@Mihai Todor:我已经用示例xlsx dataOK进行了更新,现在为您提供的输入示例对每个$r进行var_转储。@Mihai Todor我已经更新了var_转储结果OK,您看到了吗?很简单……当$r[$i][$j]时,您只需在结果中插入一个占位符即可没有定义,而是使用内爆。我认为列表($num_cols,$num_rows)=$xlsx->dimension();需要更改为列表($num_rows,$num_cols)=$xlsx->dimension();然后您可以让$I在$num_行上迭代,$j在$num_cols上迭代另一个内部循环。然后您可以连接$r[$I][$j]插入一个字符串,每个$i放入一个数组中。如果遇到问题,请告诉我。感谢您的帮助,它可以工作,但我需要内爆值$values[]=内爆(',',$rowValue);但我收到警告消息警告:内爆():传递的参数无效dude?您想做什么?$rowValue是一个字符串,您不能对字符串进行内爆。请给我一个$rowValue示例,它不包含您想要的参数,我将告诉您如何修改代码。@Mihar Todor:array(3){[0]=>string(48)“候选电话号码地址的名称”[1]=>string(17)“abcnull123,xyzxyz”[2]=>string(18)“fsdfsf23423null”}只显示前3个单元格值,我需要将单元格值与“|”或“,”分开,或者在连接字符串时添加它们:$rowValue.=(!empty($r[$j])&$r[$j]!=''?$r[$j]:“null”)“|”;您可能还需要在完成一行后修剪最后一个“|”,或者,您可以迭代到$j<$num_cols-1,然后在不添加分隔符的情况下手动执行另一次迭代:$rowValue.=(!empty($r[$num_cols-1])&$r[$num cols-1]!=”?$r[$num_cols-1]:'null');数组(3){[0]=>string(45)”邮政地址邮政地址使用$rowValue后的邮政地址“[1]=>string(30)”123,xyzxyz123,xyzxyz123,xyzxyz”[2]=>string(12)“nullnullnull”}。=(!empty($r[$num_cols-1])&$r[$num cols-1]!=”?$r[$num cols-1]:'null');