简体   繁体   English

PHP 重命名关联数组中的键

[英]PHP rename key in Associative array

I've been searching through the articles on SO for this question and tried many of the solutions in my own code but it's not seeming to work.我一直在搜索关于这个问题的 SO 文章,并在我自己的代码中尝试了许多解决方案,但似乎没有用。

I have the following array我有以下数组

$array[] = array("Order_no"=>$order_id,
                "Customer"=>$customer_id,
                "Product"=>$code_po,
                "Product_description"=>$code_description,
                "Position"=>$pos,
                "Qty"=>$item_qty
                );

I am looking to replace the "Order_no" key with a variable from a database query, lets assume in this case the variable is "new_name"我想用数据库查询中的变量替换“Order_no”键,假设在这种情况下变量是“new_name”

$new = "new_name";
$array[$new]=$array["Order_no"];
unset($array["Order_no"]);
print_r($array);

in the print_r statement I am getting the new_name coming through as the correct order number, but I am still seeing "Order_no" there also, which I shouldn't be seeing anymore.在 print_r 语句中,我得到 new_name 作为正确的订单号,但我仍然在那里看到“Order_no”,我不应该再看到它了。

Thanks.谢谢。

This is your array:这是你的数组:

Array
(
    [0] => Array
        (
            [Customer] => 2
            [Product] => 99
            [Order_no] => 12345
        )

)

One way to do it:一种方法:

<?php

$arr[] = [
    "Order_no" => 12345,
    "Customer" => 00002,
    "Product"=> 99
];

$i_arr = $arr[0];

$i_arr["new_name"] = $i_arr["Order_no"];
unset($i_arr["Order_no"]);

$arr[0] = $i_arr;

print_r($arr);

Another way:其它的办法:

<?php

$arr[] = [
    "Order_no" => 12345,
    "Customer" => 00002,
    "Product"=> 99
];

$arr[0]["new_name"] = $arr[0]["Order_no"];
unset($arr[0]["Order_no"]);

print_r($arr);

To flatten your array out at any time:随时展平您的阵列:

<?php

$arr = $arr[0];
print_r($arr);

You are using extra level of array (by doing $array[] = ... ).您正在使用额外级别的数组(通过执行$array[] = ... )。

You should do it with [0] as first index as:您应该使用[0]作为第一个索引:

$array[0][$new]=$array[0]["Order_no"];
unset($array[0]["Order_no"]);

Live example: 3v4l现场示例: 3v4l

Another option is get ride of this extra level and init the array as:另一种选择是摆脱这个额外的级别并将数组初始化为:

$array = array("Order_no"=>$order_id, ...

As $array is also an array, you have to use index:由于$array也是一个数组,你必须使用索引:

    $array[0][$new]=$array[0]["Order_no"];
    unset($array[0]["Order_no"]);

The other answers will work for the first time you add to the array, but they will always work on the first item in the array.其他答案将在您第一次添加到数组时起作用,但它们始终适用于数组中的第一项。 Once you add another it will not work, so get the current key:一旦添加另一个它将不起作用,因此获取当前密钥:

$array[key($array)][$new] = $array[key($array)]["Order_no"];
unset($array[key($array)]["Order_no"]);

If you want the first one, then call reset($array);如果你想要第一个,然后调用reset($array); first.第一的。

Change your variable to将您的变量更改为

$array=array("Order_no"=>$order_id,"Customer"=>$customer_id,"Product"=>$code_po,"Product_description"=>$code_description,"Position"=>$pos,"Qty"=>$item_qty);

or change your code to或将您的代码更改为

$new = "new_name";
$array[0][$new]=$array[0]["Order_no"];
unset($array["Order_no"]);
print_r($array);

Just be careful this would change the order of the array请注意这会改变数组的顺序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM