在手动将额外的列“CAD 数据”附加到带有表格数据的多维数组后,不幸的是,我在“CAD 数据”列之前得到了这个糟糕的 [weight] 列。我的表数据输出截图实际上我只是想再次删除输出前的整个 [weight] 列。我尝试了几件事,但没有成功。Array ( [row_0] => Array ( [col_0] => Order number [col_1] => Size [weight] => 1 ) [row_1] => Array ( [col_0] => 502 1001 [col_1] => 20 [weight] => 2 ) [row_2] => Array ( [col_0] => 502 1002 [col_1] => 25 [weight] => 3 ) [row_3] => Array ( [col_0] => 502 1003 [col_1] => 30 [weight] => 4 ) ) 在我的代码下面:<?php$rows = $element['#object']->field_product_data_table['und'][0]['tabledata']['tabledata'];$header = array_shift($rows);$attributes = array( "id" => "tablefield-0", "class" => array("tablefield"));// append CAD data columnif (array_key_exists('und', $element['#object']->field_product_step_data)) { $header[] = t('CAD data'); // make index of all entries in table $entry_row_id_index = array(); foreach ($rows as $row_id => &$row) { foreach ($row as $entry) { $entry_row_id_index[$entry] = $row_id; } } $step_files = $element['#object']->field_product_step_data['und']; // add step-file to entry to rows with corresponding order number $max_row_length = 0; foreach ($step_files as &$file) { if (array_key_exists($file['description'], $entry_row_id_index)) { $orderNumber = $file['description']; $row_id = $entry_row_id_index[$orderNumber]; $rows[$row_id][] = '<a href="#" class="step-file" data-filename="' . $file['filename'] . '" data-ordernumber="' . $orderNumber . '"></a>'; $row_length = count($rows[$row_id]); if ($row_length > $max_row_length) $max_row_length = $row_length; } } // fill rows so all have same length foreach ($rows as $row_id => &$row) { for ($i = count($row); $i < $max_row_length; $i++) { $row[] = ''; } }}?>
2 回答
有只小跳蛙
TA贡献1824条经验 获得超8个赞
使用数组映射。它遍历每个元素并创建新的元素数组,无论你想返回什么。在您的情况下,您希望删除 'weight' col,因此取消设置。
$array1 = [
[
'col_0' => '123',
'col_1' => '321',
'weight' => 1
],
[
'col_0' => '123',
'col_1' => '321',
'weight' => 1
],
[
'col_0' => '123',
'col_1' => '321',
'weight' => 1
],
[
'col_0' => '123',
'col_1' => '321',
'weight' => 1
],
[
'col_0' => '123',
'col_1' => '321',
'weight' => 1
],
];
$array2 = array_map(function($element) {
unset($element['weight']);
return $element;
}, $array1);
慕妹3146593
TA贡献1820条经验 获得超9个赞
- 2 回答
- 0 关注
- 114 浏览
添加回答
举报
0/150
提交
取消