Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] Zamiana wartosci i wyswietlenie tylko jednej
Forum PHP.pl > Forum > PHP
AndyPSV
Jak zrobic by jesli _from jest rowne _to badz _to jest rowne _from --- by wybral tylko ten \z ID wyzszym?

  1. 0 =>
  2. 'id' => '8',
  3. '_from' => '2',
  4. '_to' => '1',
  5. 'date' => '2018-10-15 15:51:07',
  6. 'message' => 'ccccccxxxxx',
  7. 'read' => '0',
  8. 'feedback' => '0',
  9. 'cnt' => '3',
  10. ),
  11. 1 =>
  12. 'id' => '6',
  13. '_from' => '1',
  14. '_to' => '2',
  15. 'date' => '2018-10-15 15:47:01',
  16. 'message' => 'zzzzzzz1',
  17. 'read' => '1',
  18. 'feedback' => '0',
  19. 'cnt' => '1',
  20. ),*/

werdan
Poniżej działający przykład:

  1.  
  2. <?php
  3.  
  4.  
  5. $a = [
  6. [
  7. 'id' => '8',
  8. '_from' => '2',
  9. '_to' => '2',
  10. 'date' => '2018-10-15 15:51:07',
  11. 'message' => 'ccccccxxxxx',
  12. 'read' => '0',
  13. 'feedback' => '0',
  14. 'cnt' => '3',
  15. ],
  16. [
  17. 'id' => '6',
  18. '_from' => '2',
  19. '_to' => '2',
  20. 'date' => '2018-10-15 15:47:01',
  21. 'message' => 'zzzzzzz1',
  22. 'read' => '1',
  23. 'feedback' => '0',
  24. 'cnt' => '1',
  25. ]
  26. ];
  27.  
  28.  
  29. $b = array_filter($a, function($row){
  30. return ($row['_from'] == $row['_to']);
  31. });
  32.  
  33. print_r($b);
  34.  
  35. function sort_arr($item_1, $item_2)
  36. {
  37. return $item_2['time'] - $item_1['time'];
  38. }
  39.  
  40. usort($b, function($x, $y){
  41.  
  42. return $y['id'] - $x['id'];
  43. });
  44.  
  45. $max = null;
  46. if(count($b)){
  47. $max = reset($b);
  48. }
  49.  
  50. print_r($max);
  51.  
  52.  
AndyPSV
Nie dziala mi ten przyklad

tu jest przyklad danych z $pm;

  1. 0 =>
  2. 'id' => '8',
  3. '_from' => '2',
  4. '_to' => '1',
  5. 'date' => '2018-10-15 15:51:07',
  6. 'message' => 'ccccccxxxxx',
  7. 'read' => '1',
  8. 'feedback' => '0',
  9. 'cnt' => '3',
  10. ),
  11. 1 =>
  12. 'id' => '6',
  13. '_from' => '1',
  14. '_to' => '2',
  15. 'date' => '2018-10-15 15:47:01',
  16. 'message' => 'zzzzzzz1',
  17. 'read' => '1',
  18. 'feedback' => '0',
  19. 'cnt' => '1',
  20. ),
  21. 2 =>
  22. 'id' => '4',
  23. '_from' => '3',
  24. '_to' => '1',
  25. 'date' => '2018-10-15 15:20:47',
  26. 'message' => 'aaaaaaaaaa1e2642d888b14f0df948682041e48fa31391081110-480-272-301....flv
  27. 1e2642d888b14f0df948682041e48fa31391081110-480-272-301-h264.flv play
  28. energy 2000 stare ale nadal jare-).mp3
  29. lil peep - benz truck (prod. smokeasac).mp3
  30. NAJWAZNIEJSZA,+websites.2013-05-31vPACK-ALL.rar
  31. httpd-WEBPAGES,2014-11-24v1-PASS.PROTECTED.rar
  32. Gang Albanii-Narkotykowy odlot (Instrumental).mp3
  33. Gang Albanii-Blachary (Instrumental).mp3
  34. 1e2642d888b14f0df948682041e48fa31391081110-480-272-301....flv
  35. 1e2642d888b14f0df948682041e48fa31391081110-480-272-301-h264.flv play
  36. energy 2000 stare ale nadal jare-).mp3
  37. lil peep - benz truck (prod. smokeasac).mp3
  38. NAJWAZNIEJSZA,+websites.2013-05-31vPACK-ALL.rar
  39. httpd-WEBPAGES,2014-11-24v1-PASS.PROTECTED.rar
  40. Gang Albanii-Narkotykowy odlot (Instrumental).mp3
  41. Gang Albanii-Blachary (Instrumental).mp3
  42. 1e2642d888b14f0df948682041e48fa31391081110-480-272-301....flv
  43. 1e2642d888b14f0df948682041e48fa31391081110-480-272-301-h264.flv play
  44. energy 2000 stare ale nadal jare-).mp3
  45. lil peep - benz truck (prod. smokeasac).mp3
  46. NAJWAZNIEJSZA,+websites.2013-05-31vPACK-ALL.rar
  47. httpd-WEBPAGES,2014-11-24v1-PASS.PROTECTED.rar
  48. Gang Albanii-Narkotykowy odlot (Instrumental).mp3
  49. Gang Albanii-Blachary (Instrumental).mp3
  50. 1e2642d888b14f0df948682041e48fa31391081110-480-272-301....flv
  51. 1e2642d888b14f0df948682041e48fa31391081110-480-272-301-h264.flv play
  52. energy 2000 stare ale nadal jare-).mp3
  53. lil peep - benz truck (prod. smokeasac).mp3
  54. NAJWAZNIEJSZA,+websites.2013-05-31vPACK-ALL.rar
  55. httpd-WEBPAGES,2014-11-24v1-PASS.PROTECTED.rar
  56. Gang Albanii-Narkotykowy odlot (Instrumental).mp3
  57. Gang Albanii-Blachary (Instrumental).mp3',
  58. 'read' => '1',
  59. 'feedback' => '0',
  60. 'cnt' => 7,
  61. ),
  62. )
AndyPSV
Chodzi o to by OUTPUT byl taki:

  1. 0 =>
  2. 'id' => '8',
  3. '_from' => '2',
  4. '_to' => '1',
  5. 'date' => '2018-10-15 15:51:07',
  6. 'message' => 'ccccccxxxxx',
  7. 'read' => '0',
  8. 'feedback' => '0',
  9. 'cnt' => '3',
  10. ),


z wysszym ID jeden klucz co jest taki sam _from jak _to
AndyPSV
  1. $newArray = '';
  2. foreach ($pm as $item) {
  3. $uniqueRecordKey = $item['_from'].'-'.$item['_to'];
  4. $oppositeRecordKey = $item['_to'].'-'.$item['_from'];
  5.  
  6. //If exists record from the opposite and new ID is greater than previous put
  7. if (isset($newArray[$oppositeRecordKey])) {
  8. $oppositeRecordKey[$uniqueRecordKey] = $item;
  9. continue; //Do not append to the end
  10. }
  11.  
  12. $newArray[$uniqueRecordKey] = $item;
  13. }
  14.  
  15. $pm = $newArray;
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.