1

I am getting the values of an array of checkboxes (created dynamically). There were several issues, but I narrowed it to a certain point. Now the abstract of the problem is this (forget for a second these are checkboxes and just imagine we are working with an array of numbers).

I have an array store[20] which contains values of 0s or 1s. I need to get rid of every 0 that follows a one. Ex. lets assume my array is store={0, 1, 0, 1, 0, 0}. I need to create a new array that will be like this check={0, 1, 1, 0}. The third and fifth terms dont have to be inserted in the new array because they follow the second and fourth term (which are ones).

Also, in case you wonder, there are no occurrences of two consecutive ones, so no need to worry about that.

Here is my php code (but implementations in other languages are the same since its just for loops and arrays):

$j=0;   $count=0;
    for($j=0;$j<20; $j++) {
        $check[$j] = $store[$j + $count];
        if($store[$j]) {
            $count = $count + 1;                
        }
    }

The code is not working correctly. Maybe I am dead wrong in the way im implementing it. But Im a bit tired and started seeing things. I really can't see what im doing wrong.

PS: I didnt include the array declarations and other preceding code because i already tested it and I know they are ok. The problem is in the logic of this part of the code.

SeinopSys
  • 8,787
  • 10
  • 62
  • 110
Lazarus Rising
  • 2,597
  • 7
  • 33
  • 58

3 Answers3

1

In PHP you could just use str_split, a little regex and an implode without having to worry about a for loop. Adding in array_map, the results will be returned as integers instead of strings.

$check = array_map('intval',str_split('',preg_replace('/01/','',implode('',$store))));

Demo:

<?php
    $store = array(0, 1, 0, 1, 0, 0);
    $check = str_split(preg_replace('/10/','1',implode('',$store)));
    var_dump($check);
    $check = array_map('intval',$check)
    var_dump($check);
?>
SeinopSys
  • 8,787
  • 10
  • 62
  • 110
  • Actually, I like your elegant solution, but the datatype is a little of a big deal in this case (as I mentioned in the beginning of the question, this is just an abstraction of the real problem. I cannot even test the solution if its not in integer result) But I will see if I can convert the result and let all know – Lazarus Rising Feb 02 '15 at 16:55
  • The above solution works perfectly and its very easy to do the converting. For that, check this thread http://stackoverflow.com/questions/8963910/string-to-array-of-integers-php The accepted answer was the best way to convert between datatypes. – Lazarus Rising Feb 02 '15 at 17:02
  • @amygrimaldi I added the conversion to the answer. – SeinopSys Feb 02 '15 at 17:03
  • I already checked myself came up with the same method (cant expect other people to do all the work for me). Anyway, thanks a lot. – Lazarus Rising Feb 02 '15 at 17:05
0

This is the output I get from running your code:

Array
(
    [0] => 0
    [1] => 1
    [2] => 1
    [3] => 0
    [4] =>
    [5] =>
    [6] =>
    [7] =>
    [8] =>
    [9] =>
    [10] =>
    [11] =>
    [12] =>
    [13] =>
    [14] =>
    [15] =>
    [16] =>
    [17] =>
    [18] =>
    [19] =>
)

So basically it's working, except for the presence of null values at the end of your array since you're reading values outside of the bounds of the $store array. Simply check whether you're outside of the bounds of the array in your for loop. Notice I added +$count to the operation.

$store = array(0, 1, 0, 1, 0, 0);
$j=0;
$count=0;
$check = array();

for($j=0;$j+$count<count($store); $j++) {
    $check[] = $store[$j + $count];
    if($store[$j+$count]) {
        $count = $count + 1;                
    }
}

Output:

Array
(
    [0] => 0
    [1] => 1
    [2] => 1
    [3] => 0
)
SolarBear
  • 4,534
  • 4
  • 37
  • 53
  • Null values at the end of the array are not an issue, I am not going to read them later anyway so dont mind them. And it did (by coincidence) work with the above input. It does work with some inputs and not with others. For example try $store = array(1, 0, 0, 1, 0, 1, 0, 0, 1, 0 ); – Lazarus Rising Feb 02 '15 at 16:26
0

As I understood the question you just need the preceeding element to decide whether a 0 is included in the result or not:

<?php
$store=[0, 1, 0, 1, 0, 0];

$prev=0; $result = array();
foreach($store as $e) {
    if ( $e || !$prev ) {
        $result[] = $e;
    }
    $prev = $e;
}

var_export($result);

...there's most likely a more elegant solution but this one prints

array (
  0 => 0,
  1 => 1,
  2 => 1,
  3 => 0,
)
VolkerK
  • 95,432
  • 20
  • 163
  • 226