Accessing nested array keys by index


#1


How can I access the nested arrays' keys and rename them?
Like I have this array:

$array = [
  0 => [
	'platform' => 'COM', 
	'firstName' => 'Nick', 
	'lastName' => 'Jones'
  ], 
  1 => [
	'platform' => 'DE',
	'firstName' => 'Julia',
	'lastName' => 'Williams'
  ]
 1 => [
	'platform' => 'COM',
	'firstName' => 'Bruce',
	'lastName' => 'Smith'
  ]
]


And I want to have a new array that looks like this:

$newArray = [
  'COM' => [
      'Nick' => 'Jones',
      'Bruce' => 'Smith'
  ]
  'DE' => [
      'Julia' => 'Williams',
  ]
]

I've tried:

foreach ($array as $rowIndex => $row) {
  foreach ($row as $item) {
    $platform = $row[0];
    $firstName = $row[1];
    $lastName = $row[2];
  }
  $newArray[$platform][$firstName] = $lastName;
}

But I get an undefined offset Error


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.