2017-06-28 3 views
-1

Ich habe Array, die eine Punktzahl und ID, die aus anderen Funktionen berechnet, und ich habe Benutzerinformationen, die aus der DB versucht.Push zu einem mehrdimensionalen Array in PHP

In beiden Array-IDs sind die gleichen Wie kann ich sie auf ein Array schieben?

Score Array

Array 
(
    [0] => Array 
     (
      [id] => 85 
      [total_cnt] => 2006 
     ) 

    [1] => Array 
     (
      [id] => 86 
      [total_cnt] => 1014 
     ) 

    [2] => Array 
     (
      [id] => 92 
      [total_cnt] => 6 
     ) 

    [3] => Array 
     (
      [id] => 93 
      [total_cnt] => 6 
     ) 
) 

Benutzer Info

Array 
(
    [0] => Array 
     (
      [id] => 52 
      [user_phone] => 00000000 
      [user_email] => [email protected] 
      [user_name] => yahoo 
      [user_picture] =>FG6K7Z3XTc.Pic.jpg 
      [user_post_hour] => 24 
      [user_is_block] => 1 
      [user_reg_date] => 2017-05-16 13:52:35 
     ) 

    [1] => Array 
     (
      [id] => 78 
      [user_phone] => 000000001 
      [user_email] => [email protected] 
      [user_name] => google 
      [user_picture] =>XqWKSDVci.Pic.jpg 
      [user_post_hour] => 24 
      [user_is_block] => 0 
      [user_reg_date] => 2017-05-16 13:52:35 

     ) 
) 

My Desire Ausgabe

Array 
    (
     [0] => Array 
      (
       [id] => 86 <--Same ID in both arrays 
       [user_phone] => 00000000 
       [user_email] => [email protected] 
       [user_name] => yahoo 
       [user_picture] =>FG6K7Z3XTc.Pic.jpg 
       [user_post_hour] => 24 
       [user_is_block] => 1 
       [user_reg_date] => 2017-05-16 13:52:35 

       [total_cnt] => 1014 <-- first array field added 
      ) 

Ich möchte eine optim ized Code und ich werde nicht Schleife verwendet für diesen

Danke für Ihre Hilfe

Antwort

0

Update:

Ein viel besserer Ansatz scheint zu sein, "array_column":

$cnts = array_column($scores, 'total_cnt', 'id'); 
foreach ($userInfo as $key => $item) { 
    $userInfo[$key]['total_cnt'] = $cnts[$item['id']]; 
} 

Ich habe einige "naiven" Benchmark-Tests unter Verwendung von Mikrozeit() und Testdaten wie Ihre Arrays:

Ausführungszeiten: 10000 Artikel in beiden Arrays: array_column 0,005 s vs 0.85s foreach

20000 Artikel in beiden Arrays: array_column 0.011s vs 18s foreach

Ursprüngliche Antwort:

Sie auch foreach-Schleifen wie diese verwenden:

foreach ($userInfo as $userKey => $item) { 
    foreach ($scores as $scoreKey => $score) { 
     if ($score['id'] == $item['id']) { 
      $userInfo[$userKey]['total_cnt'] = $score['total_cnt']; 
      unset($scores[$scoreKey]); 
      break; 
     } 
    } 
} 

Die ungesetzt innerhalb der zweiten Schleife " entfernt den bearbeiteten Score aus dem Array $ scores, um die Anzahl der Iterationszyklen im nächsten Lauf zu reduzieren. Bitte beachten Sie, dass das $ scores-Array danach leer bleibt, vielleicht eine Kopie davon erstellt und damit arbeitet.

+0

NEIN, Es ist ein schlechter Weg, weil die Ausführungszeit des Codes zu hoch ist –

+0

funktioniert leider nicht –

+0

Warum nicht? Arbeitsbeispiel: https://eval.in/823485 – user1915746

Verwandte Themen