-1

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.

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"

$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.

Thanks.

4
  • You can't unset it, because that key doesn't exist on the level you are trying to unset it... Commented Jan 8, 2020 at 15:15
  • 6
    Your array is an array inside an array. For this example, you'd need to $array[0][$new]=$array[0]["Order_no"]; and unset($array[0]["Order_no"]); and it should work. Commented Jan 8, 2020 at 15:17
  • @Jeto - No I need the [] part as I'm then sending the array to a json_encode which puts it into a pivot table. Commented Jan 8, 2020 at 15:19
  • @BertMaurau Ok I see thanks, the array gets encoded and then displayed in a pivot table. The label on the variable is what I specify the key as. Is there any way I can enter this as a variable rather than "Order_no". It won't let me add $new to the original array Commented Jan 8, 2020 at 15:21

5 Answers 5

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Is it ok to always use [0] in this case?
@Macca424 it depends on your preference/needs. Imo it's not ideal. See my edit if you don't know how to flatten it out.
1

You are using extra level of array (by doing $array[] = ...).

You should do it with [0] as first index as:

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

Live example: 3v4l

Another option is get ride of this extra level and init the array as:

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

1 Comment

This has pretty much solved it thanks, I just had to get a count of how many times my loop goes through the array, then when the array has been completed, I update and unset the variables based on j value.
1

As $array is also an array, you have to use index:

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

Comments

0

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); first.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.