S
S
Sergey Sokolov2020-05-21 12:26:14
PHP
Sergey Sokolov, 2020-05-21 12:26:14

How to swap positions of two elements of an associative array?

array

$arr = [
    'a' => 10,
    'b' => 20,
    'c' => 50, // поменять местами 
    'd' => 40, // с этим
    'e' => 10,
];

you need to change the element with the keys 'c'and 'd'places to get:
[
    'a' => 10,
    'b' => 20,
    'd' => 40,
    'c' => 50,
    'e' => 10,
];


How can you control the position of elements in an array in PHP, other than sorting and shift/unshift/push/pop ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-05-21
@sergiks

It seems that it is impossible to simply remember in places, without rewriting the entire array.
So either a numbered array, or cut-and-glue.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question