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

时间:2021-07-19 07:57:42

I have a project using Yii2, in my project the user can upload an excel file, and the system read the data and set it into a new two dimensional array, for example:

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

Example.xls

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

An this is the code that I've used in my Controller:

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

 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();

I want to add "$key_" as two first character in each array's values. So I use foreach to do that, and I've got this as the result:

我想在每个数组的值中添加“$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
      )
)

But that's not what I've expected, in the result above the array of array always start from first column of Excel data. Actually, I need a result like this:

但这并不是我所期望的,因为数组的数组总是从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
      )
)

This is the example result that I need, anyone know how to get result like above? or how can I modify my code in above so I can get result as I've expect. Thanks.

这是我需要的示例结果,有人知道如何得到如上的结果吗?或者如何修改上面的代码,这样我就可以得到我期望的结果。谢谢。

Edited

If I remove this foreach ($value as $vKey => $vValue) {, and print_r($value) inside foreach ($rowData as $key => $value) {, it has result like this:

如果我将这个foreach ($ vKey = $vKey => $vValue){,和print_r($value)在foreach内删除($rowData作为$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
)

Final array result

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
        ) 
    )    
)

Final Code that solved this case:

 $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);
    }
    }

UPDATE

the project has an update requierment, the project has been successfully create result as Final Result above. But now the array need a modify, the expected result is like this:

项目有更新要求,项目已成功创建了以上最终结果。但现在数组需要修改,预期结果如下:

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
        ) 
    )    
)

Anyone know how to solve this? Thanks

有人知道怎么解决吗?谢谢

1 个解决方案

#1


1  

First do this

第一次做这个

$rowData = [];

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

Now your $rowData should be equal to

现在你的$rowData应该等于

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

You can simply try doing this

你可以试着这么做

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

Final Code

$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);
}

Final Output

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"
  ]
]

#1


1  

First do this

第一次做这个

$rowData = [];

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

Now your $rowData should be equal to

现在你的$rowData应该等于

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

You can simply try doing this

你可以试着这么做

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

Final Code

$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);
}

Final Output

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"
  ]
]