duankuaizhe8257 2017-02-07 03:26
浏览 14
已采纳

如何使用PHPExcel在Excel中声明数组变量的打孔?

I got values of column A and B from Excel by below php code

 for($i=1;$i<=$arrayCount;$i++)
{
$col_A = array(trim($allDataInSheet [$i]["A"])); 

$col_B =array(trim($allDataInSheet [$i]["B"])); 
} 

If 'A' has 44 variable names and 'B' has 44 values.

In this scenario,How can I assign the values of 'B' to the variable names of 'A' Please help me to solve this

  • 写回答

1条回答 默认 最新

  • douhua9726 2017-02-07 03:45
    关注

    Arrays in PHP can take on two different key-types and they can mix and match. Indexed by number and indexed by string. And an array can contain any value, including an array of values. This means that you can create an array that uses the names in Array A as the keys and the values in Array B as the values for those keys.

    $columns = [];
    // Arrays start at 0, but since these came from excel 0 is the column header.
    // You want to stop 1 entry before the count of your array. Since arrays are 0 indexed, the array count is 1 larger than the last index.
    for($i=1;$i<=$arrayCount-1;$i++)
    {
        $a = trim($allDataInSheet [$i]["A"]);
        $b = trim($allDataInSheet [$i]["B"]);
        // You don't need to specify the Array constructor anymore. 
        // You can just use brackets to create a new array.
        // Not sure if you still want these, but I left them for you.
        $col_A = [$a]; 
        $col_B = [$b]; 
    
        // Assign the values of B to columns in A
        if(!isset($columns[$a]) {
            $columns[$a] = $b;
        } else {
          // Debugging message - Tried to set two values to the same name.
        }
    } 
    // Do stuff with $columns
    // $columns["a"] == "b"
    

    Above, you can see that since $allDataInSheet[$i]["A"] is the string we want, we can just use that value as our key and its matching entry in B as the value for that key.

    Notice how we don't let a value get added to the array if we already have that name set. If you want $columns[$a] to be an array of values, you can change it to look like this:

    if(!isset($columns[$a]){
        // If we don't have an entry for $columns[$a] create an array here to hold the values for possible $b's.
        $columns[$a] = [];
    }
    
    // Add $b to the $columns[$a] array.
    $columns[$a][] = $b;
    

    That will treat the $columns array as an array of arrays. Meaning that each position can hold multiple values. So, we turn it into an array and just add the $b value to that position. If we come across that $a value again, we'll see that we already have that position set and we just use the array that's already there.

    Notice - we do an isset check instead of an empty check because if 'b' was actually " " or false or 0 or for some strange reason, $columns[$a] doesn't change from an empty array to an array with something in it, than we don't want to erase the value that's already there.

    Good Luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?