-1

Here is my array

Array
(
    [0] => Array
        (
            [0] => 710715609
            [1] => 3
            [2] => 2020-02-28 00:01:01/2020-02-25 00:01:01/2020-02-21 00:01:01
            [3] => 2
            [4] => sports/Mtunes/Astro/D50
            [5] => sports-1/Mtunes-1/Astro-1/D50-2
        )

    [1] => Array
        (
            [0] => 119774100
            [1] => 2
            [2] => 2020-02-22 00:01:01/2020-02-22 00:01:01
            [3] => 1
            [4] => sports/D50
            [5] => sports-1/D50-1
        )


)

I want to change these 5 keys in array to (msisdn, logCount, logins, transc, actVas) like this and I have tried by using array_fill_keys

Is there a way to do this ?

Sampath Wijesinghe
  • 789
  • 1
  • 11
  • 33
  • 1
    [This old answer](https://stackoverflow.com/a/308794/4205384) still holds. – El_Vanja Mar 18 '20 at 17:26
  • Also, I'd change the title of the question. You're not looking to add keys, you're looking to change them. – El_Vanja Mar 18 '20 at 17:26
  • 1
    Does this answer your question? [PHP Change Array Keys](https://stackoverflow.com/questions/308703/php-change-array-keys) – Jon Sampson Mar 18 '20 at 18:38
  • @El_Vanja technically you can't change keys. You can create new arrays however with your desired keys. – Progrock Mar 19 '20 at 16:02
  • @Progrock Yes, technically. But the general idea is to replace them with something else. Adding sounds like it might be solved with a trivial assignment `$a['key'] = val`. It's way more likely someone would search for "change" over "add". – El_Vanja Mar 19 '20 at 17:41
  • This page is a duplicate of [Remap keys in each row of a 2d array to replace indexes with predefined associative keys](https://stackoverflow.com/q/23710311/2943403) – mickmackusa Apr 17 '23 at 21:43

2 Answers2

2

You can map and combine:

$array = array_map(function($v) {
                       return array_combine(["msisdn", "logCount", "logins",
                                             "transc", "actVas", "vasCount"], $v);
                   }, $array);

Or walk and combine:

array_walk($array, function(&$v) {
                       $v = array_combine(["msisdn", "logCount", "logins", 
                                           "transc", "actVas", "vasCount"], $v);
                   });

You can do either one with a defined array and use() like:

$k = ["msisdn", "logCount", "logins", "transc", "actVas", "vasCount"];

$array = array_map(function($v) use($k) { return array_combine($k, $v); }, $array);
//or
array_walk($array, function(&$v) use($k) { $v = array_combine($k, $v); });
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1

Assuming you may have a very large array, this will just replace the existing arrays keys rather than making another copy of the array.

$pushed = [
    [ 710715609, 3, '2020-02-28 00:01:01/2020-02-25 00:01:01/2020-02-21 00:01:01', 2, 'sports/Mtunes/Astro/D50', 'sports-1/Mtunes-1/Astro-1/D50-2'],
    [ 119774100, 2, '2020-02-22 00:01:01/2020-02-22 00:01:01', 1, 'sports/D50', 'sports-1/D50-1']
];

$names = ["msisdn", "logCount", "logins", "transc", "actVas", "vasCount"];

foreach ($pushed as $x => $push){
    foreach ($push as $i => $v) {
       unset ($pushed[$x][$i]);
       $pushed[$x][$names[$i]] = $v;
    }
}
print_r($pushed);

RESULT

Array
(
    [0] => Array
        (
            [msisdn] => 710715609
            [logCount] => 3
            [logins] => 2020-02-28 00:01:01/2020-02-25 00:01:01/2020-02-21 00:01:01
            [transc] => 2
            [actVas] => sports/Mtunes/Astro/D50
            [vasCount] => sports-1/Mtunes-1/Astro-1/D50-2
        )

    [1] => Array
        (
            [msisdn] => 119774100
            [logCount] => 2
            [logins] => 2020-02-22 00:01:01/2020-02-22 00:01:01
            [transc] => 1
            [actVas] => sports/D50
            [vasCount] => sports-1/D50-1
        )

)
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149