3

I have an array like this ...

[0,0,23,0,0,18,0,0]

Then I want to change values ​​that are not '0' (23 & 18) to auto increment, so the end result will be like this,

[0,0,1,0,0,2,0,0]

is there the best way for all that? So far this is what I did, but the results were not as expected ... :)

<?php

$arr = [0,0,23,0,0,18,0,0];
$x = 1;
$r = collect($arr)->map(function ($value, $key)use($x) {
    if ($value == 0) {
        return $value;
    } else {
        return $x++;
    }
})->all();

dd($r);
5
  • What have you tried so far? Commented Nov 13, 2018 at 20:27
  • Show us some code! Commented Nov 13, 2018 at 20:30
  • If you increment 23, does that not become 24? Commented Nov 13, 2018 at 20:32
  • Out of curiosity... what's function collect? Commented Nov 13, 2018 at 21:08
  • @dnFer I think it's probably this. Commented Nov 13, 2018 at 21:28

2 Answers 2

3

The problem is that when you have your value of $x passed into the function via use, this isn't allowing the value to be updated. You need to pass it by reference - use (&$x) to allow it to increment the value outside the function...

$r = collect($arr)->map(function ($value, $key) use (&$x) {
Sign up to request clarification or add additional context in comments.

Comments

0

Another way... Using foreach with reference of array &

With Passed by Reference

Snippet

$arr = [0,0,23,0,0,18,0,0];
$counter = 1;
foreach ($arr as &$val){
   if($val !== 0){
      $val = $counter++;
   }
}
print_r($arr);

Note: Be aware of dangling reference


Without reference

Snippet

$arr = [0,0,23,0,0,18,0,0];
    $counter = 1;
    foreach ($arr as $key => $val){
       if($val !== 0){
          $arr[$key] = $counter++;
       }
    }
print_r($arr);

Output

Array
(
    [0] => 0
    [1] => 0
    [2] => 1
    [3] => 0
    [4] => 0
    [5] => 2
    [6] => 0
    [7] => 0
)

Live demo
Pass by reference docs

9 Comments

$val = $counter++; or $val = ++$counter; saves you a line of code. ;)
Not when I change your demo.
@dnFer Link modified demo
@dnFer Thanks, modified.
@Progrock look at foreach, passed array by reference, that's why array is being altered directly.
|

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.