0

I have got several or an amount of arrays which cannot be foreseen. I want to access them und change their keys. I guess with a loop.

    Array
(
    [count] => 1
    [0] => Max, Mustermann
    [1] => Job
    [2] => Companyname
    [3] => IT
    [4] => CEO
    [5] => N610-611
    [6] => +49 (30) 111111
    [7] => +49 (30) 111111
    [8] => max@company.de
)
Array
(
    [count] => 1
    [0] => Alicia Keys
    [1] => Job
    [2] => Companyname
    [3] => IT
    [4] => CEO
    [5] => N610-N611
    [6] => +49 11111
    [7] => +49 11111
    [8] => alikey@company.de
)

I'd like to have an output like this:

    Array
(
    [count] => 1
    [Name] => Max, Mustermann
    [Jobname] => Job
    [Company] => Companyname
    [Division] => IT
    [CEO] => CEO
    [Room] => N610-611
    [Tel] => +49 (30) 111111
    [Fax] => +49 (30) 111111
    [E-Mail] => max@company.de
)
Array
(
    [count] => 1
    [Name] => Alicia Keys
    [Job] => Job
    [Company] => Companyname
    [Division] => IT
    [CEO] => CEO
    [Room] => N610-N611
    [Tel] => +49 11111
    [Fax] => +49 11111
    [E-Mail] => alikey@company.de
)

I'm not sure whether to use a foreach oder a for loop, or if a loop is even necessary. I know that changing keys is not that simple and read of a second array which holds the keys you want to change to. But I'm not sure how to do that

2 Answers2

3

You can use array_combine() along with foreach() with predefined keys arrays

//predefined keys array
$index_array = array('count','Name','Jobname','Company','Division','CEO','Room','Tel','Fax','E-Mail');

foreach($array as &$value){
   $value = array_combine($index_array ,$value);
}

Output:-https://3v4l.org/VnmWJ

Note:- If your array is single-dimensional then:

$array = array_combine($index_array ,$array);

Output:-https://3v4l.org/R8oY0

Explanation:-

foreach() because it take care about indexes as well as more readable. (save you from undefined index error happen multiple times using for() loop)

&$valueis passing by reference, so that any change in child array will reflect in the original/initial array automatically.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • 1
    I guess this is what I want, but I have to check this first. Can you explain to me why foreach and not for, and whats the point with &$value? –  Oct 30 '18 at 11:45
  • 1
    @RicardoMüller foreach is much more readable than for, but it is just a preference (you could use for if you want to). `&$value` is a [pointer](https://stackoverflow.com/a/746322/3435728) to the current element in the loop. Meaning that every change you make in its value will also change the element in the array – CarlosCarucce Oct 30 '18 at 11:49
1

If you have a 1D array and you want to change keys of this array. It's enough to define an array with new keys and change keys of array in a loop. I make a example in following snippets code:

<?php $arr = [
    'count' => 1,
    '0' => 'Max Mustermann',
    '1' => 'Job',
    '2' => 'Companyname',
    '3' => 'IT',
    '4' => 'CEO',
    '5' => 'N610-611',
    '6' => '+49 (30) 111111',
    '7' => '+49 (30) 111111',
    '8' => 'max@company.de',
];
$arr2 = ['count', 'Name', 'Jobname', 'Company', 'Division', 'CEO', 'Room', 'Tel', 'Fax', 'E-Mail'];  
    $index = 0;
  foreach($arr as $oldkey => $value) {
    $arr[$arr2[$index]] = $arr[$oldkey];
    unset($arr[$oldkey]);
    $index++;
  }
print_r($arr);

you can see output in this link

mshomali
  • 664
  • 1
  • 8
  • 27