在PHP中更改关联数组索引的位置

时间:2021-08-31 00:34:45

I have an associative array in PHP. i want to change position of an array index and its value.

我在PHP中有一个关联数组。我想改变数组索引的位置及其值。

Array
(
    [savedRows] => 1
    [errors] => Array
        (
            [0] => System has Skipped this row, because you have enter not valid value "" for field "description" in sheat "Electronics Laptops" row number "4"
    )

    [success] => successfully saved.
)

to like this

喜欢这个

Array

( [savedRows] => 1 [success] => successfully saved. [errors] => Array ( [0] => System has Skipped this row, because you have enter not valid value "" for field "description" in sheat "Electronics Laptops" row number "4" ) )

([savedRows] => 1 [success] =>已成功保存。[errors] =>数组([0] =>系统已跳过此行,因为您输入了无效值“”字段“描述”在sheat中“电子笔记本电脑“行号”4“))

i want to change ["errors"] index position from second to last and [success] index position at second when ever this array build. This is a dynamic array not a static it builds when a function call on function return i am getting this array.

我希望改变[“errors”]索引位置从秒到最后和[成功]索引位置在第二次这个数组构建时。这是一个动态数组而不是静态它在函数调用函数返回时生成这个数组。

3 个解决方案

#1


3  

You can use array functions, but by far the easiest way to change it would be:

您可以使用数组函数,但到目前为止,更改它的最简单方法是:

$newRow = ['savedRows' => $oldRow['savedRows'],
           'success'   => $oldRow['success'], 
           'errors'    => $oldRow['errors']];

But it is an associative array, not a numeric array, so order should not be that important.

但它是一个关联数组,而不是数字数组,所以顺序不应该那么重要。

#2


2  

Why does the order in the array matter?

为什么数组中的顺序很重要?

If you really need that visually, you should initialize your array before you use it and overwrite the values when you fill it:

如果你真的需要它,你应该在使用之前初始化你的数组,并在填充它时覆盖它们:

$arr = [
  'savedRows' => 0,
  'success' => '',
  'errors' => [],
]
// the rest of your code

Note that the order should not matter, if it does, you have another problem that you should fix.

请注意,顺序无关紧要,如果确实如此,则还有其他问题需要修复。

#3


2  

You need not make this too complicated or use any fancy functions. Just follow a few simple steps.

你不需要太复杂或使用任何花哨的功能。只需按照几个简单的步骤。

  • Store the errors sub array in another variable $errorField.
  • 将errors子数组存储在另一个变量$ errorField中。
  • Unset the array index "errors"
  • 取消设置数组索引“错误”
  • Append this $errorField to a new key "errors".

    将此$ errorField附加到新密钥“errors”。

    $errorField = $array['errors'];
    unset($array['errors']);
    $array['errors'] = $errorField;
    

#1


3  

You can use array functions, but by far the easiest way to change it would be:

您可以使用数组函数,但到目前为止,更改它的最简单方法是:

$newRow = ['savedRows' => $oldRow['savedRows'],
           'success'   => $oldRow['success'], 
           'errors'    => $oldRow['errors']];

But it is an associative array, not a numeric array, so order should not be that important.

但它是一个关联数组,而不是数字数组,所以顺序不应该那么重要。

#2


2  

Why does the order in the array matter?

为什么数组中的顺序很重要?

If you really need that visually, you should initialize your array before you use it and overwrite the values when you fill it:

如果你真的需要它,你应该在使用之前初始化你的数组,并在填充它时覆盖它们:

$arr = [
  'savedRows' => 0,
  'success' => '',
  'errors' => [],
]
// the rest of your code

Note that the order should not matter, if it does, you have another problem that you should fix.

请注意,顺序无关紧要,如果确实如此,则还有其他问题需要修复。

#3


2  

You need not make this too complicated or use any fancy functions. Just follow a few simple steps.

你不需要太复杂或使用任何花哨的功能。只需按照几个简单的步骤。

  • Store the errors sub array in another variable $errorField.
  • 将errors子数组存储在另一个变量$ errorField中。
  • Unset the array index "errors"
  • 取消设置数组索引“错误”
  • Append this $errorField to a new key "errors".

    将此$ errorField附加到新密钥“errors”。

    $errorField = $array['errors'];
    unset($array['errors']);
    $array['errors'] = $errorField;