PHP:使用foreach修改多维数组的值

时间:2016-12-19 04:19:54

标签: php arrays excel multidimensional-array foreach

我有一个项目使用 Yii2 ,在我的项目中,用户可以上传excel文件,系统读取数据并将其设置为新的二维数组,例如:

Example.xls

enter image description here

这是我在控制器中使用的代码:

 for ($row = 1; $row <= $highestRow; ++$row) {
            $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
            if (!is_null($rowData[0][0])) {
                    foreach ($rowData as $key => $value) {
                        foreach ($value as $vKey => $vValue) {
                            $tempArrayData[] = $vKey . "_" . $vValue;
                        }
                }
                $arrayData[] = $tempArrayData;
            }
}
 echo '<pre>';
 print_r($arrayData);
 echo '</pre>';
 die();

我想在每个数组的值中添加"$key_"作为两个第一个字符。 所以我使用 foreach 来做到这一点,我将其作为结果

Array
(
  [0] => Array
      (
        [0] => 0_first_name
        [1] => 1_last_name
        [2] => 2_age
        [3] => 3_sex
      )
  [1] => Array
      (
        [0] => 0_first_name
        [1] => 1_last_name
        [2] => 2_age
        [3] => 3_sex
        [4] => 0_Archie
        [5] => 1_Will
        [6] => 2_21
        [7] => 3_Male
      )  
  [2] => Array
      (
        [0] => 0_first_name
        [1] => 1_last_name
        [2] => 2_age
        [3] => 3_sex
        [4] => 0_Archie
        [5] => 1_Will
        [6] => 2_21
        [7] => 3_Male
        [8] => 0_Vani
        [9] => 1_Priscillia
        [10] => 2_21
        [11] => 3_Female
      )
)

但这不是我所期望的,在上面的结果数组数组总是从第一列Excel数据开始。实际上,我需要这样的结果:

Array
(
  [0] => Array
      (
        [0] => 0_first_name
        [1] => 1_last_name
        [2] => 2_age
        [3] => 3_sex
      )
  [1] => Array
      (
        [0] => 0_Archie
        [1] => 1_Will
        [2] => 2_21
        [3] => 3_Male
      )  
  [2] => Array
      (
        [0] => 0_Vani
        [1] => 1_Priscillia
        [2] => 2_21
        [3] => 3_Female
      )
)

这是我需要的示例结果,任何人都知道如何得到上面的结果?或者我如何在上面修改我的代码,这样我就能得到我期望的结果。感谢。

被修改

如果我在foreach ($value as $vKey => $vValue) {内删除此print_r($value)foreach ($rowData as $key => $value) {,则结果如下:

Array
(
[0] => first_name
[1] => last_name
[2] => age
[3] => sex
)
Array
(
[0] => Archie
[1] => Will
[2] => 21
[3] => Male
)  
Array
(
[0] => Vani
[1] => Priscillia
[2] => 21
[3] => Female
)

最终数组结果

Array
(
 [0] => Array
    (
     [0] => Array
        (
          [0] => 0_first_name
          [1] => 1_last_name
          [2] => 2_age
          [3] => 3_sex
        )
    )
)

 [1] => Array
    (
     [0] => Array
        (
          [0] => 0_Archie
          [1] => 1_Will
          [2] => 2_21
          [3] => 3_Male
        )  
    )

 [2] => Array
    (
     [0] => Array
        (
          [0] => 0_Vani
          [1] => 1_Priscillia
          [2] => 2_21
          [3] => 3_Female
        ) 
    )    
)

解决了这种情况的最终代码:

 $arrayData = [];
for($row = 1; $row <= $highestRow; ++$row) {
  $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);

  $arrayData[] = array_map(function($values) {
if (!is_null($rowData[0][0])) {
    $tempArrayData = [];
    foreach($values as $key => $value) {
      $tempArrayData[] = $key . '_' . $value;
    }
    return $tempArrayData;
  }, $rowData);
    }
    }

更新

项目有更新需求,项目已成功创建结果为最终结果。但是现在数组需要修改,预期结果就像这样

Array
(
  [0] => Array
    (
     [0] => Array
        (
          [0] => 0_first_name
          [1] => 1_last_name
          [2] => 2_age
          [3] => 3_sex
        )
     )
  [1] => Array
    (
     [0] => Array
        (
          [0] => 0_Archie
          [1] => 1_Will
          [2] => 2_21
          [3] => 3_Male
        )  
    )
  [2] => Array
    (
     [0] => Array
        (
          [0] => 0_Vani
          [1] => 1_Priscillia
          [2] => 2_21
          [3] => 3_Female
        ) 
    )    
)

任何人都知道如何解决这个问题?感谢

1 个答案:

答案 0 :(得分:1)

首先这样做

$rowData = [];

for($row = 1; $row <= $highestRow; ++$row) {
  $rowData[] = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
}

现在您的$rowData应该等于

$rowData = [
  ["first_name", "last_name", "age", "sex"],
  ["Archie", "Will", "21", "Male"],
  ["Vani", "Priscillia", "21", "Female"]
];

你可以尝试这样做

$arrayData[] = array_map(function($values) {
  $tempArrayData = [];
  foreach($values as $key => $value) {
    $tempArrayData[] = $key . '_' . $value;
  }
  return $tempArrayData;
}, $rowData);

最终守则

$arrayData = [];

for($row = 1; $row <= $highestRow; ++$row) {
  $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);

  $arrayData[][0] = array_map(function($values) {
    $tempArrayData = [];
    foreach($values as $key => $value) {
      $tempArrayData[] = $key . '_' . $value;
    }
    return $tempArrayData;
  }, $rowData);
}

最终输出

array:3 [▼
  0 => array:4 [▼
    0 => "0_first_name"
    1 => "1_last_name"
    2 => "2_age"
    3 => "3_sex"
  ]
  1 => array:4 [▼
    0 => "0_Archie"
    1 => "1_Will"
    2 => "2_21"
    3 => "3_Male"
  ]
  2 => array:4 [▼
    0 => "0_Vani"
    1 => "1_Priscillia"
    2 => "2_21"
    3 => "3_Female"
  ]
]