Lunatic Fnatic
Lunatic Fnatic

Reputation: 681

How to get highest number while in foreach loop

I have a function that returns mp3 list with its qualities. I want to return HD value true if I reach the last element in the array but i have a problem in foreach loop. It always returns true.

Code

   function funcName() {
    foreach($dirs as $d) {
        if (filesize($d) > 200) {
            $qualities = substr(strrchr(basename($d), "-"), 1);
            $qualities = preg_replace('/\\.[^.\\s]{3,4}$/', '', $qualities);
            // This is where I check whether it is the last element or not.
            $numItems = count($dirs);
            $i = 0;
            foreach($dirs as $key => $value) {
                if (++$i === $numItems) {
                    $zaa = true;
                } else {
                    $zaa = false;
                }
            }
            $files[] = ["files" => basename($d), "qualities" => $qualities, "hd" => $zaa];
        }
    }
    return ($files);
}

I just want to return $zaa = true if I reach to the last element. In my code, it returns true all the time.

Could you please show me in whic part am I failing?

Upvotes: 0

Views: 253

Answers (5)

Lunatic Fnatic
Lunatic Fnatic

Reputation: 681

Both of the answers are correct below, but I came up with this solution seems faster;

 if ($d === end($dirs)) {
    $zaa = true;
 } else {
    $zaa = false;
 }

Upvotes: 0

NoOorZ24
NoOorZ24

Reputation: 3257

I guess this should work:

function funcName() {

$numItems = count($dirs);
$i = 0;
foreach($dirs as $d) {
$i++;
    if (filesize($d) > 200) {
        $qualities = substr(strrchr(basename($d), "-"), 1);
        $qualities = preg_replace('/\\.[^.\\s]{3,4}$/', '', $qualities);         
        if ($i == $numItems) {
            $zaa = true;
        } else {
            $zaa = false;
        }            
        $files[] = ["files" => basename($d), "qualities" => $qualities, "hd" => $zaa];
    }
}
return ($files);
}

Upvotes: 1

Alisson Reinaldo Silva
Alisson Reinaldo Silva

Reputation: 10705

You don't have to loop $dirs again, just do it like this:

function funcName() {
    $i = 1;
    $numItems = count($dirs);
    foreach($dirs as $d) {
        $i++;
        $zaa = $i === $numItems;
        if (filesize($d) > 200) {
            $qualities = substr(strrchr(basename($d), "-"), 1);
            $qualities = preg_replace('/\\.[^.\\s]{3,4}$/', '', $qualities);
            // This is where I check whether it is the last element or not.
            $numItems = count($dirs);

            $files[] = ["files" => basename($d), "qualities" => $qualities, "hd" => $zaa];
        }
    }
    return ($files);
}

Upvotes: 1

NoOorZ24
NoOorZ24

Reputation: 3257

Use for loop instead , if you are using count() and all those statements it's just adding useless lines to your code, for(){} will be shorter and less complex

Upvotes: 0

Asif Uddin
Asif Uddin

Reputation: 409

you can change your condition by below condition. if your key started with 0 then you need to (sizeof($dirs) - 1) to get the total array element.

 if(sizeof($dirs) == $key)

Upvotes: 0

Related Questions