1

I have an array like:

$columns = [
        'id'=>'Id',
        'status'=>'Status',
        'created_at'=>'Created At'
];

and I want to replace key and value 'status'=>'Status' with 'payment_status'=>'Payment status'; I know that I can delete element by key using unset($columns['status']) and add the new element to the end or to the beginning of the array. But I want to keep elements order so the new element should be added after `id'.

Andriy Lozynskiy
  • 2,444
  • 2
  • 17
  • 35
  • Why is the order so important? – Nigel Ren Dec 18 '17 at 08:23
  • @NigelRen because there are columns for a table in this array. I use foreach to display them and `payment_status` column should be after `id` – Andriy Lozynskiy Dec 18 '17 at 08:27
  • The order of the items shouldn't make any difference. https://3v4l.org/RqBv7 – Andreas Dec 18 '17 at 08:40
  • @Andreas I don't use multidimentional array. I use datatables and in my array key is a name of a column and value is a label. – Andriy Lozynskiy Dec 18 '17 at 08:56
  • If it's not multidimensional then I can not understand why you need them in a specific order. You just echo the correct item at the correct place. Don't loop if you don't have to, it's just creates a chance for a bug to find its way in. – Andreas Dec 18 '17 at 08:59
  • @Andreas this peace of code displays table: http://sandbox.onlinephpfunctions.com/code/f748717284cb9f9f4b12cb6e2c20415acd199e22 – Andriy Lozynskiy Dec 18 '17 at 09:10
  • Related: https://stackoverflow.com/q/1783089/2943403 and https://stackoverflow.com/questions/24534357/array-splice-with-a-custom-key – mickmackusa Feb 07 '22 at 12:51

3 Answers3

2

One way I think of is using array_search() to get key as offset. Then using array_merge() with array_slice()

$offset = array_search('status', array_keys($columns));
$payment_status = $columns['status'];

$result = array_merge(array_slice($columns, 0, $offset), array('payment_status' => $payment_status), array_slice($columns, $offset, null));
unset($result['status']);

print_r($result);
Goma
  • 2,018
  • 1
  • 10
  • 19
0

One of the way is, to create another array and assign it to your exiting array, this way your original array will be replaced.

$newarr = array(
'id' => 'Id',
'payment_status' => 'Payment status',
'created_at' => 'Created At');

$columns = $newarr;
Ashish
  • 3,572
  • 3
  • 18
  • 25
halojoy
  • 225
  • 2
  • 7
  • 3
    Although this may answer the questions, you may wan't to provide some extra details and explain the answer. In it's current state this answer is low-quality. – Rolf ツ Dec 18 '17 at 08:50
0

You can use array_map to walk through the entire array only once and create a new array of new columns name.

$myArray=array("id"=>123,"status"=>"no paid");
$otherArray = array();
$columns = array(
        'id'=>'Id',
        'status'=>'Status',
        'created_at'=>'Created At'
);


array_map(function($column,$key) {
    if(array_key_exists($key,$GLOBALS['columns']))
        $GLOBALS['otherArray'][$GLOBALS['columns'][$key]]=$column;
    else
        $GLOBALS['otherArray'][$key]=$column;
    return $column;

}, $myArray,array_keys($myArray));
print_r($otherArray);

Here is the demo

Ayush Jain
  • 144
  • 10