Php array vraagjes.

Gestart doorAlemaalte17 reacties29 weergaven

18 berichten · pagina 1 van 2

Alemaalte

Lid · Topicstarter

Berichten
5.957
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Hey,

Ik ben bezig met een script waarin ik veel met arrays ga moeten werken, nu zoek ik een paar functies maar ik vind ze niet goed.

1) Kijken of een getal x keer voorkomt.
Ik heb bv een array met 4,8,8,7,3. Nu zou ik moeten weten of er een getal 2 keer voorkomt. Is daar een functie voor?

2) Kijken of getallen elkaar opvolgen.
Ik heb een array met 4,5,6,7,8 nu moet ik weten of die getallen elkaar opvolgen, is daar ook een functie voor?

Mvg,
alemaalte
0
Berichten
6.485
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
1. voor zover ik weet niet, kan wel alle dubbele verwijderen met array_unique() .

anders doe je gewoon:

[COLOR= #0000BB]<?php
$numbers[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]1[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]2[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]2[/COLOR][COLOR= #007700]);[/COLOR]

[COLOR= #0000BB]$uniquenumbers[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]

[COLOR= #0000BB]$doublenumbers[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]

[URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$numbers[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$number[/COLOR][COLOR= #007700]) {[/COLOR]
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/in_array"]in_array[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$number[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$uninquenumbers[/COLOR][COLOR= #007700])) {[/COLOR]
        [COLOR= #0000BB]$uniquenumbers[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #0000BB]$number[/COLOR][COLOR= #007700];
    }[/COLOR]
    [URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]
        [COLOR= #0000BB]$doublenumbers[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #0000BB]$number[/COLOR][COLOR= #007700];
    }
}[/COLOR]
?>


Dit werkt helaas alleen door dubbele en niet voor 3 dubbele enzo.

Voor 2de is voor zover ik weet alleen de functie OM ze op volgorde te krijgen niet om te controleren of het al zo is. (misschien == true erachter zetten?) Namelijk sort().
0
Fass

Lid

Berichten
3.771
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
1) $antwoord wordt TRUE (als er iets $nummer keer voorkomt) of FALSE (als er niets zovaak voorkomt) in de array $array
Let op: Als je $nummer dus 2 is en er komt iets 3 keer voor, geeft het dus FALSE. Bedoel je dit niet, dan zal ik het veranderen

$antwoord = in_array($nummer, array_count_values($array), true);


2) Ik weet niet of je arrays zo in een dinges mag zetten maar je kunt het proberen. Het $antwoord is weer TRUE of FALSE en je array heet weer $array

$antwoord = (sort($array) == $array ? TRUE : FALSE);


Als dit niet werkt moet je forren.

$sorted = sort($array);
$antwoord = TRUE;
for($i = 0; $h < count($array) ; $i++) {
    if ($array[$i] != $sorted[$i]) {
        $antwoord = FALSE;
        break;
    }
}
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Als ik je 2e vraag goed begrijp moet je dit hebben:

[COLOR= #0000BB]<?php

$array[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]4[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]5[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]6[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]7[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]8[/COLOR][COLOR= #007700]);

function[/COLOR] isOpvolgend[COLOR= #007700]([/COLOR][COLOR= #0000BB]$arrGetallen[/COLOR][COLOR= #007700])
{[/COLOR]
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR] [URL="http://www.php.net/is_array"]is_array[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$arrGetallen[/COLOR][COLOR= #007700]) &&[/COLOR] [URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$arrGetallen[/COLOR][COLOR= #007700]) !=[/COLOR] [COLOR= #0000BB]0[/COLOR] [COLOR= #007700])
    {[/COLOR]
        [URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$arrGetallen[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$key[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]$value[/COLOR][COLOR= #007700])
        {[/COLOR]
            [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]( ([/COLOR][COLOR= #0000BB]$value[/COLOR] [COLOR= #007700]+[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700]) == ([/COLOR][COLOR= #0000BB]$arrGetallen[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$key[/COLOR] [COLOR= #007700]+[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700]]) )
            {[/COLOR]
                [COLOR= #FF8000]// do nothing    [/COLOR]
            [COLOR= #007700]}[/COLOR]
            [URL="http://www.php.net/else"]else[/URL]
            [COLOR= #007700]{[/COLOR]
                [URL="http://www.php.net/return"]return[/URL] false[COLOR= #007700];[/COLOR] [COLOR= #FF8000]// return false, thus stopping the function.[/COLOR]
                [URL="http://www.php.net/break"]break[/URL][COLOR= #007700];[/COLOR] [COLOR= #FF8000]// just a fail-safe    [/COLOR]
            [COLOR= #007700]}
        }[/COLOR]
        
        [URL="http://www.php.net/return"]return[/URL] true[COLOR= #007700];
    }[/COLOR]
    [URL="http://www.php.net/else"]else[/URL]
    [COLOR= #007700]{[/COLOR]
        [URL="http://www.php.net/return"]return[/URL] false[COLOR= #007700];[/COLOR] [COLOR= #FF8000]// input not array    [/COLOR]
    [COLOR= #007700]}
}[/COLOR]

[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR] isOpvolgend[COLOR= #007700]([/COLOR][COLOR= #0000BB]$array[/COLOR][COLOR= #007700]) ) {[/COLOR] [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]SUCCES![/COLOR][COLOR= #007700]; }[/COLOR]

?>
0
Alemaalte

Lid · Topicstarter

Berichten
5.957
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Bij het tweede mag het ook bv. 5,4,7,6,3 zijn, zolang die dan maar er in zitten, maar dat los ik met rik zijn code wel op, alleen voor dat eerste weet ik nog niet hoe ik het ga doen.
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
In dat geval moet je ook sort() toevoegen. Als vanmiddag nog niemand heeft gereageerd maak ik voor de eerste ook wel een functie. Of je doet hrt zelf. Je moet array_count_values gebruiken, en dan van de array die je terug krijgt elke key apart checken of value groter dan 1 is. Zoja-> retrun false. Zonee-> retrun true.
0
Alemaalte

Lid · Topicstarter

Berichten
5.957
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Dat van die sort ging ik al doen biggrinDat tweede snap ik niet goed, dat doet niet wat ik wil?
0
Berichten
35.796
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Welke moet je nog? Alleen deze?

"2) Kijken of getallen elkaar opvolgen.
Ik heb een array met 4,5,6,7,8 nu moet ik weten of die getallen elkaar opvolgen, is daar ook een functie voor?"

hm nee die heeft Rik net gemaakt
0
Alemaalte

Lid · Topicstarter

Berichten
5.957
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Nee, ik heb ze beiden niet, de eerste niets, bij de tweede denk ik er net aan dat het bij 2 getallen niet moet. Dus 1,5,6,7,8,9,1532 is ook goed.
0
Berichten
35.796
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Huh?

Die eerste snap ik ook niet: "Kijken of een getal x keer voorkomt.
Ik heb bv een array met 4,8,8,7,3. Nu zou ik moeten weten of er een getal 2 keer voorkomt. Is daar een functie voor?"
Je wil dus een functie die je aanroept met als argument een getal A, en die dan teruggeeft hoe vaak dat getal voorkomt ofzo? Of alleen een TRUE als getal A > 1x voorkomt?
0
Alemaalte

Lid · Topicstarter

Berichten
5.957
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Nee, ik wil dit:
double($array);
triple($array);
qaud($array);

Stel een getal komt 2 keer voor dan geeft double true, 3 keer dan geeft triple true, maar double niet meer. 4 keer, dan geeft enkel qaud true.
0
Berichten
35.796
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
En het maakt dan niet uit welk getal dat is?
0
Alemaalte

Lid · Topicstarter

Berichten
5.957
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Nee.
0
Berichten
35.796
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
[COLOR= #0000BB]<?php
$array[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]4[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]8[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]8[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]7[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]3[/COLOR][COLOR= #007700]);

function[/COLOR] komtVoor[COLOR= #007700]([/COLOR][COLOR= #0000BB]$array[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$hoevaak[/COLOR][COLOR= #007700])
{[/COLOR]
    
    [COLOR= #0000BB]$tmp[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]
    
    [URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$array[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$a[/COLOR][COLOR= #007700])
    {[/COLOR]
        
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$tmp[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$a[/COLOR][COLOR= #007700]]))[/COLOR]
            [COLOR= #0000BB]$tmp[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$a[/COLOR][COLOR= #007700]]++;[/COLOR]
        [URL="http://www.php.net/else"]else[/URL]
            [COLOR= #0000BB]$tmp[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$a[/COLOR][COLOR= #007700]] =[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700];
        
    }[/COLOR]
    
    [URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$tmp[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$val[/COLOR][COLOR= #007700])
    {[/COLOR]
        
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$val[/COLOR] [COLOR= #007700]==[/COLOR] [COLOR= #0000BB]$hoevaak[/COLOR][COLOR= #007700])[/COLOR]
            [URL="http://www.php.net/return"]return[/URL] true[COLOR= #007700];
            
    }[/COLOR]
    
    [URL="http://www.php.net/return"]return[/URL] false[COLOR= #007700];
    
}[/COLOR]

[URL="http://www.php.net/var_dump"]var_dump[/URL][COLOR= #007700]([/COLOR]komtVoor[COLOR= #007700]([/COLOR][COLOR= #0000BB]$array[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700]));[/COLOR]
[URL="http://www.php.net/var_dump"]var_dump[/URL][COLOR= #007700]([/COLOR]komtVoor[COLOR= #007700]([/COLOR][COLOR= #0000BB]$array[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]2[/COLOR][COLOR= #007700]));[/COLOR]
[URL="http://www.php.net/var_dump"]var_dump[/URL][COLOR= #007700]([/COLOR]komtVoor[COLOR= #007700]([/COLOR][COLOR= #0000BB]$array[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]3[/COLOR][COLOR= #007700]));[/COLOR]


true
true
false
0
Alemaalte

Lid · Topicstarter

Berichten
5.957
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Uhm, die code snap ik nog niet zo meteen, ik zal er zo even naar kijken.

*edit*

Ik snap het, bedankt.
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren