Moin,
ich hoffe es wird nicht zu kompliziert.
Was will: ein Multidimensionales durchlaufen, und dabei das Unter-Array mit dem größten "Gewicht" finden.
Was für ein Gewicht?
// bsp 1
array(
array( // dieses array "gewinnt", da es den größten wert besitzt
'a' => 1
),
array(
'a' => 0
)
)
// bsp 2
array(
array(
'a' => 0
'b' => 1
),
array( // dieses array "gewinnt", da es den größten wert besitzt
'a' => 1
'b' => 1
)
)
Alles anzeigen
Per mitgegebenen Keys soll noch festgelegt werden können, welche Keys geprüft werden.
Die Reihenfolge der mitgegebenen Keys soll außerdem das "Gewicht" dieser Keys festlegen.
Zwischenfrage:
kennt jemand einen Algo / Funktion, die genau das o.ä. macht?
Was ich momentan hab und funktioniert:
<?php
$from = 0;
$upto = 1;
$i = 0;
$source = array(
'key' . ++$i => array(
'a' => mt_rand($from, $upto),
'b' => mt_rand($from, $upto),
'c' => mt_rand($from, $upto),
),
'key' . ++$i => array(
'a' => mt_rand($from, $upto),
'b' => mt_rand($from, $upto),
'c' => mt_rand($from, $upto),
),
'key' . ++$i => array(
'a' => mt_rand($from, $upto),
'b' => mt_rand($from, $upto),
'c' => mt_rand($from, $upto),
),
);
echo '<pre>';
$r = array_weigth($source, array('c', 'b', 'a'));
echo '<pre>source:<br>';
print_r($source);
echo '<hr>result:<br>';
echo 'compare_keys: array(\'c\', \'b\', \'a\')<br>';
print_r($r);
echo '<br>';
print_r($source[$r]);
function array_weigth(array $source, array $compare_keys=array()){
if(!function_exists('array_weigth_')){
function array_weigth_(array &$source, $source_key, $target_source_key, array &$compare_keys, $compare_key, $greater_before){
$current_source_val = $source[$source_key][$compare_keys[$compare_key]];
$current_target_val = $source[$target_source_key][$compare_keys[$compare_key]];
$greater = ($current_source_val > $current_target_val);
$greater_equal = ($current_source_val >= $current_target_val);
if(
($greater_equal === true or $greater_before === true)
and (
!isset($compare_keys[++$compare_key])
or array_weigth_(
$source, $source_key, $target_source_key, $compare_keys, $compare_key, $greater
) === true
)
){
return true;
}
return false;
}
}
if(count($compare_keys) === 0){
foreach($source as $array){
if(!is_array($array)){
return false;
}
$compare_keys = array_unique(array_merge($compare_keys, array_keys($array)));
}
if(count($compare_keys) === 0){
return false;
}
}
reset($source);
$target_source_key = key($source);
foreach(array_keys($source) as $source_key){
$target_source_key = (
array_weigth_($source, $source_key, $target_source_key, $compare_keys, 0, null) === true
) ? $source_key : $target_source_key;
}
return $target_source_key;
}
?>
Alles anzeigen
Bsp-Ausgabe:
source:
Array
(
[key1] => Array
(
[a] => 0
[b] => 1
[code=c] => 1
)
[key2] => Array
(
[a] => 0
[b] => 1
[code=c] => 0
)
[key3] => Array
(
[a] => 1
[b] => 1
[code=c] => 1
)
)
result:
compare_keys: array('c', 'b', 'a')
key3
Array
(
[a] => 1
[b] => 1
[code=c] => 1
)
Alles anzeigen
Ergebnis: ok.
key3 gewählt, da alle Felder 1 sind --was mehr als bei allen anderen ist.
Bsü-Ausgabe 2:
source:
Array
(
[key1] => Array
(
[a] => 0
[b] => 0
[code=c] => 1
)
[key2] => Array
(
[a] => 0
[b] => 1
[code=c] => 1
)
[key3] => Array
(
[a] => 1
[b] => 0
[code=c] => 1
)
)
result:
compare_keys: array('c', 'b', 'a')
key2
Array
(
[a] => 0
[b] => 1
[code=c] => 1
)
Alles anzeigen
Ergebnis: ok.
key2 gewählt, da:
keys angegeben in Reihenfolge: c, b, a (c hat das größte Gewicht, dann b, dann a) -- also wird auch erst c, dann b, dann a gecheckt
Bei allen ist c = 1, also kommen erstmal alle in Frage.
b ist aber nur bei key2 auf 1 gesetzt.
Auch wenn a bei key3 größer ist, "gewinnt" hier key2, da b mehr Gewicht hat als a.
(hoffe man versteht mich )
Frage:
Was mach ich bei Values die > 1 sind. Also zB 0-10?
Ist das folgende Ergebnis "gefühlt" in Ordnung, oder eher nicht?
source:
Array
(
[key1] => Array
(
[a] => 8
[b] => 4
[code=c] => 9
)
[key2] => Array
(
[a] => 1
[b] => 7
[code=c] => 7
)
[key3] => Array
(
[a] => 2
[b] => 9
[code=c] => 3
)
)
result:
compare_keys: array('c', 'b', 'a')
key1
Array
(
[a] => 8
[b] => 4
[code=c] => 9
)
Alles anzeigen
Test script hier (refresh für random values -- klar) Test Script