ein Array von Zahlen befindet sich:Filtern eines Array von Zahlen entsprechend einem numerischen Muster
$list = array
(
[0] => 111
[1] => 112
[2] => 113
[3] => 114
[4] => 121
[5] => 122
[6] => 123
[7] => 124
[8] => 131
[9] => 132
[10] => 1234
[11] => 1123
[12] => 1223
[13] => 1233
[14] => 4321
)
und eine Variable (das Muster):
$input = 1231;
würde Ich mag das Array filtern, mit die folgenden Regeln. Sei $list[$i]
ein Gegenstand der $list
Reihe, $d
sei eine Ziffer von $list[$i]
. Dann
- , wenn die Anzahl der Ziffern gleich
$d
in$list[$i]
größer ist als die Anzahl von Ziffern gleich$d
in$input
, dann ist das Array-Element übersprungen werden muß; - Wenn in
$input
keine$d
Ziffer angegeben ist, muss das Array-Element übersprungen werden.
Zum Beispiel in den oben genannten $input
variable
1
erscheint zweimal,2
und3
einmal erscheinen.
Dann alles, was mehr als das erscheint sollte aus dem Array entfernt werden:
$list = array
(
[0] => 111 ==> should be removed (1 is only defined twice in $input, so it shouldn't appear more than twice)
[1] => 112
[2] => 113
[3] => 114 ==> should be removed (there is no 4)
[4] => 121
[5] => 122 ==> should be removed (2 is only defined once, so it shouldn't appear more than once)
[6] => 123
[7] => 124 ==> should be removed (there is no 4)
[8] => 131
[9] => 132
[10] => 1234 ==> should be removed (there is no 4)
[11] => 1123
[12] => 1223 ==> should be removed (2 is only defined once in $input, so it shouldn't appear more than once)
[13] => 1233 ==> should be removed (3 is only defined once in $input, so it shouldn't appear more than once)
[14] => 4321 ==> should be removed (there is no 4)
)
Wie kann ich das erreichen?
warum 111.112 .. entfernt werden sollte? –
Sie könnten etwas mit [substr_count] (http://php.net/manual/en/functionsubstrocount.php) und [array_filter] (http://php.net/manual/en/function.array) versuchen -filter.php) –
Warum in der Welt sollte '111' entfernt werden und' 123' bleibt? Was ist die Logik dahinter? Sie müssen uns den Grund angeben, nicht nur das gewünschte Ergebnis. – Irvin