Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $data = array(
- array("1" => "12306"),
- array("0" => "12106"),
- array("1" => "12770"),
- array("3" => "12770"),
- array("3" => "12306"),
- array("3" => "12770")
- );
- $v = array_reverse(array_values($data));
- $newdata = array();
- foreach($data as $key => $value)
- {
- $newdata[][current($value)] = key($value);
- }
- $result = array();
- $ii = 0;
- foreach($data as $key => $value)
- {
- $i = 0;
- $match = array();
- $current = key($value);
- echo 'For key #'. $current . " in ".'$data['."$ii".']'. " with value " . current($value) . "\n\n";
- echo "KEY\tVALUE1\tVALUE2\tCOMPARE\n";
- foreach($newdata as $ki => $vi)
- {
- $key = key($vi);
- echo '#'. current($vi) . "\t " . $key . "\t ";
- if(in_array($key, $value) && $key == current($value))
- {
- # if there are equal cases (same key, same value) these are overwritten,
- # so if you get 3 matches then you see only those which are different by key,
- # to me it seems to fit with the request...
- $match[current($vi)] = $key;
- echo $key;
- }
- else
- {
- echo 'n/f'; # not found
- }
- if($key == current($value))
- {
- $i++;
- echo "\t";
- switch(true)
- {
- case current($vi) > $current:
- echo "+";
- break;
- case current($vi) == $current:
- echo "=";
- break;
- case current($vi) < $current:
- echo "-";
- break;
- }
- }
- echo "\n";
- }
- if($i == 1) echo "\t\t\tMatch: 1 ". json_encode($match, JSON_FORCE_OBJECT) ."\n";
- if($i > 1) echo "\t\t\tMatches: $i " . json_encode($match, JSON_FORCE_OBJECT) . "\n";
- # get max key from matches:
- $maxkey = max(array_keys($match));
- if($current == $maxkey)
- {
- echo "\t\t\t".'Chosen key: #'.$maxkey . "\n";
- $result[$ii][$maxkey] = $match[$maxkey];
- }
- else
- {
- echo "\t\t\tDiscarded\n";
- }
- echo "--------------------------------------------------------\n\n";
- $ii++;
- }
- echo "RESULT:\n";
- print_r($result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement