3

So I have a collection of integer values, which is built from a result of another function that can have different values every time. Consider the following in PHP:

$arr = [0,0,2,2,0,0,0,3,3];

Which i need to convert to:

$newArr = [null,0,2,2,0,null,0,3,3];

What i want to accomplish is: If a value is > 0, its neighbours should be 0, and the rest should be null.

What is the best strategy here?

4
  • Answer below :) It should work for you. Commented Oct 27, 2016 at 7:39
  • Thank you all so much for your inputs, i have accepted the answer which helped the most by @Casimir et Hippolyte. Keep it up! Commented Oct 27, 2016 at 8:02
  • You can always vote up those which worked too ;) Thanks! Commented Oct 27, 2016 at 8:06
  • 1
    I wish i could but i don't have enough reputation yet :( Commented Oct 27, 2016 at 8:08

6 Answers 6

2

Playing with operator precedence:

$zero = true;

$arr = [0,0,2,2,0,0,0,3,3];
$newArr = [];

foreach($arr as $k=>$v) {
    if ($v) {
        $newArr[] = $v;
        $zero = false;
    } else {
        if ($zero and isset($arr[$k+1]) && !$arr[$k+1] || !isset($arr[$k+1]))
            $newArr[] = null;
        else
            $newArr[] = 0;

        $zero = true;
    }
}

print_r($newArr);
Sign up to request clarification or add additional context in comments.

Comments

2

Looping through the entire array, we evaluate each element for three conditions:

1.Element is zero.

2.Previous element is set, and it is equal to zero or null.

3.Next element is set, and it is equal to zero or null.

<?php
    foreach($array as $key => $element)
    {
        if($element == 0 && ((isset($array[$key - 1]) && !$array[$key - 1]) || (isset($array[$key + 1]) && !$array[$key + 1])))
        {
            $array[$key] = null;
        }
    }
?>

Comments

0

I think it should work for you:

<?php
$arr = [0,0,2,2,0,0,0,3,3];
$new_array = $arr;

$array_count = count($arr);

for ($i=0; $i<$array_count; $i++) {
    if ($i == 0 && $arr[$i+1] == 0) {   
        $new_array[$i] = null;
    } elseif ($i == ($array_count-1) && $arr[$i-1] == 0) {  
        $new_array[$i] = null;
    } elseif ($arr[$i-1] == 0 && $arr[$i+1] == 0) {     
        $new_array[$i] = null;
    }
}
echo "<pre>";
print_r($new_array);

?>

1 Comment

Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
0

We need to check for three conditions: is there, prev. is zero and next is zero. You can combine first two conditions into the third one. Broke it down for simplicity.

$array = [0,0,2,2,0,0,0,3,3];

$newArray = [];

foreach ($array as $key => $val) {

    $previous = NULL;
    $next = NULL;

    if (isset($array[$key + 1])) {
        $next = $array[$key + 1];
    }

    if ($key != 0) {
        $previous = $array[$key - 1];
    }

    if ($val === 0 && $next == 0 && $previous == 0) {
        $newArray[] = 'NULL';
    } else {
        $newArray[] = $val;
    }

}

Comments

0

Another way to do this:

$arr = [0,0,2,2,0,0,0,3,3];

foreach($arr as $key => $value){
    if($arr[$key] > 0 && isset($arr[$key - 1]) && $arr[$key - 1] == 0){
        if(isset($arr[$key - 2]) && $arr[$key - 2] == 0){
            $arr[$key - 2] = null;  
        }
    }
    if($arr[$key] > 0 && isset($arr[$key + 1]) && $arr[$key + 1] == 0){
        if(isset($arr[$key + 2]) && $arr[$key + 2] == 0){
            $arr[$key + 2] = null;  
        }
    }
}
echo '<pre>';
print_r($arr);
echo '</pre>';

Here, i am looking for the positive value first then checking and if neighbor is 0 then set neighbor's neighbor to null.

Comments

-1
<?php

    $arr        = [0,0,2,2,0,0,0,3,3];
    $extra      = [];


    for($x=0; $x<count($arr); $x++){
        if($arr[$x] == 0){
            $tmp = isset($tmp)?$tmp:[];
            $tmp[]=$x;
        }else{
            if(isset($tmp)){
                $extra[]  = $tmp;
                unset($tmp);
            }
        }
    }
    foreach($extra as $subArr){
        if(count($subArr) == 2){
            $arr[$subArr[0]] = null;
            $arr[$subArr[1]] = 0;
        }else if(count($subArr) == 3){
            $arr[$subArr[0]] = 0;
            $arr[$subArr[1]] = null;
            $arr[$subArr[2]] = 0;
        }
    }
    var_dump($arr);
    // YIELDS::
        array (size=9)
          0 => null
          1 => int 0
          2 => int 2
          3 => int 2
          4 => int 0
          5 => null
          6 => int 0
          7 => int 3
          8 => int 3

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.