So i have array with numbers, and my task is to call function that inserts this value - this number appears x times.
I know how to do it with using foreach function twice, but i dont understand it this way.
<?php
$sifra=array(100,200,100,90,30,200,44,100,90);
function Funkcija($sifra){
$nove_sifre=array();
foreach($sifra as $k => $v){
if(isset($nove_sifre[$v])){
$nove_sifre[$v]++;
}
else{
$nove_sifre[$v]=1;
}
}
return $nove_sifre;
}
$razlicitesifre= Funkcija($sifra);
foreach ($razlicitesifre as $k => $v){
echo "<br>Šifra ".$k." se ponavlja ".$v." puta.";
}
?>
so unside function Funkcija there is $nove_sifre =array();
and it loops thru $sifra which is array and searches $k =>$v, i get that part
but then if(isset($nove_sifre[$v])){
$nove_sifre[$v]++;
}
else{
$nove_sifre[$v]=1;
}
}
return $nove_sifre;
}
i dont get that part, so if value is in $nove_sifre, which is empty array, increase value by 1, else display it as 1
but $nove_sifre is empty array so there is no value in first point, and second, if value is increased by 1, it wont count how many times that number is displayed, it would increase 100 to 101, an so on?
But the result is valid so thats what i dont get in this case.
How is everything above giving validd result if $nove_sifre is empty andd $v which arre numbers, increases by 1? and not the counter that counts how much times numbeer appears?
I know how to do it with using foreach function twice, but i dont understand it this way.
<?php
$sifra=array(100,200,100,90,30,200,44,100,90);
function Funkcija($sifra){
$nove_sifre=array();
foreach($sifra as $k => $v){
if(isset($nove_sifre[$v])){
$nove_sifre[$v]++;
}
else{
$nove_sifre[$v]=1;
}
}
return $nove_sifre;
}
$razlicitesifre= Funkcija($sifra);
foreach ($razlicitesifre as $k => $v){
echo "<br>Šifra ".$k." se ponavlja ".$v." puta.";
}
?>
so unside function Funkcija there is $nove_sifre =array();
and it loops thru $sifra which is array and searches $k =>$v, i get that part
but then if(isset($nove_sifre[$v])){
$nove_sifre[$v]++;
}
else{
$nove_sifre[$v]=1;
}
}
return $nove_sifre;
}
i dont get that part, so if value is in $nove_sifre, which is empty array, increase value by 1, else display it as 1
but $nove_sifre is empty array so there is no value in first point, and second, if value is increased by 1, it wont count how many times that number is displayed, it would increase 100 to 101, an so on?
But the result is valid so thats what i dont get in this case.
How is everything above giving validd result if $nove_sifre is empty andd $v which arre numbers, increases by 1? and not the counter that counts how much times numbeer appears?