Guest User

php arrays

a guest
Oct 30th, 2013
72
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $data = array(
  4.     array("1" => "12306"),
  5.     array("0" => "12106"),
  6.     array("1" => "12770"),
  7.     array("3" => "12770"),
  8.     array("3" => "12306"),
  9.     array("3" => "12770")
  10. );
  11.  
  12. $v = array_reverse(array_values($data));
  13.  
  14. $newdata = array();
  15.  
  16. foreach($data as $key => $value)
  17. {
  18.     $newdata[][current($value)] = key($value);
  19. }
  20.  
  21. $result = array();
  22. $ii = 0;
  23.  
  24. foreach($data as $key => $value)
  25. {
  26.     $i = 0;
  27.     $match = array();
  28.     $current = key($value);
  29.     echo 'For key #'. $current . " in ".'$data['."$ii".']'. " with value " . current($value) . "\n\n";
  30.     echo "KEY\tVALUE1\tVALUE2\tCOMPARE\n";
  31.     foreach($newdata as $ki => $vi)
  32.     {
  33.         $key = key($vi);
  34.         echo '#'. current($vi) . "\t " . $key . "\t ";
  35.         if(in_array($key, $value) && $key == current($value))
  36.         {
  37.             # if there are equal cases (same key, same value) these are overwritten,
  38.             # so if you get 3 matches then you see only those which are different by key,
  39.             # to me it seems to fit with the request...
  40.  
  41.             $match[current($vi)] = $key;
  42.             echo $key;
  43.         }
  44.  
  45.         else
  46.         {
  47.             echo 'n/f'; # not found
  48.         }
  49.  
  50.         if($key == current($value))
  51.         {
  52.             $i++;
  53.             echo "\t";
  54.             switch(true)
  55.             {
  56.                 case current($vi) > $current:
  57.                     echo "+";
  58.                     break;
  59.                 case current($vi) == $current:
  60.                     echo "=";
  61.                     break;
  62.                 case current($vi) < $current:
  63.                     echo "-";
  64.                     break;
  65.             }
  66.  
  67.         }
  68.         echo "\n";
  69.     }
  70.  
  71.     if($i == 1) echo "\t\t\tMatch: 1 ". json_encode($match, JSON_FORCE_OBJECT) ."\n";
  72.     if($i > 1) echo "\t\t\tMatches: $i " . json_encode($match, JSON_FORCE_OBJECT) . "\n";
  73.    
  74.     # get max key from matches:
  75.  
  76.     $maxkey = max(array_keys($match));
  77.    
  78.     if($current == $maxkey)
  79.     {
  80.         echo "\t\t\t".'Chosen key: #'.$maxkey . "\n";
  81.         $result[$ii][$maxkey] = $match[$maxkey];
  82.     }
  83.  
  84.     else
  85.     {
  86.         echo "\t\t\tDiscarded\n";
  87.     }
  88.  
  89.     echo "--------------------------------------------------------\n\n";
  90.     $ii++;
  91. }
  92. echo "RESULT:\n";
  93. print_r($result);
RAW Paste Data