Replace array value to increment
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);
php arrays
add a comment |
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);
php arrays
What have you tried so far?
– Nigel Ren
Nov 13 '18 at 20:27
Show us some code!
– Brogan
Nov 13 '18 at 20:30
If you increment 23, does that not become 24?
– Progrock
Nov 13 '18 at 20:32
Out of curiosity... what's function collect?
– dn Fer
Nov 13 '18 at 21:08
@dnFer I think it's probably this.
– Don't Panic
Nov 13 '18 at 21:28
add a comment |
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);
php arrays
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);
php arrays
php arrays
edited Nov 13 '18 at 21:30
Don't Panic
28.9k93856
28.9k93856
asked Nov 13 '18 at 20:27
Cristal TravelCristal Travel
383
383
What have you tried so far?
– Nigel Ren
Nov 13 '18 at 20:27
Show us some code!
– Brogan
Nov 13 '18 at 20:30
If you increment 23, does that not become 24?
– Progrock
Nov 13 '18 at 20:32
Out of curiosity... what's function collect?
– dn Fer
Nov 13 '18 at 21:08
@dnFer I think it's probably this.
– Don't Panic
Nov 13 '18 at 21:28
add a comment |
What have you tried so far?
– Nigel Ren
Nov 13 '18 at 20:27
Show us some code!
– Brogan
Nov 13 '18 at 20:30
If you increment 23, does that not become 24?
– Progrock
Nov 13 '18 at 20:32
Out of curiosity... what's function collect?
– dn Fer
Nov 13 '18 at 21:08
@dnFer I think it's probably this.
– Don't Panic
Nov 13 '18 at 21:28
What have you tried so far?
– Nigel Ren
Nov 13 '18 at 20:27
What have you tried so far?
– Nigel Ren
Nov 13 '18 at 20:27
Show us some code!
– Brogan
Nov 13 '18 at 20:30
Show us some code!
– Brogan
Nov 13 '18 at 20:30
If you increment 23, does that not become 24?
– Progrock
Nov 13 '18 at 20:32
If you increment 23, does that not become 24?
– Progrock
Nov 13 '18 at 20:32
Out of curiosity... what's function collect?
– dn Fer
Nov 13 '18 at 21:08
Out of curiosity... what's function collect?
– dn Fer
Nov 13 '18 at 21:08
@dnFer I think it's probably this.
– Don't Panic
Nov 13 '18 at 21:28
@dnFer I think it's probably this.
– Don't Panic
Nov 13 '18 at 21:28
add a comment |
2 Answers
2
active
oldest
votes
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) {
Thank you very much @Nigel, this really saved my day...
– Cristal Travel
Nov 13 '18 at 20:49
add a comment |
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
$val = $counter++;or$val = ++$counter;saves you a line of code. ;)
– dn Fer
Nov 13 '18 at 21:13
@dnFer error occurred
– Smartpal
Nov 13 '18 at 21:26
Not when I change your demo.
– dn Fer
Nov 13 '18 at 21:30
@dnFer Link modified demo
– Smartpal
Nov 13 '18 at 21:33
3v4l.org/eIEhe
– dn Fer
Nov 13 '18 at 21:41
|
show 7 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288980%2freplace-array-value-to-increment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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) {
Thank you very much @Nigel, this really saved my day...
– Cristal Travel
Nov 13 '18 at 20:49
add a comment |
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) {
Thank you very much @Nigel, this really saved my day...
– Cristal Travel
Nov 13 '18 at 20:49
add a comment |
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) {
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) {
answered Nov 13 '18 at 20:43
Nigel RenNigel Ren
27.2k61833
27.2k61833
Thank you very much @Nigel, this really saved my day...
– Cristal Travel
Nov 13 '18 at 20:49
add a comment |
Thank you very much @Nigel, this really saved my day...
– Cristal Travel
Nov 13 '18 at 20:49
Thank you very much @Nigel, this really saved my day...
– Cristal Travel
Nov 13 '18 at 20:49
Thank you very much @Nigel, this really saved my day...
– Cristal Travel
Nov 13 '18 at 20:49
add a comment |
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
$val = $counter++;or$val = ++$counter;saves you a line of code. ;)
– dn Fer
Nov 13 '18 at 21:13
@dnFer error occurred
– Smartpal
Nov 13 '18 at 21:26
Not when I change your demo.
– dn Fer
Nov 13 '18 at 21:30
@dnFer Link modified demo
– Smartpal
Nov 13 '18 at 21:33
3v4l.org/eIEhe
– dn Fer
Nov 13 '18 at 21:41
|
show 7 more comments
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
$val = $counter++;or$val = ++$counter;saves you a line of code. ;)
– dn Fer
Nov 13 '18 at 21:13
@dnFer error occurred
– Smartpal
Nov 13 '18 at 21:26
Not when I change your demo.
– dn Fer
Nov 13 '18 at 21:30
@dnFer Link modified demo
– Smartpal
Nov 13 '18 at 21:33
3v4l.org/eIEhe
– dn Fer
Nov 13 '18 at 21:41
|
show 7 more comments
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
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
edited Nov 14 '18 at 9:21
answered Nov 13 '18 at 20:51
SmartpalSmartpal
962516
962516
$val = $counter++;or$val = ++$counter;saves you a line of code. ;)
– dn Fer
Nov 13 '18 at 21:13
@dnFer error occurred
– Smartpal
Nov 13 '18 at 21:26
Not when I change your demo.
– dn Fer
Nov 13 '18 at 21:30
@dnFer Link modified demo
– Smartpal
Nov 13 '18 at 21:33
3v4l.org/eIEhe
– dn Fer
Nov 13 '18 at 21:41
|
show 7 more comments
$val = $counter++;or$val = ++$counter;saves you a line of code. ;)
– dn Fer
Nov 13 '18 at 21:13
@dnFer error occurred
– Smartpal
Nov 13 '18 at 21:26
Not when I change your demo.
– dn Fer
Nov 13 '18 at 21:30
@dnFer Link modified demo
– Smartpal
Nov 13 '18 at 21:33
3v4l.org/eIEhe
– dn Fer
Nov 13 '18 at 21:41
$val = $counter++; or $val = ++$counter; saves you a line of code. ;)– dn Fer
Nov 13 '18 at 21:13
$val = $counter++; or $val = ++$counter; saves you a line of code. ;)– dn Fer
Nov 13 '18 at 21:13
@dnFer error occurred
– Smartpal
Nov 13 '18 at 21:26
@dnFer error occurred
– Smartpal
Nov 13 '18 at 21:26
Not when I change your demo.
– dn Fer
Nov 13 '18 at 21:30
Not when I change your demo.
– dn Fer
Nov 13 '18 at 21:30
@dnFer Link modified demo
– Smartpal
Nov 13 '18 at 21:33
@dnFer Link modified demo
– Smartpal
Nov 13 '18 at 21:33
3v4l.org/eIEhe
– dn Fer
Nov 13 '18 at 21:41
3v4l.org/eIEhe
– dn Fer
Nov 13 '18 at 21:41
|
show 7 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288980%2freplace-array-value-to-increment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What have you tried so far?
– Nigel Ren
Nov 13 '18 at 20:27
Show us some code!
– Brogan
Nov 13 '18 at 20:30
If you increment 23, does that not become 24?
– Progrock
Nov 13 '18 at 20:32
Out of curiosity... what's function collect?
– dn Fer
Nov 13 '18 at 21:08
@dnFer I think it's probably this.
– Don't Panic
Nov 13 '18 at 21:28