如何用PHP从多维数组的表数据中删除所有权重元素?

在手动将额外的列“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 column

if (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[] = '';

        }

    }

}

?>


梦里花落0921
浏览 118回答 2
2回答

有只小跳蛙

使用数组映射。它遍历每个元素并创建新的元素数组,无论你想返回什么。在您的情况下,您希望删除 'weight' col,因此取消设置。$array1 = [&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'col_0' => '123',&nbsp; &nbsp; &nbsp; &nbsp; 'col_1' => '321',&nbsp; &nbsp; &nbsp; &nbsp; 'weight' => 1&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'col_0' => '123',&nbsp; &nbsp; &nbsp; &nbsp; 'col_1' => '321',&nbsp; &nbsp; &nbsp; &nbsp; 'weight' => 1&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'col_0' => '123',&nbsp; &nbsp; &nbsp; &nbsp; 'col_1' => '321',&nbsp; &nbsp; &nbsp; &nbsp; 'weight' => 1&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'col_0' => '123',&nbsp; &nbsp; &nbsp; &nbsp; 'col_1' => '321',&nbsp; &nbsp; &nbsp; &nbsp; 'weight' => 1&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'col_0' => '123',&nbsp; &nbsp; &nbsp; &nbsp; 'col_1' => '321',&nbsp; &nbsp; &nbsp; &nbsp; 'weight' => 1&nbsp; &nbsp; ],];$array2 = array_map(function($element) {&nbsp; &nbsp; unset($element['weight']);&nbsp; &nbsp; return $element;}, $array1);

慕妹3146593

您可以使用array_map 函数操作数组$arr = array_map(    # apply function for each array element    function ($el) {        # return modified array elements        return [            'col_0'=>$el['col_0'],             'col_1'=>$el['col_1']        ];    },     $arr);上面的代码使用带有匿名函数的 array_map 从源数组接收每个元素,操作它并返回更改的元素
打开App,查看更多内容
随时随地看视频慕课网APP