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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀