0

I have two arrays:

The first one looks like this:

   Array: 8 [▼
      0 => array:17 [▼
        "id" => 4
        "firstname" => "Maria"
        "lastname" => "von Moszczenski"
        "slug" => "marianne-von-moszczenski"
        "email" => "marianne.von.moszczenski@gmail.com"
        "email_verified_at" => null
        "created_at" => "2019-11-21 17:34:53"
        "updated_at" => "2019-11-21 17:34:53"
        "deleted_at" => null
      ]
        1 => User {...}
        2 => User {...}
        3 => User {...}
        ... unknown number of elements
     ]

The second array looks like this:

array:8 [▼
  "Maria" => 17
  "Stefanie" => 2
  "Angela" => 3
  "Andrea" => 4
  "Michelle" => 5
  "Yvonne" => 6
  "Martina" => 7
  "Karolina" => 8
  ... unknown number of elements...
]

I want to merge both of them so, i get the following structure:

Array: 8 [▼
  0 => array:17 [▼
    "id" => 4
    "firstname" => "Maria"
    "lastname" => "von Moszczenski"
    "slug" => "marianne-von-moszczenski"
    "email" => "marianne.von.moszczenski@gmail.com"
    "email_verified_at" => null
    "created_at" => "2019-11-21 17:34:53"
    "updated_at" => "2019-11-21 17:34:53"
    "deleted_at" => null
    "total" => 17  // <------ this is the new field
    ... unknown number of elements...
  ]
    1 => User {...}
    2 => User {...}
    3 => User {...}
 ]

So i did this:

 foreach ($totalsPerEmployee as $key => $total) {
    foreach ($users as $user) { 
        if ($user['firstname'] === $key) {
            $user['totalappointments'] = $total;  // if I stop and echo hier I see the new field
        }
    }
}

If I echo the result of the foreach in the middle, I see that the new field is added, but once the two foreach end, the new field was not added.

My questions is:

This method looks to error prone or if the arrays get too big could lead to performance issues.

Is there any other way to map/combine/intersect or whatever these two arrays mapping one attribute (firstname) in the first one against the key of the second one?

if there is not any "magic" method and I have to make the two loops. What am I doing wrong?

Rafael Munoz
  • 636
  • 1
  • 7
  • 27

1 Answers1

2

You can turn the loop round, you already have the totals array indexed by the user name, so just loop over the user array and if there is a value for it, then add it in...

foreach ($users as &$user) {
    if (isset($totalsPerEmployee[$user['firstname']])) {
        $user['totalappointments'] = $totalsPerEmployee[$user['firstname']];
    }
}
unset($user);

Also to ensure that you update the original array, using &$user uses a reference to the data, otherwise you (as you've found) update a copy of the data. Just to make sure - use unset($user) after the loop in case.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Thanks for the answer. It works, could you explain the "&" in the foreach? ->foreach ($users as &$user) . I took it it away it does not work. – Rafael Munoz Nov 24 '19 at 17:34
  • I've added some more explanation - it's called a reference and it allows you to modify the original object rather than a copy. https://stackoverflow.com/questions/25835856/php-foreach-change-array-value has some more explanation. – Nigel Ren Nov 24 '19 at 17:39
  • One more question, the last element of the array looks like this: 7 => & array:18. is there any way to get rid of that "&" symbol? – Rafael Munoz Nov 24 '19 at 17:57