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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?