重新组织缺少关键项的数组-PHP

时间:2019-02-21 12:21:44

标签: php arrays sorting

我有一个输出这样的数组,您会注意到在输出[2]的键中没有数据。我如何遍历此数组并确保该数组没有丢失的键并且对数据进行了排序。仅使用数组中的数据即可。

print_r($layout_array); 

Array
(
    [0] => Array
        (
            [row_identifier] => home
            [row_text_title] => home
            [original_row_position] => 1
            [row_position] => 1
            [row_show] => yes
            [row_link_ref] => https://demo.ul
            [button_styling] => off
            [tab] => self
        )

    [1] => Array
        (
            [row_identifier] => benefits
            [row_text_title] => benefits
            [row_link_ref] => #benefits
            [button_styling] => off
            [tab] => self
            [row_position] => 2
            [original_row_position] => 2
            [row_show] => yes
        )

    [3] => Array
        (
            [row_identifier] => about
            [row_text_title] => about
            [row_link_ref] => #about
            [button_styling] => off
            [tab] => self
            [row_show] => no
        )

    [4] => Array
        (
            [row_identifier] => team
            [row_text_title] => team
            [row_link_ref] => #team
            [button_styling] => off
            [tab] => self
            [row_position] => 4
            [original_row_position] => 4
            [row_show] => yes
        )
)

谢谢!

3 个答案:

答案 0 :(得分:5)

您可以使用array_values,它基本上会“重置”键:

$layout_array = array_values($layout_array);

答案 1 :(得分:1)

如果要对数据进行排序并重新索引以删除丢失的键,则可以使用array_multisort。例如,如果您要按row_identifier进行排序:

$sort_by_column = 'row_identifier';
array_multisort(array_column($layout_array, $sort_by_column), $layout_array);
print_r($layout_array);

输出:

Array
(
    [0] => Array
        (
            [row_identifier] => about
            [row_text_title] => about
            [row_link_ref] => #about
            [button_styling] => off
            [tab] => self
            [row_show] => no
        )

    [1] => Array
        (
            [row_identifier] => benefits
            [row_text_title] => benefits
            [row_link_ref] => #benefits
            [button_styling] => off
            [tab] => self
            [row_position] => 2
            [original_row_position] => 2
            [row_show] => yes
        )

    [2] => Array
        (
            [row_identifier] => home
            [row_text_title] => home
            [original_row_position] => 1
            [row_position] => 1
            [row_show] => yes
            [row_link_ref] => https://demo.ul
            [button_styling] => off
            [tab] => self
        )

    [3] => Array
        (
            [row_identifier] => team
            [row_text_title] => team
            [row_link_ref] => #team
            [button_styling] => off
            [tab] => self
            [row_position] => 4
            [original_row_position] => 4
            [row_show] => yes
        )
)

Demo on 3v4l.org

请注意,您只能对所有值中都存在的键进行排序。

答案 2 :(得分:0)

如果要在没有php函数的情况下执行:

$new_array = array();            
foreach($test as $variable){    // $test is your array
  array_push($new_array,$variable);
}