2

Here is my code, and it is not removing $arr[5] element so that I am trying to remove strings starting with # from my array

this is code

<?php
    $arr = [
        '#EXTM3U',
        '#EXTINF:177,Paul Dateh & Oren Yoel - Be More',
        'Be More.mp3',
        '#EXTINF:291,Christopher Toy - Just Because',
        'Just Because.mp3',
        '#EXTINF:238,Magnetic North - Drift Away',
        'Drift Away.mp3'
    ];
    for ($i = 0; $i <= count($arr); $i++) {
        if ($arr[$i]{0} == '#') {
            echo $arr[$i] . "\n";
            unset($arr[$i]);
        }
    }
    print_r($arr); 
?>
1
  • Alisherbek Rakhimov check the answer below Commented Apr 28, 2018 at 8:56

3 Answers 3

3

Reason:- You are counting array length inside the loop and every time when any value got unset() from the array, length of array decreased and value of count($array) changed (simply decreased)

So logically your 5th and 6th element never goes through if condition (they never get traversed by loop because of decreasing length of the array )

Solution 1:- Put count outside and it will work properly:-

$count = count($arr);

//loop start from 0 so use < only otherwise, sometime you will get an undefined index error

    for ($i = 0; $i < $count; $i++) { 
        if ($arr[$i]{0} == '#') {
            //echo $arr[$i] . "\n";
            unset($arr[$i]);
        }
    }
    print_r($arr);

Output:-https://eval.in/996494

Solution 2:- That's why i prefer foreach() over for() loop

 foreach($arr as $key=> $ar){
        if ($ar[0] == '#') {
            unset($arr[$key]);
        }
    }
    print_r($arr); 

Output:-https://eval.in/996502

Sign up to request clarification or add additional context in comments.

Comments

1

more spacific :

for ($i = 0; $i < count($arr); $i++) {

    if (strpos($arr[$i], '#') !== false) {
     echo "<br/>";
    } else {
        echo $arr[$i]."<br/>";
    }
}

2 Comments

Code is always good, but an explanation helps people work out if/how your answer solves the problem.
if condition will check that your string contain # then it break out from loop otherwise it will print your desire output.There is no rocket logic in it...
1

Try to use additional array to push right values. You calc count($arr); each iteration and when you do count($arr); your array gets smaller and count($arr); returns smaller values, so last elements won't be comparing, try to use variable to calc count before loop make changes:

<?php 
    //...
    $start_count = count($arr);
    for ($i = 0; $i <= $start_count; $i++) {
        if ($arr[$i]{0} == '#') {
            echo $arr[$i] . "\n";
            unset($arr[$i]);
        }
    }

Or remove bad element with a help of additional array, put good elements in new array and don't delete them from input array:

<?php

$arr = [
        '#EXTM3U',
        '#EXTINF:177,Paul Dateh & Oren Yoel - Be More',
        'Be More.mp3',
        '#EXTINF:291,Christopher Toy - Just Because',
        'Just Because.mp3',
        '#EXTINF:238,Magnetic North - Drift Away',
        'Drift Away.mp3'
    ];
    $cleared_from_mess_array = array();
    for ($i = 0; $i <= count($arr); $i++) {
        if ($arr[$i]{0} != '#') 
        {           
            array_push($cleared_from_mess_array,$arr[$i]);
        }
    }
    print_r($cleared_from_mess_array);

exit;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.