1

I am trying to overwrite [19] in my array, the first value every time. [19] will not always be the same as it populates off an ID that is passed into the array. I want to replace that first index with a new ID based on certain validation.

I've tried unsetting that value and replacing it but it creates a new entry (shown below).

<pre style='font-size:11px'>
[------- var dumping... -------]
arrcount: 2
Array
(
    [19] => Array
        (
            [quantity] => 1
        )

    [] => 50
)
</pre>

Rather than have a new entry in the array like [] => 50, I would like to replace [19] with [50] while keeping the quantity value the same. Keeping in mind that these numbers are not static, they are dynamic in the case that it is whatever Id is passed based of outside validation.

q_w_d
  • 31
  • 3
  • 2
    Have you looked at using [`array_shift()`](https://www.php.net/manual/en/function.array-shift.php). – Nigel Ren Jul 19 '19 at 14:46
  • If an answer solved your problem, consider accepting the answer. Here's how http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work then return here and do the same with the tick/checkmark till it turns green. This informs the community, a solution was found. Otherwise, others may think the question is still open and may want to post (more) answers. You'll earn points and others will be encouraged to help you. *Welcome to Stack!* – Jay Blanchard Jul 19 '19 at 15:06

3 Answers3

1

Welcome to StackOverflow.

array_shift() and then array_unshift will be what you are looking for.

https://www.php.net/manual/en/function.array-unshift.php

If you post a code example we could help more.

Robin Wright
  • 303
  • 1
  • 12
0

Just assign the value to the new key, and unset the old one.

$arr[$newkey] = $arr[$oldkey];
if (isset($arr[$oldkey])) {
   unset($arr[$oldkey]);
}
failedCoder
  • 1,346
  • 1
  • 14
  • 38
  • If you are just going to copy from another answer - this would be considered a duplicate and closed as such. It saves having multiple questions with the same answers repeated. – Nigel Ren Jul 19 '19 at 14:48
  • OP said the index will change. You could check for the key that but shift and unshift should do the trick. – Robin Wright Jul 19 '19 at 14:50
  • This will place the new value at the end of the array, won't it? – Don't Panic Jul 19 '19 at 14:59
0

Get the first key and the first value.

$first_key = array_key_first($array);
$first_value = $array[$first_key];
/**
*  Or for PHP < 7.3
*  $first_value = reset($array);
*  $first_key = key($array);
*/

Unset the first key and union the remainder with the re-keyed first value.

unset($array[$first_key]);
$array = [50 => $first_value] + $array;

array_shift won't work for this because it reindexes the array. Keep in mind that if the array happens to have another element with index 50, that element will be removed, since the keys must be unique.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • Several of the answers on the duplicate question (not the accepted answer) would work for this as well, but the fact that it's just the first element makes the problem a little simpler. – Don't Panic Jul 19 '19 at 15:51