如何使用PHPExcel在Excel中声明数组变量的打孔??/
how to declare punch of array variable in Excel using PHPExcel??/
我通过下面的 php 代码
从 Excel 得到了 A 列和 B 列的值
for($i=1;$i<=$arrayCount;$i++)
{
$col_A = array(trim($allDataInSheet [$i]["A"]));
$col_B =array(trim($allDataInSheet [$i]["B"]));
}
如果'A'有44个变量名,'B'有44个值。
在这种情况下,如何将'B'的值赋值给'A'的变量名
请帮我解决这个问题
PHP 中的数组可以采用两个不同的 key-types,并且它们可以混合搭配。按数字索引和按字符串索引。数组可以包含任何值,包括值数组。这意味着您可以创建一个数组,使用数组 A 中的名称作为键,使用数组 B 中的值作为这些键的值。
$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"
上面,你可以看到,因为 $allDataInSheet[$i]["A"] 是我们想要的字符串,我们可以只使用那个值作为我们的键,它在 B 中的匹配条目作为值那把钥匙。
请注意,如果我们已经设置了某个名称,我们是如何不让该值添加到数组中的。如果您希望 $columns[$a] 是一个值数组,您可以将其更改为如下所示:
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;
这会将 $columns 数组视为数组的数组。这意味着每个位置可以包含多个值。所以,我们把它变成一个数组,然后将 $b 值添加到该位置。如果我们再次遇到 $a 值,我们会看到我们已经设置了那个位置,我们只是使用已经存在的数组。
注意 - 我们进行 isset 检查而不是空检查,因为如果 'b' 实际上是 " " 或 false 或 0 或出于某些奇怪的原因,$columns[$a] 不会从空数组到包含内容的数组,而不是我们不想删除已经存在的值。
祝你好运!
我通过下面的 php 代码
从 Excel 得到了 A 列和 B 列的值 for($i=1;$i<=$arrayCount;$i++)
{
$col_A = array(trim($allDataInSheet [$i]["A"]));
$col_B =array(trim($allDataInSheet [$i]["B"]));
}
如果'A'有44个变量名,'B'有44个值。
在这种情况下,如何将'B'的值赋值给'A'的变量名 请帮我解决这个问题
PHP 中的数组可以采用两个不同的 key-types,并且它们可以混合搭配。按数字索引和按字符串索引。数组可以包含任何值,包括值数组。这意味着您可以创建一个数组,使用数组 A 中的名称作为键,使用数组 B 中的值作为这些键的值。
$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"
上面,你可以看到,因为 $allDataInSheet[$i]["A"] 是我们想要的字符串,我们可以只使用那个值作为我们的键,它在 B 中的匹配条目作为值那把钥匙。
请注意,如果我们已经设置了某个名称,我们是如何不让该值添加到数组中的。如果您希望 $columns[$a] 是一个值数组,您可以将其更改为如下所示:
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;
这会将 $columns 数组视为数组的数组。这意味着每个位置可以包含多个值。所以,我们把它变成一个数组,然后将 $b 值添加到该位置。如果我们再次遇到 $a 值,我们会看到我们已经设置了那个位置,我们只是使用已经存在的数组。
注意 - 我们进行 isset 检查而不是空检查,因为如果 'b' 实际上是 " " 或 false 或 0 或出于某些奇怪的原因,$columns[$a] 不会从空数组到包含内容的数组,而不是我们不想删除已经存在的值。
祝你好运!