SHARE
TWEET

siang all.. ada yg tau cara membuat array baru hasil filter

robbyirwanto Aug 23rd, 2019 (edited) 106 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Demo</title>
  6. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css"/>
  7. </head>
  8. <body>
  9.  
  10. <?php
  11. //sample
  12. $data1 = '["a1","a3"]';
  13. $data2 = '[
  14.     {
  15.         "code" : "a1",
  16.         "name" : "aaa"
  17.     }, {
  18.         "code" : "a2",
  19.         "name" : "bbb"
  20.     }, {
  21.         "code" : "a3",
  22.         "name" : "ccc"
  23.     }
  24. ]';
  25.  
  26.  
  27. $array1 = json_decode( $data1, true );
  28. $array2 = json_decode( $data2, true );
  29. $array3=[];
  30.  
  31. echo "<pre class='bg-primary p-2 text-light'>";
  32. print_r($array1);
  33. echo "</pre>";
  34.  
  35. echo "<pre class='bg-warning p-2'>";
  36. print_r($array2);
  37. echo "</pre>";
  38.  
  39. foreach( $array2 as $arr2 ) {
  40.     foreach( $array1 as $arr1 ) {
  41.         if( $arr2['code'] == $arr1 ){
  42.             $array3[] = $arr2;
  43.         }
  44.     }
  45. }
  46.  
  47. echo "<pre class='bg-info p-2'>";
  48. print_r($array3);
  49. echo "</pre>";
  50. ?>
  51.  
  52. </body>
  53. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top